Flow of Control Class 11
Flow of Control Class 11
9 Flow Of Control
REVISION
Control Statements
Flow control statementsare used to control the flow of
execution depending upon the specified condition/logic.
1. if statements
An if statement is a
programming
conditional statement
that, if proved true,
performs a function or
displays information.
Decision Making Statement
1. if statements Syntax:
if(condition):
statement [statements]
e.g. notebooks = 2
if (notebooks == 2):
print('You have ')
print(‘two books’)
print(‘outside of if statement’) Output
You have two books
Note:To indicate a block of code in Python, you must indent each line of the
block by the same amount. In above e.g. both print statements are part of if
condition because of both are at same level indented but not the third print
statement.
Decision Making Statement
2. if-else Statements
#find absolute value
a=int(input("enter a number"))
if(a<0):
a=a*-1
print(a)
Output :-
condition matcing the
criteria
------------------------
------------------------
-----------
a=100
if not(a == 20):
print('a is not equal
to 20')
Decision Making Statement
2. if-else Statements
If-else statement
executes some code if
the test expression is
true (nonzero) and
some other code if the
test expression is false.
Decision Making Statement
2. if-else Statements
Syntax:
if(condition):
statements
else:
statements
e.g. a=10
if(a < 100):
print(‘les
s than
100')
else:
print(‘mo
re than
equal
100')
Decision Making Statement
3. Nested if-else statement
The nested if...else statement allows you
to check for multiple test expressions and
execute different codes for more than two
conditions.
Decision Making Statement
E.G.
3. Nested if-else num = float(input("Enter a number: "))
if num >= 0:
Syntax if num == 0:
If (condition): print("Zero")
else:
statements print("Positive number")
elif (condition): else:
print("Negative number")
statements OUTPUT
else: Enter a number: 5
Positive number
statements * Write python program
to find out largest of 3
numbers.
Decision Making Statement
if (year % 4) == 0:
if (year % 100) == 0:
if (year % 400) == 0:
print("{0} is a leap year".format(year))
else:
print("{0} is not a leap year".format(year))
else:
print("{0} is a leap year".format(year))
else:
print("{0} is not a leap year".format(year))
Iteration Statements (Loops)
1. While Loop
2. For Loop
x=1
while (x < 3):
print('inside while loop value of x is ',x)
x=x+1
else:
print('inside else value of x is ', x)
Output
inside while loop value of x is 1
inside while loop value of x is
2 inside else value of x is 3
*Write a program in python to
find out the factorial of a given
number
Iteration Statements (Loops)
While Loop continue
Infinite While Loop
e.g.
x=5
whil
e (x
==
5):
pri
nt(
‘in
sid
e
lo
op
')
Iteration Statements (Loops)
2. For Loop
It is used to iterate over items of any sequence, such as a list
or a string.
Syntax
for val in sequence:
statements
e.g.
for i in range(3,5):
print(i)
Output
3
4
Iteration Statements (Loops)
Output
5
4
range()
Functi
on
Param
eters
start:
Starting
Iteration Statements (Loops)
Output
1
2
3
No
Break
Iteration Statements (Loops)
Output
123456789
10
2 4 6 8 10 12 14
16 18 20
Iteration Statements (Loops)
for i in range(years):
n=n+((n*rate)/100)
print(n)
Iteration Statements (Loops)
3. Jump Statements
print("Th
e end")
Outp
ut s
t
r
Iteration Statements (Loops)
2.continue
It is used to skip all the remaining statements
in the loop and move controls back to the top of
the loop.
e.g.
for val in "init":
if val == "i":
continue
print(val)
print("The
end")
Output
n
t
The end
Iteration Statements (Loops)
3. pass Statement
This statement does nothing. It can be used when a
statement is required syntactically but the program
requires no action.
Use in loop
while True:
pass # Busy-wait for keyboard interrupt (Ctrl+C)
In function
It makes a controller to pass by without executing any code.
e.g.
def myfun():
pass #if we don’t use pass here then error message will be shown
print(‘my program')
OUTPUT
My program
Iteration Statements (Loops)
3. pass Statement continue
e.g.
for i in 'initial':
if(i == 'i'):
pass
else:
print(i)
OUTPUT
n
L
N
O