Using else to Handle the Default Case
An if-elif-else chain is one decision structure. It is different from writing several independent if statements.
One Decision, Several Outcomes
A decision does not always have only two possible outcomes. Sometimes a program must choose among several results. Python uses elif, short for else if, to connect multiple conditions in one decision structure. An elif chain checks its conditions in order and selects the first branch whose condition is true.
An if-elif-else chain is one decision structure. It is different from writing several independent if statements.
Following the Chain
Python evaluates a chained conditional from top to bottom. It checks the if condition first. If that condition is false, Python checks the next elif condition. This continues until a true condition is found. As soon as one condition is true, its branch runs and the rest of the chain is skipped.
The First True Branch
Exactly one branch executes in an if-elif-else chain. This remains true even when more than one condition would be true. Python does not continue checking after it finds the first true condition. The later conditions are skipped, so the chain produces one selected outcome rather than several.
A Complete Chain
x = 15
if x < 10:
print("less than 10")
elif x < 20:
print("between 10 and 20")
else:
print("20 or greater")- 1Python begins with the if condition.
- 2Because 15 is not less than 10, Python moves to the elif condition.
- 3Because 15 is less than 20, Python prints the second message.
- 4The chain ends after that branch; Python does not evaluate the else branch.
- The if condition x < 10 is false.
- Python checks the elif condition x < 20.
- The elif condition is true, so its branch executes.
- The else branch is skipped because a previous condition was true.
between 10 and 20Tracing a value through three outcomes
Determine which message is printed when x has the value 15.
- 1Check if
The condition x < 10 is false, so its branch does not run.
- 2Check elif
The condition x < 20 is true, so the elif branch runs.
- 3Skip else
The else branch is not considered because a previous condition was true.
The program prints between 10 and 20.
Ordering the Conditions
Because conditions are checked from top to bottom, their order affects the result. Put a more specific condition before a more general condition. If a general condition appears first and is true, Python stops there and never reaches a later condition that might describe the case more precisely.
if x < 20:
print("less than 20")
elif x < 10:
print("less than 10")The general condition x < 20 appears first, so a value less than 10 matches it before the specific condition is reached.
if x < 10:
print("less than 10")
elif x < 20:
print("less than 20")The specific condition x < 10 appears first, so values less than 10 can receive the more specific result.
The Default Branch
The else clause represents the remaining case. It has no condition of its own. Python reaches it only when the if condition and every elif condition before it are false. If an else clause is included, one branch of the chain will execute. Without else, it is possible for no branch to run.
Use elif for a specific additional condition. Use else when the program should handle every case that did not match an earlier condition. Keep else at the end of the chain.
Mistakes Beginners Make
Common mistake Writing several independent if statements when only one outcome should be selected.
What it looks likeif x < 20: ... if x < 30: ... if x < 40: ...Why it failsA value such as 15 satisfies all three conditions, so all three branches can execute.
Do this insteadUse an if-elif chain when the branches represent one choice and only one branch should run.
Common mistake Putting a general condition before a more specific condition.
What it looks likeif x < 20: ... elif x < 10: ...Why it failsA value less than 10 also satisfies x < 20, so the chain stops before the specific condition is checked.
Do this insteadPlace the more specific condition first.
Common mistake Expecting else to test a condition.
What it looks likeelse x == 20:Why it failsElse has no condition. It handles the remaining cases after all earlier conditions are false.
Do this insteadUse elif when another specific condition must be tested, and reserve else for the final default case.
Common mistake Placing else before the end of the chain.
What it looks likeif condition_a: ... else: ... elif condition_b: ...Why it failsAn else clause must come at the end of the if-elif-else structure.
Do this insteadPut every elif before the final else.
Practice the Trace
Trace the chain from top to bottom. Which message is printed when x is 25? How many conditions are checked before a branch runs?
if x < 10:
print("less than 10")
elif x < 20:
print("between 10 and 20")
elif x < 30:
print("between 20 and 30")
else:
print("30 or greater")Hints
- Check the if condition first.
- Continue to the next elif only when the earlier condition is false.
- Stop as soon as you find the first true condition.
What do you think happens?
What happens when choice is a value other than a, b, or c in a chain with three matching if or elif conditions and no else clause?
Reveal answer
None of the branches executes.
Each condition is false for that value, and without an else clause there is no remaining branch to run.
Working Rules
- 1Use elif to connect multiple possible outcomes in one decision structure. Python checks the conditions from top to bottom and executes only the first true branch. Put more specific conditions before general ones so the intended case is reached first. Place else at the end when every unmatched case should receive a default action. If there is no else and every condition is false, no branch runs.
Key Takeaways
- An elif chain handles more than two possible outcomes in one decision structure.
- Conditions are evaluated from top to bottom.
- Only the first true branch executes, even if later conditions are also true.
- More specific conditions should come before general conditions.
- An optional else clause must come last and handles the remaining cases.