Page 115 - 2611_SmartGPT Pro V(5.0) C-7
P. 115
IF..ELIF….ELSE STATEMENT
Python provides the elif(else-if) statement to check for multiple conditions and execute the
code block within if any of the conditions are evaluated to be true.
The elif statement is optional like the else Start
statement but multiple elif statements can be
used in a block of code following an if statement. True
if condition 1 block 1
Syntax of the if..elif….else statement:
if condition1: False
Statement block 1 True
if condition 2 block 2
elif condition2:
Statement block 2
False
elif condition3:
True
Statement block 3 if condition 3 block 3
..
False
.. block
else:
Stop
Statement block n
The interpreter starts at the top, testing condition1. If it is true, statement block 1 is executed and
the rest of the constructs are ignored. If it is false, the control moves down and checks condition2,
if it is true, statement block 2 will be executed and if it is false, it moves next to condition3 and so
on. The process repeats till a true condition is found. In case none of the conditions evaluate to
true, the block after the else is executed.
Program 6. To take marks of a student as input and display a message based on the marks:
If the marks are equal or more than 90, show “Excellent”
If equal or more than 50, show “Fairly Good”
Otherwise, show “Need more effort”
Program6.py
File Edit Format Run Options Window Help
marks =int(input("Enter your marks: "))
if marks>=90:
print("Excellent")
elif marks>=70:
print("Very Good")
elif marks>=50:
print("Fairly Good")
else:
print("Need more effort")
Flow of Control in Python 113

