Flow of Control
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
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
But if code is named separately, and used later, the code becomes much more readable and
understandable
if r_5 :
rate = 0.05
elif r_7 :
rate = 0.07
.
.
.
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.
Output :-
The loop with ‘break’ produces output :
1
2
#when condition became true, the loop simply moved to the next iteration, and iterations
with true condition are terminated
• 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