0% found this document useful (0 votes)
19 views8 pages

Flow of Control

Computer

Uploaded by

hemlatasahu0103
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
19 views8 pages

Flow of Control

Computer

Uploaded by

hemlatasahu0103
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 8

FLOW OF CONTROL

STATEMENTS IN PYTHON
• Statements are instructions given to the computer to perform an action. They may be
◦ Empty / Null – a statement which does nothing e.g. pass
◦ Simple / single line – single executable statement e.g. print (25)
◦ Compound – group of statements executed as a unit
▪ header – begins with a keyword and ends with a colon
▪ body – one or more python statements indented at the same level inside the header
• Statement execution may be sequential, selective or iterative.
◦ Sequential - Represents default or normal flow of control
◦ Selective / Conditional - Execution depends on a condition-test. Ifcondition is fulfilled
(True), one set of statements arefollowed, otherwise a different set of statements are
followed.
◦ Iterative / Looping construct
▪ Represents repetition of a set of statements, based upon a condition-test.
▪ The set of repeated statements is the body of the loop.
▪ The condition on which the exit of a loop depends, is called the exit condition.

INDENTATION
• Indentation refers to the spaces at the beginning of a code line.
• Python uses indentation to indicate a block of code. The indentation in Python is not just
for readability
• Number of spaces is up to the programmer, but has to be at least one and the same number
of spaces have to be used in the same block of code, otherwise Python gives an indentation
error.
• By default, python gives 4 spaces for indentation.
SELECTION / DECISION MAKING
• Selection / decision making in Python is facilitated by the if statement
• If the condition tested by the if statement is true, a statement / set of statements are followed,
otherwise (if condition evaluates false, those statements are ignored and another course of
action is followed.
• if <conditional expression> : ← header
statement /
statements / ← body
pass

ch = input (“Enter a single character : ”)Enter a single character : ”))


if ch >= ‘0’ and ch <= ‘9’ :0’ and ch <= ‘9’ : and ch <= ‘0’ and ch <= ‘9’ :9’ and ch <= ‘9’ : :
print (“Enter a single character : ”)You entered a digit.”))
if ch == ‘0’ and ch <= ‘9’ : ’ and ch <= ‘9’ : :
print (“Enter a single character : ”)You entered a space.”))

age = int (input(“Enter a single character : ”)Enter your age : “Enter a single character : ”)))
if age >= 18 :
print (“Enter a single character : ”)Eligible to vote”))
• A variant of the if statement is the if–else statement.
◦ if <conditional expression> :
statements
else :
statements

a = int (input (“Enter a single character : ”)Enter a number”)))


if a >= 0 :
print (a, “Enter a single character : ”)is positive”))
else :
print (a, “Enter a single character : ”)is negative”))
• In some cases where another condition needs to be tested (if condition evaluates to false),
the if–elif statement is used
◦ if <conditional expression> :
statements
elif <conditional expression> :
statements
else :
statements

a = int (input (“Enter a single character : ”)Enter a number”)))


if a > 0 :
print (a, “Enter a single character : ”)is positive”))
elif a == 0 :
print (a, “Enter a single character : ”)is equal to zero”))
else :
print (a, “Enter a single character : ”)is negative”))
• At times when all of the above aren’t enough to test all conditions, the nested–if statement is
used.
◦ if inside if’s body
▪ if <conditional expression> :
if <conditional expression> :
statements
else :
statements
elif <conditional expression> :
statements
else :
statements
◦ if inside elif’s body
▪ if <conditional expression> :
statements
elif <conditional expression> :
if <conditional expression> :
statements
else :
statements
else :
statements
◦ if inside else’s body
▪ if <conditional expression> :
statements
elif <conditional expression> :
statements
else :
if <conditional expression> :
statements
else :
statements
◦ Multiple if’s
▪ if <conditional expression> :
if <conditional expression> :
statements
else :
statements
elif <conditional expression> :
if <conditional expression> :
statements
else :
statements
else :
if <conditional expression> :
statements
else :
statements
• Named conditional - conditions stored in a name (variable), in case of complex and
repetitive conditions.
e.g.
if deposit is less than ₹ 2000, and for 2 or more years, rate of interest is 5 % 2000, and for 2 or more years, rate of interest is 5 %
if deposit is ₹ 2000, and for 2 or more years, rate of interest is 5 % 2000 or more but less than ₹ 2000, and for 2 or more years, rate of interest is 5 % 6000, and for 2 or more years, rate of interest is
7%
if deposit is more than ₹ 2000, and for 2 or more years, rate of interest is 5 % 6000, and for 1 or more years, rate of interest is 8 %
On all deposits of 5 years or more, interest is 10% compounded annually.
Traditionally, code is written as :

if deposit < 2000 and time >= 2 :


rate = 0.05
.
.
.

But if code is named separately, and used later, the code becomes much more readable and
understandable

r_5 = deposit < 2000 and time >= 2


r_7 = 2000 <= deposit < 6000 and time >= 2
r_8 = deposit > 6000 and time >= 1
r_10 = time >= 5

if r_5 :
rate = 0.05
elif r_7 :
rate = 0.07
.
.
.

REPETITION / ITERATION / LOOPING


