5 Flow Control
5 Flow Control
Flow control:
Statement Flow control:
In a program, statements may be executed sequentially, selectively or
iteratively.
Sequence:
The sequence construct means the statements are being executed
sequentially. This represents the default flow of statement.
Selection:
The selection construct means the execution of statements depending upon a
condition-test. If a condition evaluates True, a set of statements is followed
otherwise a different set of statements are executed. This construct is also called
decision construct because it helps in making decision about which set of statements
are executed.
We can apply decision making or selection in your real life so many times e.g., if the
traffic signal light is red, then stop; if the traffic signal light is yellow then wait; and if
the signal is green then go.
Iteration(Looping):
The iteration constructs means repetition of a set of statements depending upon a
condition-test. Till the time a condition is True (or False depending upon the loop), a
set of statements are repeated again and again. As soon as the condition becomes
False. The iteration construct is also called as looping construct.
Flow control
if for break
if-else while continue
if-elif-else pass
1
Python
Ex:
name=input("Enter name") if
name="Krishna":
print("Hello Krishna!Goodmorning")
print("How r u")
If-else condition
Syntax:
if condition:
Statements
else:
statements
This form of if statement tests a condition and if the condition evaluates to True, it
carries out statements indented below if and in case condition evaluates to false, it
carries out statements indented below else.
Ex:
name=input("Enter name") if
name="rama":
print("Hello rama! Good morning")
else:
print("Hello Guest! How r u")
2
Python
If-elif-else condition
Syntax:
if condition1:
Statements
elif condition2:
statements
elif condition3:
statements
elif condition-n:
statements
else:
statements
Ex:
a=int(input("Enter a number:") if
a==1:
print("Sunday")
elif a==2:
print("Monday")
elif a==3:
print("Tuesday")
elif a==4:
print("Wednesday")
elif a==5:
print("Thursday")
elif a==6:
print("Friday")
elif a==7:
print("Saturday")
else:
print("Invalid no")
3
Python
Ternary operators: -
Syntax:
x=firstvalue if condition else secondvalue
If condition is True then firstvalue will be considered else secondvalue will be
considered.
Ex: To read two numbers and print minimum value
a=int(input("Enter firstno:"))
b=int(input("Enter secondno:"))
min=a if a<b else b
print("Minimum value=",min)
4
Python
Iterative statements:
The iteration statements or repetition statements allow a set of instructions to be
performed repeatedly until a certain condition is fulfilled. The iteration statements are
also called loops or looping statements.
Python provides two kinds of loops:
- While loop
- For loop
Actually loops are two categories:
Counting loops: The loops that repeat certain number of times. Python’s for loop is
counting loop.
Conditional loop: The loops that repeat until a certain thing happens i.e, they
keep repeating as long as some condition is true. Python’s while loop is conditional
loop.
While loop:
If we want to execute a group of statements iteratively until some condition false,
then we should go for while loop.
Syntax:
while condition:
statements
5
Python
Ex:
while True:
eid = input("Enter emp id")
ename=input("enter emp name")
if(ename=="krishna"):
print("login success")
break
else:
print("login fail try with valid name")
6
Python
for loop:
If we want to execute some action for every element present in some sequence
(it may be string or collection) then we should go for for loop. Syntax:
for variable in sequence:
statements
The loop variable is assigned the first value in the sequence.
All the statements in the body of for loop are executed with assigned value of
loop variable.
Once step 2 is over, the loop variable is assigned the next value in the sequence and
the loop body is executed with the new value of loop variable.
This continues until all values in the sequences are processed.
>>>r=range(10)
>>>type(r)
<class ‘range’>
>>>r[0]
0
>>>r[-1]
9
>>>r[2:8]
range(2,8)
>>>for i in r(2:8): print (i)
2
3
7
Python
4
5
6
7
range(begin,end) begin to end-1
range(10,20) 10 to 19
>>>for i in range(10,20): print(i)
10 11 12 13 14 15 16 17 18 19
range(begin,end,step) begin to end-1 change by step value
range(1,101,5) 1,6,11,16…..
>>>for i in range(0,101,5): print(i)
Ex:
for i in range(5):
print("Hello:",i)
8
Python
Ex:
for i in range(1,10):
if i%2==0:
print("even number=",i)
else:
print("odd number",i)
To check whether a value is not contained inside a list we can use not in
operator.
Ex:
3 not in [1,2,3,4]
#This expression will test if value 3 is not contained in the given sequence.
Will return False as value 3 is contained in sequence [1,2,3,4]
5 not in [1,2,3,4]
#This expression will test if value 5 is not contained in the given sequence.
Will return True as value 5 is not contained in sequence [1,2,3,4]
9
Python
Ex:
words = ["cat", "apple", "one","four"]
for w in words:
print(w, len(w))
Ex:
result4 = [ ]
l=[10,20,30,40]
for x in l:
if x>20:
result4.append(x+1)
print(result4)
10
Python
Nested loops:
Sometimes we can take a loop inside another loop, which are also known as nested
loops.
Ex:
for i in range(4):
for j in range(4):
print("i=",i," j=",j)
Transfer statements:
break: Break can be used to unconditionally jump out of the loop. It terminates the
execution of the loop. Break can be used in while loop and for loop. Break is mostly
required, when because of some external condition, we need to exit from a loop.
Example
for letter in "Python":
if letter = = 'h':
break
print letter
Ex:
for i in range(10):
if i==7:
print("Processing is enough….")
break
print(i)
print("End of loop")
11
Python
Ex:
a=int(input("Enter any number greater than 10:\t"))
while a>0:
a=a-1
if a==10:
break
else:
print(a,"These are the numbers upto 10")
Ex:
a=b=c=0
for i in range(1,21):
a=int(input("Enter number1: "))
b=int(input("Enter number2: "))
if b==0:
print("Division by zero error")
break
else:
c=a/b
print("Quotient=",c)
print("Program over")
continue: - Within the loop, if you want to current iteration should be skipped and
continue to the next iteration then continue statement is used.
Example
for letter in "Python":
if letter == 'h':
continue
print letter
P
y
12
Python
t
o
n
Ex:
for i in range(10):
if i%2==0:
continue
print(i)
Ex:
a=int(input("Enter any number greater than 10:\t"))
while a>0:
a=a-1
if a==10:
continue
else:
print (a,"These are the numbers upto 10")
Ex:
a=b=c=0
13
Python
for i in range(3):
a=int(input("Enter number1: "))
b=int(input("Enter number2: "))
if b==0:
print("The denominator cannot be zero. Enter again")
continue
else:
c=a/b
print("Quotient=",c)
Ex:
numbers=[10,20,0,5,0,30]
for n in numbers:
if n==0:
print("Cannot divide by zero")
continue
print("100/{} = {}".format(n,100/n)
14
Python
Ex:
for a in range(1,4):
print("Element is:",a)
else:
print("Ending loop after printing all elements of sequence")
Ex:
for a in range(1,4):
k=int(input("Enter a number:"))
if k%2==0:
break
15
Python
print("Element is:",k)
else:
print("Ending loop after printing all elements of sequence")
Ex: else is always executed after the loop is over unless a break
statement is encountered.
a=0
while(a<10):
print(a)
a=a+1
else:
print("else block after while");
print("process done")
16