Q.What is the difference between else and elif construct of if statement?
You're viewing a preview — the full solution, concept, methods & PYQ mapping are locked.
Start your 14-day free trial to unlock the full solution →else is a catch-all for when no if condition is true, while elif (short for "else if") lets you check additional conditions only when the previous ones were false — it chains multiple conditions without nesting.
The Core Idea: Conditional Control Flow
Every programming language needs a way to make decisions. The if statement is the most basic tool for this — it says "if this condition is true, do this thing." But real-world logic is rarely that simple. You often need to handle multiple possibilities: "If it's raining, take an umbrella; otherwise, if it's sunny, wear sunglasses; otherwise, just go out." This is where else and elif come in, and they serve fundamentally different purposes.
Think of else as the default path — the "everything else" bucket. It runs when none of the preceding conditions were true. It has no condition of its own; it simply says "if nothing above matched, do this."
elif, on the other hand, is a chained condition. It's short for "else if" — it lets you check another condition, but only if all previous conditions were false. This is crucial: elif prevents unnecessary checks. If the first if is true, Python skips the entire elif/else block entirely.
Common Mistake
Beginners often write multiple if statements instead of using elif. This is wrong because each if is independent — multiple conditions could be true, and all matching blocks would run. With elif, only the first true condition's block executes.
The Structural Difference
# Using elif — only ONE block runs
marks = 85
if marks >= 90:
grade = "A"
elif marks >= 75:
grade = "B" # This runs
elif marks >= 50:
grade = "C"
else:
grade = "D"
print(grade) # Output: B
# Using multiple ifs — WRONG for this logic
marks = 85
if marks >= 90:
grade = "A" # False, skipped
if marks >= 75:
grade = "B" # True, runs
if marks >= 50:
grade = "C" # ALSO True, runs — overwrites grade!
else:
grade = "D" # False, skipped
print(grade) # Output: C (wrong!)
The second example shows the classic pitfall: because each if is independent, marks >= 75 and marks >= 50 are both true, so both blocks execute. The second one overwrites the first, giving the wrong grade.
When to Use Each
Use else when:
- You have exactly two paths: one for a condition being true, one for it being false.
- You want a default action when nothing else matches.
Use elif when:
- You have three or more mutually exclusive paths.
- You want to check conditions in order, stopping at the first match.
Shortcut for Remembering
else = "otherwise" (no condition). elif = "else if" (has its own condition). If you see a condition after the word, it's elif. If there's no condition, it's else.
The Syntax Rules
if condition1:
# block A
elif condition2: # optional, can have many
# block B
elif condition3: # optional, can have many
# block C
else: # optional, must come last
# block D
Key rules:
elsemust come last — after allelifclauses.elifcan appear any number of times (including zero).- Both
elifandelseare optional — you can have just anif. …
Unlock everything free for 14 days
- Full step-by-step solutions
- Concept-first explanations
- Methods, shortcuts & mistakes
- PYQ mapping + timed mock tests
Full access for 14 days. No credit card required.