Flow control statements
Sensitivity: Internal & Restricted
Agenda
1 if else 5 break
2 elif 6 continue
3 for 7 pass
4 while
Sensitivity: Internal & Restricted © confidential 2
if else
Sensitivity: Internal & Restricted
if else
• if statements are used for decision making i.e whether a block of code needs to be
executed or not.
• if block can be optionally followed by an else block.
Syntax:
if(condition):
All statements
statement-1
which belong to statement-2
the if block are . .
having same . .
indentation. statement-n
Sensitivity: Internal & Restricted © confidential 4
if else continued..
Syntax:
All statements
if(condition):
which belong to statement-1
the if block are statement-2
having same . .
indentation. statement-n
else:
All statements
statement-1
which belong to
the else block are
statement-2
having same . .
indentation. statement-n
Sensitivity: Internal & Restricted © confidential 5
if else continued..
Predict the output:
a = 10 You are right..!!
if(a%2 == 0): Output:
print("Even")
Even
else:
print("Odd")
Sensitivity: Internal & Restricted © confidential 6
if else continued..
Predict the output:
a = 500
if(a%10 == 0):
print("In multiples of 10") You are right..!!
else:
SyntaxError because
print("Not in multiples of 10")
of the second else
else:
block.
print("End")
Sensitivity: Internal & Restricted © confidential 7
Multiple if statements
Predict the output:
a = 15
if(a%2 == 0): Output:
print("Even")
Odd
if(a%2 != 0):
Positive
print("Odd")
if(a >= 0):
print("Positive")
Sensitivity: Internal & Restricted © confidential 8
Nested if statements
Predict the output:
gender = ‘female’
age = 25
if(gender == ‘female’):
if(age > 18): Output:
print("Eligible")
Eligible
else:
print("Not Eligible")
else:
print("End")
Sensitivity: Internal & Restricted © confidential 9
Nested if statements continued..
Predict the output:
gender = ‘male’
age = 25
if(gender == ‘female’):
if(age > 18): Output:
print("Eligible")
End
else:
print("Not Eligible")
else:
print("End")
Sensitivity: Internal & Restricted © confidential 10
elif
Sensitivity: Internal & Restricted
elif
• You have multiple if conditions and when any one if condition is satisfied, you may want
other conditions not to be checked and to be simply skipped.
• elif is the solution. It is similar to else if in other languages like C,C++,Java.
Syntax:
if(condition-1):
elif(condition-2):
. .
. . When none of the
elif(condition-n): if conditions are
matching, else will
else: be executed.
Sensitivity: Internal & Restricted © confidential 12
elif continued..
Predict the output:
designation = ‘Engineer’
if(designation == 'Doctor'):
Output:
print('Hi Doctor..!!')
Hi Engineer..!!
elif(designation == 'Engineer'):
print('Hi Engineer..!!')
elif(designation == 'Lawyer'):
print('Hi Lawyer..!!') When this else will
be executed ?
else: Think..!!
print('Invalid designation')
Sensitivity: Internal & Restricted © confidential 13
for
Sensitivity: Internal & Restricted
for
• Two important uses of for loop:
1. It is used when a block of code needs to be executed more than once.
2. It is used to iterate over a collection of elements. Elements of List, Tuple, String,
Dictionary are iterated using for loop.
• All the statements that belong to the for loop should have same indentation.
• Syntax of for loop is different in Python.
• Before proceeding to that, lets quickly learn what is range( ) function?
Sensitivity: Internal & Restricted © confidential 15
What is range( ) function?
• range( ) is the built-in function which returns a range object, which is a sequence of
integers.
Syntax:
range(start, stop, step)
• start : It specifies the starting value for the sequence. Its an optional argument.
By default it is 0.
• stop : It specifies the ending value for the sequence and this value is excluded.
It is a mandatory argument.
• step : It specifies the increment or decrement value for the next number in the sequence.
Its an optional argument. By default it is 1.
Sensitivity: Internal & Restricted © confidential 16
Examples for range( ) function
Code Values generated
range (1, 5, 1) 1, 2, 3, 4
range (10) 0, 1, 2, 3, 4, 5, 6, 7, 8, 9
range (1, 3) 1, 2
range (1,10,2) 1, 3, 5, 7, 9
range (10, 0, -1) 10, 9, 8, 7, 6, 5, 4, 3, 2, 1
range (20, 0, -5) 20, 15, 10, 5
Sensitivity: Internal & Restricted © confidential 17
Using range( ) function with for loop
Program: Output:
for count in range(0, 5, 1): Hello..count is 0
print('Hello..count is ',count) Hello..count is 1
Hello..count is 2
Hello..count is 3
Hello..count is 4
Sensitivity: Internal & Restricted © confidential 18
Calculating sum of numbers from 1 to 10
Program: Output:
sum = 0; 55
for num in range(1, 11):
sum = sum + num
print(sum)
Sensitivity: Internal & Restricted © confidential 19
Using for loop with string to print each character
Program: Output:
name = ‘Tushar’ T
for letter in name: u
print(letter) s
h
a
r
Sensitivity: Internal & Restricted © confidential 20
Using for loop with list to print only the even numbers
Program: Output:
li = [2, 67, 44, 89]
2
for num in li: 44
if(num%2 == 0):
print(num)
Sensitivity: Internal & Restricted © confidential 21
Using for loop with tuple to calculate the sum of elements
Program: Output:
t1 = (1, 3, 5, 7)
16
sum = 0
for num in t1:
sum = sum + num
print(sum)
Sensitivity: Internal & Restricted © confidential 22
Using for loop with dictionary to print the values
Program:
Output:
d1 = {
'name' : 'Chandu', Chandu
'age' : 24, 24
'gender' : 'Male', Male
'country' : 'India' India
}
for key in d1:
print(d1[key])
Sensitivity: Internal & Restricted © confidential 23
Using else with for loop
• for loop can be optionally followed by an else block.
• Statements in else block will be executed only when for loop is not executed even once or
when the loop is completed without breaking in between.
Program: Output:
s = '' #empty string Empty string
for letter in s: #not executed even once
print(letter)
else:
print("Empty string")
Sensitivity: Internal & Restricted © confidential 24
Using else with for loop continued..
Program:
li=[1,2,3]
for x in li:
print(x,end=" ")
else:
print('\nLoop is completed without breaking')
Output:
1 2 3
Loop is completed without breaking
Sensitivity: Internal & Restricted © confidential 25
Nested for loops
Program: Output:
M
names = [‘MARCEL’, ‘CHAD’] A
for name in names: R
C
for letter in name: Statements that
E
belong to the
print(letter) outer for loop. L
***
print('***') Statements that C
belong to the
inner for loop.
H
A
D
***
Sensitivity: Internal & Restricted © confidential 26
while
Sensitivity: Internal & Restricted
while
• while loop is used to repeatedly execute certain block of statements as long as the given
condition is True.
• while loop can be optionally followed by an else block which will be executed only when
while loop is never executed i.e condition is always false.
Syntax:
while(condition):
All statements statement-1
which belong to statement-2
the while loop are . .
having same . .
indentation. statement-n
Sensitivity: Internal & Restricted © confidential 28
Calculating sum of digits using while loop
Program:
x = 12345
Output:
sum = 0
while(x!=0): 15
last_digit = x%10
sum = sum + last_digit
x = x//10
print(sum)
Sensitivity: Internal & Restricted © confidential 29
while with optional else block
Syntax:
while(condition):
statement-1
All statements
which belong to statement-2
the while loop are . .
having same . .
indentation. statement-n
All statements else:
which belong to statement-1
the else block are . .
having same statement-n
indentation.
Sensitivity: Internal & Restricted © confidential 30
while with optional else block continued..
Program: Output:
num = 0 End
while(num > 0): #condition is always false
print('Hello..')
else:
print('End')
Sensitivity: Internal & Restricted © confidential 31
while with optional else block continued..
Program:
i=10
while(i<20):
print(i,end=" ")
i=i+5
else:
print('\nLoop completed without breaking')
Output:
10 15
Loop completed without breaking
Sensitivity: Internal & Restricted © confidential 32
Nested while loops
Program:
row = 1
while(row<=5): Output:
col = 1
while(col<=row): *
print('*', end =" ") * *
col = col+1 * * *
print() * * * *
row = row+1 * * * * *
Sensitivity: Internal & Restricted © confidential 33
break
Sensitivity: Internal & Restricted
break
• break keyword is used only within loops.
• It helps in terminating the loop in between based on some condition.
• When break statement is encountered, control goes out of the loop.
• Statements written after the break statement are never executed.
Example:
while(condition):
if(condition):
break
statement-1 of if #never executed
statement-1 of while
Sensitivity: Internal & Restricted © confidential 35
break continued..
Program:
li = [1, 3, 5, 4, 7, 9]
for num in li:
if(num%2 == 0):
break Output:
print('After break')
1
else:
3
print(num)
5
print('Outside for loop') Outside for loop
Sensitivity: Internal & Restricted © confidential 36
continue
Sensitivity: Internal & Restricted
continue
• continue keyword is used only within loops.
• It helps in skipping certain statements from being executed based on some condition.
• When continue statement is encountered, all the following statements are skipped and loop
goes to the next iteration.
Example:
while(condition):
if(condition):
statement-1 of if
continue
All these statements are
else:
skipped when if condition is
statement-1 of else True and continue keyword
statement-1 of while is encountered.
Sensitivity: Internal & Restricted © confidential 38
continue keyword continued..
Program:
alphabets = ['A', 'B', 'C', 'D', 'E']
Output:
for letter in alphabets:
if(letter=='C'): A
continue B
else: D
print(letter) E
Sensitivity: Internal & Restricted © confidential 39
continue keyword continued..
Program:
Output:
a = 5
while(a>0): 5
if(a==3): ***
a = a-1 4
print('skipped') ***
continue skipped
else: 2
print(a) ***
print('***') 1
a = a-1 ***
Sensitivity: Internal & Restricted © confidential 40
pass
Sensitivity: Internal & Restricted
pass
• pass statement is doing no operation.
• It is executed like a valid statement but it does nothing.
• We can use pass statement inside a function to denote it does nothing for time being.
• We will see more about pass statement when we learn functions.
Program:
t1 = (1, 2, 3, 4, 5)
for x in t1: Output:
if(x%2 == 0):
pass #does nothing 1
else: 3
print(x) 5
Sensitivity: Internal & Restricted © confidential 42
Thank you
Sensitivity: Internal & Restricted