• To carry out repetitive tasks, Python provides the following looping / iterative statements
◦ Counting loop for – loops repeat a certain number of times
◦ Conditional loop while – loops repeat until a given condition is true.
• range ( ) function
◦ Used in the for loop
◦ It generates a list (sequence type data type). Some other sequence types are – strings,
lists, tuples, etc.
◦ Lower limit is included in the list, upper limit is not.
◦ range (<lower limit>, <upper limit>)
#both limits should be integers
e.g. range (0, 5) = [0, 1, 2, 3, 4] range (5, 0) = [] # +1 → default step-value
◦ range (<lower limit>, <upper limit>, <step value>)
#all values should be integers
e.g. range (10, 1 , -2) = [10, 8, 6, 4, 2]
◦ range (number) = creates list from 0 to number-1
e.g. range (5) = [0, 1, 2, 3, 4]
• in, not in operators
◦ Membership operators, also used in the for loop, to check if a value is contained inside a
list.
◦ 3 in [1, 2, 3, 4] = True
5 in [1, 2, 3, 4] = False
◦ not in does the opposite of in operator
◦ These operators work with all sequence types.
◦ line = input (“Enter a line : ”)
string = input (“Enter a string : ”)

if string in line :
print (string, “is a part of”, line)
else :
print (string, “is not in”, line)

• for LOOP
◦ Designed to process the items of a sequence, one by one
◦ for <variable> in <sequence> :
statements to repeat (body)
◦ How is the for loop processed ?
▪ The loop variable is assigned the first value in the sequence
▪ Statements in the body are executed with assigned value of loop variable
▪ Then, the loop variable is assigned the next value in the sequence and the loop body
is executed
▪ This continues till all values in the sequence are processed

for a in [1, 4, 7] :
print (a)
print (a * a)

Output : 1
1
4
16
7
49
for ch in ‘calm’ :
print (ch)

Output : c
a
l
m
• while LOOP
◦ Repeats the instructions within itself as long as a conditional remains true.
◦ while <logical expression> :
loop-body
◦ How is the while loop processed ?
▪ The logical / conditional expression in the loop is evaluated
▪ – If the result of step 1. is true, then all statements in the loop’s body are executed
– If the result is false, then control moves to the next statement after the loop’s body
▪ Body of loop is executed, if result of conditional expression is is true.
▪ Steps are repeated as long as condition remains true
◦ Elements of a while loop
▪ Initialization expression(s)
• Loop variable must be initialized before entering the loop.
• This initialization gives the variable its first value and is outside the loop before
it starts.
▪ Test expression(s)
• The expression whose truth value decides whether loop body will be executed or
not
• In a while loop, text expression is tested before entering the loop
▪ Body of loop – statements executed repeatedly
▪ Update Expression(s)
• Changes value of loop variable
• present as a statement inside the loop body
◦ While loop is an example of of an entry-controlled loop, since condition-check is at the
entry of the loop
◦ n=1 #initialization
while n < 5 :
print (“Square of”, n, ‘is’, n * n)
n += 1
print (“Thank you.”)

Output :- Square of 1 is 1
Square of 2 is 4
Square of 3 is 9
Square of 4 is 16
Thank You.

• Jump statements – break and continue


◦ Both statements enable a program to skip over a part of the code
◦ break statement
▪ Terminates the very loop it lies within
▪ Execution resumes at the statement immediately following the body of the
terminated statement
◦ continue statement
▪ Forces the next iteration of the loop to take place
▪ For the for loop, it causes the next iteration by updating the loop variable with next
value in sequence; for the while loop, program control passes to the conditional text
given at the top of the loop
◦ print (“Enter a single character : ”)The loop with ‘0’ and ch <= ‘9’ :break’ and ch <= ‘9’ : produces output :”))
for i in range (1, 11) :
if i % 3 == 0 :
break
else :
print (i)
print (“Enter a single character : ”)The loop with ‘0’ and ch <= ‘9’ :continue’ and ch <= ‘9’ : produces output :”))
for i in range (1, 11) :
if i % 3 == 0 :
continue
else :
print (I)

Output :-
The loop with ‘break’ produces output :
1
2

#loop terminates with break, when condition becomes true

The loop with ‘continue’ produces output :


1
2
4
5
7
8
10

#when condition became true, the loop simply moved to the next iteration, and iterations
with true condition are terminated

◦ It is discouraged to use these, whenever possible

• Loop else statement


◦ Both loops of Python (for and while) have an else clause, which is different from the
else of if-else statements
◦ This else statement executes only when the loop ends normally (while loop’s test
condition has resulted in false or for loop has executed for the last value in sequence)
◦ The else clause appears at the same indentation as that of while or for
◦ for <variable> in <sequence> : while <logical expression> :
statements loop-body
else : else :
statements statements
◦ for a in range (1, 4) :
print (“Element is”, end = ‘ ’)
print (a)
else :
print (“Ending loop after printing all elements”)
Output :-
Element is 1
Element is 2
Element is 3

Ending loop after printing all elements

• Nested Loops
◦ A loop containing another loop in its body
◦ Inner loop must terminate before the outer loop
◦ The value of the outer loop variable will change only after the inner loop is completely
finished
◦ for o in range (5, 10, 4) :
for i in range (1, o, 2) :
print (o, i)

Output :-

5 1
5 3
9 1
9 3
9 5
9 7

You might also like