Chapter - 4
Control Structures
Conditional and looping constructs
Introduction
Tools that help in better understanding of the problem
statement and decisions to be made accordingly are:
1. Flow charts
2. Pseudo code
3. Decision tree
Flow charts
It is the pictorial representation of the sequence of steps and
decisions needed to perform a task.
Flow chart depicts the “flow” of a program.
Commonly used symbols in flow chart
Symbol Name
Start/Stop
Process
Input/Output
Decision
Connection (Flow Line)
Example of Flow chart
Example of Flow chart
Program Control Flow
Program control flow is the order in which individual
statements or instructions are executed.
The control flow of a program in any programming
language can be broadly classified into three
categories.
1. Sequence
2. Selection/Decision
3. Iteration/Loop
Program Control Flow
1. Sequence
The statements in the programs are executed in
an order one line at a time from the top to the bottom
of your program.
2. Selection/Decision
The set of statements are executed based on the
condition and then change the course of program.
3. Iteration/Loop
Loops are used to repeatedly execute the same
statement(s) in a program.
1. Sequence
2. Selection
(Decision making)
There are four types of decision-making statements in
python:
a. if statement
b. if-else statement
c. if-elif-else statement
d. Nested if-else statement
2. Selection (Decision making)
a. if statement
if the condition is True, then the statement written after, is
executed.
Syntax:
if condition:
statement(s)
e.g.
if x > 0:
print (“x is positive”)
2. Selection (Decision making)
b. if-else statement
if the condition is True, then the statement written after, if is executed. If
False, statement(s) written after else is executed
Syntax:
if condition:
statement(s)
else:
statement(s)
e.g.
if age >= 18:
print (“Can Vote”)
else:
print (“Can’t Vote”)
2. Selection (Decision making)
c. if-elif-else statement
It is used for multiple test condition.
In the chained conditions, each condition is checked in order if
previous is False then next is checked, and so on. If one of them is
True then corresponding block of statement(s) are executed and the
statement ends.
If none is true, then else block gets executed if provided. If more
than one condition is true, then only the first true option block gets
executed.
2. Selection (Decision making)
Syntax:
if condition:
statement(s)
elif condition:
statement(s)
elif condition:
statement(s)
else:
statement(s)
e.g.
if per>=90:
print (“A”)
elif per>=70 and per<90:
print (“B”)
elif per>=50 and per<70:
print (“C”)
else
2. Selection (Decision making)
d. Nested if-else statement
It is possible to have a condition within another condition. Such conditions
are known as Nested Condition.
Example:
num=int(input("Enter number"))
if (num>=0):
if (num==0):
print("Zero")
else:
print("Positive number")
else:
print("Negative number")
3. Iteration
Loops are used to repeatedly execute the same code
in a program.
Python provides two types of looping constructs:
a) while loop
b) for loop
3. Iteration
a) while loop
The statement with keyword while followed by boolean
condition followed by colon (:).
What follows next is block of statement(s).
The statement(s) in BLOCK 1 keeps on executing till
condition in while remains True;
once the condition becomes False and if the else clause is
written in while, then else will get executed.
While loop may not execute even once, if the condition
evaluates to false, initially, as the condition is tested before
entering the loop.
3. Iteration
Syntax:
while condition: # condition is Boolean expression
returning True or False
STATEMENTs
[else: # optional part of while
STATEMENTs ]
Example
a loop to print nos. from 1 to 10
i=1
while (i <=10):
print (i, end=‘ ‘)
i = i+1
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
Nested while loop
Block of statement belonging to while can have
another while statement, i.e. a while can
contain another while.
e.g.
i=1
while(i<=3):
j=1
while(j<=i):
print(j, end=" ") # inner while loop
j=j+1 Output:
print("\n") 1
i=i+1 1 2
1 2 3
3. Iteration
b) for loop
The for statement is used to repeat itself over a range of
values or a sequence, such as a list, string or tuple.
Its Syntax is:
for <control_variable> in <sequence/items in range>:
STATEMENT BLOCK 1
[else: # optional block
STATEMENT BLOCK 2]
3. Iteration
for i in range(1, 11,1):
print (i, end=' ')
Output: 12345678910
The statement introduces a function range ( ),
its syntax is
range(start, stop, [step]) # step is optional
range( ) generates a list of values starting from start till stop-1.
step if given is added to the value generated, to get next value in
the list.
3. Iteration
i is the variable, which keeps on getting a value generated by
range ( ) function, and the block of statement (s) are worked on
for each value of i. As the last value is assigned to i, the loop
block is executed last time and control is returned to next
statement.
If else is specified in for statement, then next statement
executed will be else. Now we can easily understand the result
of for statement. range( ) generates a list from 1, 2, 3, 4, 10 as
the step mentioned is 1, I keeps on getting a value at a time,
which is then printed on screen.
Apart from range( ), i (loop control variable) can take
values from string, list, dictionary, etc.
Example 1:
for letter in "Python":
print("Current letter", letter)
else:
print("End of loop")
Example 2:
num=[10,20,30,40,50]
for i in num:
print(i)
Nested for
for num in range(6):
for i in range(num):
print (num, end=" ") print("\
n")
Output:
1
1 2
1 2 3
1 2 3 4 5
1 2 3 4 5
Break Statement
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
Output:
for letter in "Python": >>>
if (letter == "h"): P
break y
print(letter) t
Continue Statement
This statement is used to tell Python to skip the rest of the
statements of the current loop block and to move to next
iteration, of the loop.
Continue will return back the control to the beginning of the
loop. This can also be used with both while and for
statement.
Output:
Example
>>>
P
for letter in "Python":
y
if (letter == "h"):
t
continue
o
print(letter)
n