Module 1.2
Module 1.2
Control
Program Control Statement or Control Structures
• Normally, the program execution starts with the first statement of
program and ends with the last statement of program.
• With the help of control statement or control structure, we can change
the execution order.
• Control structures specify the statement to be executed and the order of
execution of statements.
• There are three kinds of control structures.
1. Sequential Statements
2. Selection or Branching or Conditional Statements
3. Iteration or Looping Statements
1.Sequential Statements : instructions are executed in linear order.
2. Selection or Branching or Conditional Statements : it asks a true / false
question and then selects the next instruction based on the answer.
3. Iteration or Looping Statements : it repeats the execution of a block of
instructions.
Control Structures in Flowchart
if (test expression):
statement(s)
Python if Statement Flowchart
if (test expression):
Body of if
else: Python if…else Flowchart
Body of else
a,b,c = 1,2,"john"
Python if...elif...else Flowchart of if...elif...else
if (test expression):
Body of if
elif (test expression):
Body of elif
elif (test expression):
Body of elif
else:
Body of else
EXAMPLE 1 EXAMPLE 2
Flow Diagram
1. while loop
2. for loop
3. for…...else
4. while……else
• While Loop
Syntax
count = 0
while (expression): while (count < 9):
print ('The count is:', count)
statement(s) count = count + 1
print ("Good bye!“)
Nested while loop
while expression:
while expression: 1,5
i,j = 1,5 1,6
statement(s) #j = 5 1,7
while i < 6: 2,5
statement(s) j=5 2,6
while j < 8: 2,7
print(i, ",", j) 3,5
j=j+1
i=1 Output: i=i+1
3,6
3,7
j=5 1 , 5 4,5
4,6
1,6 4,7
while i < 4:
1,7 5,5
5,6
while j < 8: 5,7
print(i, ",", j)
j=j+1
i=i+1
Python – while loop with else block
• We can have a ‘else’ block associated with while loop.
• The ‘else’ block is optional.
• It executes only after the loop finished execution.
num = 10 Output:
while (num > 6): 10
print(num) 9
num = num-1 8
7
else:
loop is finished
print("loop is finished")
For Loop
for iterating_var in sequence:
statements
Flow Diagram
for let in 'Python': # First Example
Output
print ('Current Letter :', let)
Current Letter : P
fruits = ['banana', 'apple', 'mango']
Current Letter : y
for fru in fruits: # Second Example Current Letter : t
print ('Current fruit :', fru) Current Letter : h
Current Letter : o
print ("Good bye!“) Current Letter : n
Current fruit : banana
Current fruit : apple
Current fruit : mango
Good bye!
numbers = [1, 2, 4, 6, 11, 20]