A program’s control flow is the order in which the program’s code executes.
Python has three types of control structures:
Sequential - default mode
Selection/ Condition - used for decisions and branching
Iteration /Repetition - used for looping, i.e., repeating a piece of code multiple times.
Selection/ Condition
In Python, 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:
One Way (Simple if)
Two Way (if-else)
Multi Way (if-elif-else)
Relational Operator
Logical Operator
One Way
(Syntax)
if(condition):
Statements
Example (1)
mark=int(input("Enter Mark"))
if(mark>=40):
print('Pass the exam.')
Two Way
(Syntax)
if(condition):
Statements(true)
else:
Statements(false)
Example (2)
mark=int(input("Enter Mark"))
if(mark>=40):
print('Pass the exam.')
else:
print('Fail the exam.')
Multi Way
(Syntax)
if(condition1):
Statements1
elif(condition2):
Statements2
elif(condition3):
Statements3
else:
Statements
Example (3)
mark=
Nested If
if(condition):
if(condition):
Statement
else:
Statement
elif(condition):
if(condition):
Statement
else:
Statement
elif(condition):
if(condition):
Statement
else:
Statement
else:
if(condition):
Statement
else:
Statement
Example (4)
a=int(input("Enter First Number"))
b=int(input("Enter Second Number"))
c=int(input("Enter Third Number"))
if(a>b and a>c):
if(b>c):
print(a,b,c)
else:
print(a,c,b)
elif(b>a and b>c):
if(a>c):
print(b,a,c)
else:
print(b,c,a)
else:
if(a>b):
print(c,a,b)
else:
print(c,b,a)
Exercise
Write a Python program that determines whether a given number (accepted from the user) is even or odd,
and prints an appropriate message to the user.