Q.What is the difference between else and elif construct of if statement?
elif is a conditional branch ("else if") that tests a NEW condition when the previous ones fail and can be chained many times; else is the single, condition-less default branch that runs only when every test above it is False.
The idea. An if statement chooses between alternative paths of execution. When there are more than two possibilities we need further tests — that is what elif provides — while else exists to catch everything left over.
| Aspect | elif | else |
|---|---|---|
| Condition | Must have its own condition | Takes no condition at all |
| When it runs | When all previous if/elif conditions were False AND its own condition is True | When every preceding condition was False |
| How many allowed | Any number, chained between if and else | At most one, always last |
| Purpose | Test further alternatives | Default/fallback action |
Example showing both in one ladder:
marks = int(input("Enter marks: "))
if marks >= 90:
print("Grade A")
elif marks >= 75:
print("Grade B")
elif marks >= 60:
print("Grade C")
else:
print("Grade D")
Sample runs:
Enter marks: 80
Grade B
Enter marks: 40
Grade D
For 80: the first test (80 >= 90) fails, the elif test (80 >= 75) succeeds, so "Grade B" prints and the rest of the ladder is skipped. For 40: every condition fails, so control falls into else. Exactly one branch of an if–elif–else ladder executes, always the first whose condition is True.
Writing else marks >= 60: is a syntax error — else can never carry a condition. If you need a condition, the construct you want is elif.
An if–elif ladder stops at the FIRST true condition, so order your tests from most restrictive to least — that is what makes range-style ladders (>= 90, >= 75, >= 60, else) work without writing both bounds of each range.
elif introduces an additional test with its own condition (checked only if all earlier conditions failed; many elifs may be chained), whereas else is the lone condition-less default branch executed only when every condition in the ladder is False.
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.