Ch-5 Flow of Control
Introduction:
Statements are the instructions given to the computer to perform some tasks.
Types of Statements:
Python statements can belong to one of the following three types:
1. Empty Statement
2. Simple Statement
3. Compound Statement
Empty statement:
The simplest statement is the empty statement i.e., a statement which does
nothing. In Python an empty statement is pass statement.
Syntax:
pass
Simple statement:
Any single executable statement is a simple statement in Python.
ex:
name=input (“Enter your Name “)
Compound Statement:
A Compound statement represents a group of statements executed as a unit.
Syntax:
<Compound Statement Header>:
<Indented Body containing multiple simple statements/compound statements>
• Here, Header line starts with the keyword and ends at colon (:).
• The body consists of more than one simple Python statements or compound
statements.
ex:
a=20
b=10
if a>b: # Compound statement header
print(“a is biggest”)
else: group of statements / Compound statements
print(“b is biggest”)
Statement Flow Control:
In a program, statements executes in sequential manner or in selective manner
or in iterative manner.
Sequence:
The Sequence construct means the statements are being executed sequentially.
This represents the default flow of statement.
Selection Statement:
The Selection constructs means the execution of statement(s) depending upon a
condition-test.
If a condition evaluates to True, a set of statements is followed otherwise
another different set of statements is followed.
This construct (selection) is also called as decision construct. It helps to
making decision about which set-of-statements is to be executed.
Repetition Iteration (Looping):
The repetition constructs mean repetition of a set-of-statements depending
upon a condition-test.
Till the time a condition is True, a set-of-statements are repeated again and
again. Once the condition becomes False, the repetition stops.
The iteration construct is also called looping construct.
The if statements of Python:
The if statements are the conditional statements in Python and these implement
selection constructs (decision constructs).
The if statement:
The simplest form of if statement tests a condition and if the condition
evaluates to true, it carries out some instructions and does nothing in case condition
evaluates to false.
Syntax:
if <condition>:
statement(s)
Flowchart:
ex:
a=20
b=10
if a>b: # if the condition evaluates True, then it prints the statement, otherwise exits
print(“a is biggest”)
The if-else statement:
If out of two statements, it is required to select one statement for processing on
the basis of a condition, if-else statement is to be used.
Syntax:
if <condition>:
statement(s) when condition is true
else:
statement(s) when condition is false
Flowchart:
ex:
a=20
b=10
if a>b: # if test evaluates True, the True statement execute, otherwise False statement
print(“a is biggest”) # True statement
else:
print(“b is biggest”) # False Statement
Nested if-else:
a=int(input(“enter a value’))
b=int(input(“enter a value’))
c=int(input(“enter a value’))
if a>b:
if a>c:
print(“a is biggest”)
else:
print(“c is biggest”)
else:
if b>c:
print(“b is biggest”)
else:
print(“c is biggest”)
The if-elif statement:
To check another condition in case the test-condition of if evaluates to false.
That is you want to check a condition when control reaches else part, i.e., condition
test in the form of else if.
Python provides if-elif and if-elif-else statements.
Syntax for if-elif:
if(conditional expression):
statement(s)
elif(conditional expression):
statement(s)
Syntax for if-elif-else:
if(conditional expression):
statement(s)
elif(conditional expression):
statement(s)
else:
statement(s)
ex:
a=int(input(“Enter first number”))
b=int(input(“Enter second number”))
ch=input(“enter operator + - * / %”)
if ch==’+’:
result=a+b
elif ch==’-‘:
result=a-b
elif ch==’*‘:
result=a*b
elif ch==’/‘:
result=a/b
elif ch==’%‘:
result=a%b
else:
print(“Invalid operator”)
print(a,ch,b,”=”,result)
Iteration Statements (Loops):
Iteration statements (loop) are used to execute a block of statements as long as
the condition is true. Loops statements are used when we need to run same code
again and again.
Python Iteration (Loops) statements are of three types:
1. While Loop
2. For Loop
3. Nested For Loops
1. While Loop:
It is used to execute a block of statement as long as a given condition is true.
If the condition becomes false, the control will come out of the loop.
The condition is checked every time at the beginning of the loop.
Syntax:
while (condition):
statement
[statements]
Flow chart:
e.g.
x=1
while (x <= 4):
print(x)
x=x+1
1st iteration: while(1<=4)
2nd iteration: while(2<=4)
3rd iteration:while(3<=4)
4th iteration: while(4<=4)
5th iteration: while(5<=4)
Output:
While Loop With Else:
e.g.
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
2. For Loop:
It is used to iterate over items of any sequence, such as a list or a string.
Each time, when the loop-body is executed is called iteration.
The range() function:
The range() function of Python generates a list which is a special sequence
type.
A sequence in Python is a succession of values bound together by a single
name.
range() Function Parameters:
1. start: Starting number of the sequence.
2. stop: Generate numbers up to, but not including this number (n-1).
3. step(Optional): Determines the increment between each numbers in the
sequence.
examples of range() function:
1. range(10) values generated as 0,1,2,3,4,5,6,7,8,9
2. range(5,10) values generated as 5,6,7,8,9
3. range(5,15,3) values generated as 5,8,11,14
4. range(10,1,-2) values generated as 10,8,6,4,2
Operators in and not in:
ex:
>>> 3 in [1,2,3,4,5]
True
>>> 5 not in [1,2,3,4]
True
Syntax:
for val in sequence:
statements
e.g. 1
for i in range(3,5):
print(i)
Output :
e.g. 2
for i in range(5,3,-1):
print(i)
Output:
4
For Loop With Else:
e.g.
for i in range(1, 4):
print(i)
else: # Executed because no break in for
print("No Break")
Output:
No Break
Nested For Loop:
A loop may contain another loop in its body. This form of loop is called nested
loop.
But in a nested loop, the inner loop must terminate before the outer loop.
e.g. 1
for i in range(1,6): Outer loop
for j in range(1,i):
print(“*”,end=’’) inner loop statement
print() Outer loop statement
Output:
* *
* * *
* * * *
* * * * *
e.g. 2
for i in range(1,3):
for j in range(1,11):
k=i*j
print (k, end=' ')
print()
Output:
1 2 3 4 5 6 7 8 9 10
2 4 6 8 10 12 14 16 18 20