if–elif–else Statement
The if elif else statement is a conditional statement used to check multiple conditions and execute one matching block of code.
The term elif means “else if”, and it helps to check conditions one by one.
When the program runs, it executes the first True condition and skips the remaining ones.
What is If Elif Else Statement?
The if elif else statement is used to check multiple conditions one after another.
Python executes the block whose condition becomes True.
If none of the conditions become True, the else block executes.
Meaning of elif
The word:
elif
means:
Else If
It is used to add extra conditions after the if statement.
If Elif Else Statement in Python
The if elif else statement is one of the most powerful decision-making statements in Python programming.
It is used when a program has more than two choices or conditions.
In real life, many situations require checking multiple conditions.
For example:
- Marks above 90 → Excellent
- Marks above 75 → Very Good
- Marks above 35 → Pass
- Otherwise → Fail
Python provides the if elif else statement to handle these kinds of multiple conditions efficiently.
Why If Elif Else Statements are Important
If elif else statements are important because they help programs:
- make multiple decisions
- handle complex conditions
- improve program logic
- create intelligent applications
- build real-world systems
Without if elif else statements, checking multiple conditions would become difficult and repetitive.
Explanation
- The program starts with if
- If the condition is False, it checks the elif condition
- It keeps checking conditions one by one
- Stops when it finds a True condition
- If none are True, the else block runs
- In the end, only one block executes
Real-Time Examples of If Elif Else
| Condition | Output |
|---|---|
| Marks >= 90 | Excellent |
| Marks >= 75 | Very Good |
| Marks >= 35 | Pass |
| Otherwise | Fail |
Explanation of the If-Elif-Else Flowchart in Python
The given diagram explains how the if-elif-else statement works in Python programming.
It shows the flow of execution when a program checks multiple conditions one by one.
This flowchart helps beginners understand:
- how Python checks conditions
- which block gets executed
- how decision making works in Python
If the Condition is True
If the condition becomes True:
- Python executes the if code block
- Remaining
elifandelseblocks are skipped
If the Condition is False
If the condition becomes False:
- Python does not execute the if block
- It moves to the next condition (
elif)
If elif Condition is False
If the elif condition is also False:
- Python moves to the
elseblock
If elif Condition is True
If the elif condition becomes True:
- Python executes the elif code block
- Remaining conditions are skipped
End of the If-Elif-Else Statement
After executing one block:
- Python exits the conditional structure
- Program continues with the next statements
Syntax
if condition1:
statements
elif condition2:
statements
elif condition3:
statements
else:
statements
Explanation of Syntax
| Part | Purpose |
|---|---|
if |
Checks the first condition |
elif |
Checks additional conditions |
else |
Executes when all conditions are False |
: |
Colon symbol required |
| Indentation | Defines the block of code |
Example 1
marks = 75
if marks >= 90:
print("A Grade")
elif marks >= 75:
print("B Grade")
elif marks >= 50:
print("C Grade")
else:
print("Fail")
Output:
B Grade
Example 2
time = 14
if time < 12:
print("Morning")
elif time < 18:
print("Afternoon")
else:
print("Evening")
Output:
Afternoon
How If Elif Else Works
The flow of execution is:
- Python first checks the
ifcondition - If True → executes that block
- If False → checks the next
elif - Continues checking until a True condition is found
- If all conditions are False → executes the
elseblock
Common Beginner Mistakes
| Mistake | Problem |
|---|---|
Forgetting colon : |
Syntax error |
| Wrong indentation | Indentation error |
Using = instead of == |
Logical error |
| Incorrect condition order | Wrong output |
➤ Summary
-
The if elif else statement in Python is used when a program needs to check multiple conditions.
Important points:
ifchecks the first conditionelifchecks additional conditionselseexecutes when all conditions are False- only one block executes
The if elif else statement is widely used in real-world applications such as grading systems, banking software, gaming applications, age categorization systems, and online platforms.
