Control Flow
Control Flow
• Control Flow
A program's control flow is the order in which the program's
code executes.
The control flow of a Python program is regulated
by conditional statements, loops, and function calls.
• Python has three types of control structures:
Sequential - default mode
Selection - used for decisions and branching
Repetition - used for looping, i.e., repeating a piece
of code multiple times.
Control Statements
• 1. Sequential
• Sequential statements are a set of statements whose execution
process happens in a sequence.
• The problem with sequential statements is that if the logic has
broken in any one of the lines, then the complete source code
execution will break.
Example
a=20
b=10
c=a-b
print("Subtraction is : ",c)
Control Statements
• 2. Selection/Decision control statements
• The selection statements are also known as Decision control
statements or branching statements.
• The selection statement allows a program to test several
conditions and execute instructions based on which condition is
true.
• Some Decision Control Statements are:
– Simple if
– if-else
– nested if
– if-elif-else
Control Statements
• Simple if:
– If statements are control flow statements that help us to run a particular
code, but only when a certain condition is met or satisfied. A simple
if only has one condition to check.
• Syntax
If condition :
true statements
Control Statements
• Simple if
• Example
n = 10
if n % 2 == 0:
print("n is an even number")
Control Statements
• if-else:
– The if-else statement evaluates the condition and will execute the body
of if , if the test condition is True, but if the condition is False, then the
body of else is executed.
• Syntax
If condition :
true statements
else:
false statements
Control Statements
• Example
n=5
if n % 2 == 0:
print("n is even")
else:
print("n is odd")
Control Statements
• Nested if:
• Nested if statements are an if statement inside another if
statement.
Control Statements
• Nested if:
• Nested if statements are an if statement inside another if
statement.
• Syntax
If condition1:
if condition2:
true statement for condition1,2
else:
false statement for condition2
else:
false statement for condition1
Control Statements
• Example
a=5
b = 10
c = 15
if a > b:
if a > c:
print("a value is big")
else:
print("c value is big")
elif b > c:
print("b value is big")
else:
print("c is big")
Control Statements
• if-elif-else:
• The if-elif-else statement is used to conditionally execute a
statement or a block of statements.
If condition1:
true statements
elif condition2:
true statement for condition2
else:
false statement for cnodition2
Control Statements
• Example
x = 15
y = 12
if x == y:
print("Both are Equal")
elif x > y:
print("x is greater than y")
else:
print("x is smaller than y")
Control Statements
• 3. Repetition
instructions.
– for loop
– while loop
Control Statements
for loop:
A for loop is used to iterate over a sequence that is either a list, tuple,
dictionary, or a set.
We can execute a set of statements once for each item in a list, tuple, or
dictionary.
Syntax:
Where , val is the variable that takes the value of the item inside the
sequence on each iteration.
Control Statements
for loop Operation:
Loop Operation:
the loop runs the body lines again and again, once for each element
in the collection.
For the first iteration, the variable is set to the first element, and the body
lines run.
Control Statements
for loop Example:
for i in range(10):
print(i)
#0123456789
Control Statements
for loop Example:
2. Loop Over All Index Numbers
For a linear collections (e.g. list, string), index numbers 0, 1, ... len-1 indentify
each element in the collection.
The following loop combines for/range/len to run a variable i through all the
index numbers in a string or list.
# string s, loop though all its chars by index number
s = 'Hello'
for i in range(len(s)):
# i.e. range(5)
print(i, s[i]) 0 H
1 e
2 l
3 l
4 0
Control Statements
for loop:
A for loop is used to iterate over a sequence that is either a list, tuple,
dictionary, or a set.
We can execute a set of statements once for each item in a list, tuple, or
dictionary.
lst = [1, 2, 3, 4, 5]
for i in range(len(lst)):
print(lst[i], end = " ")
for j in range(0,10):
print(j, end = " ")
Control Statements
while loop:
In Python, while loops are used to execute a block of statements repeatedly until a
given condition is satisfied.
Then, the expression is checked again and, if it is still true, the body is executed
again. This continues until the expression becomes false.
Syntax :
initialization
while test_expression:
Body of the loop
iteration
Control Statements
while loop:
In Python, while loops are used to execute a block of statements repeatedly until a
given condition is satisfied.
Then, the expression is checked again and, if it is still true, the body is executed
again. This continues until the expression becomes false.
m=5
i=0
while i < m:
print(i, end = " ")
i=i+1
print("End")
Control Statements
Syntax of break
break
Control Statements
print("The end")
Flow chart for break statements
Example
Control Statements
Syntax of continue
continue
Control Statements
print("The end")