After completing this chapter, you will be able to:
- Understand the
if...elif...elsestatement. - Explain why the
if...elif...elsestatement is used. - Write programs involving multiple conditions.
- Understand the execution flow of multi-way decision making.
- Apply the
if...elif...elsestatement in real-world applications.
Sometimes a program needs to evaluate more than two conditions.
For example:
- Student Grade Calculation
- Employee Salary Classification
- Traffic Signal System
- Menu-Driven Programs
- Online Shopping Discounts
In such situations, using only if or if...else becomes difficult.
Python provides the if...elif...else statement to handle multiple conditions efficiently.
The if...elif...else statement is a multi-way decision-making statement that evaluates multiple conditions one by one.
- If a condition is True, its corresponding block executes.
- The remaining conditions are skipped.
- If none of the conditions are
True, theelseblock executes.
It is used whenever a program has multiple possible outcomes.
- Grade Calculation
- ATM Transaction Menu
- Employee Bonus Calculation
- Income Tax Calculation
- Electricity Bill Calculation
- Weather Prediction
if condition1:
statements
elif condition2:
statements
elif condition3:
statements
else:
statements Condition 1
/ \
True False
| |
Execute Block Condition 2
|
True False
| |
Execute Block Condition 3
|
True False
| |
Execute Block else Block
|
Next Statement
- Python checks the first condition.
- If it is
True, that block executes. - Otherwise, Python checks the next
elifcondition. - This process continues until a condition becomes
True. - If no condition is
True, theelseblock executes. - Only one block executes.
Write a Python program to display the student's grade based on marks.
# Student marks
marks = 82
# Grade calculation
if marks >= 90:
print("Grade A+")
elif marks >= 75:
print("Grade A")
elif marks >= 60:
print("Grade B")
elif marks >= 35:
print("Grade C")
else:
print("Fail")Grade A
START
Create variable marks
Assign marks
If marks >= 90
Display Grade A+
Else if marks >= 75
Display Grade A
Else if marks >= 60
Display Grade B
Else if marks >= 35
Display Grade C
Else
Display Fail
STOP
Write a Python program to find the largest among three numbers.
# Store three numbers
a = 25
b = 40
c = 18
# Find the largest
if a >= b and a >= c:
print("A is Largest")
elif b >= a and b >= c:
print("B is Largest")
else:
print("C is Largest")B is Largest
Write a Python program to display an action based on the traffic signal color.
# Traffic signal
signal = "Green"
if signal == "Red":
print("Stop")
elif signal == "Yellow":
print("Get Ready")
elif signal == "Green":
print("Go")
else:
print("Invalid Signal")Go
The if...elif...else statement is widely used in:
- Student Grade Calculation
- Salary Processing
- Income Tax Calculation
- Banking Applications
- Online Shopping Discounts
- Menu-Driven Programs
- Railway Reservation Systems
- Handles multiple conditions efficiently.
- Improves readability.
- Avoids deeply nested
ifstatements. - Makes programs easier to maintain.
- Executes only one matching block.
| Statement | Purpose |
|---|---|
if |
Executes a block only when the condition is True. |
if...else |
Handles two possible outcomes. |
if...elif...else |
Handles multiple conditions. |
❌ Incorrect
marks = 85
if marks >= 35:
print("Pass")
elif marks >= 75:
print("Grade A")Since marks >= 35 is already True, the elif block will never execute.
✅ Correct
marks = 85
if marks >= 90:
print("Grade A+")
elif marks >= 75:
print("Grade A")
elif marks >= 35:
print("Pass")Always write conditions from highest priority to lowest priority.
It is a decision-making statement used to evaluate multiple conditions and execute only one matching block of code.
The elif keyword allows us to check another condition when the previous condition is False.
There is no fixed limit. A program can have any number of elif blocks.
No. The else block is optional.
Only one block executes.
- Used for multiple conditions.
- Conditions are checked from top to bottom.
- The first matching condition executes.
- Remaining conditions are skipped.
elseexecutes only when no condition isTrue.
In this chapter, you learned:
- What the
if...elif...elsestatement is. - Why it is used.
- Syntax and execution flow.
- Practical programs.
- Flow diagram.
- Real-world applications.
- Advantages.
- Common mistakes.
- Interview questions.