If, Elif, Else, and Decision Trees
Key Takeaways
- An if statement starts a decision tree; elif branches are tested in order only if all earlier conditions were false.
- Only the first true branch in an if/elif/else chain executes, even if later conditions would also be true.
- An else clause has no condition and runs only when no earlier branch in the same chain ran.
- Indentation decides which statements belong to a branch, so visually similar snippets can behave differently.
- PCEP branch questions are easiest when traced as condition-result-action rows instead of guessed from the final output.
The shape of a decision tree
An if statement asks one question. If the condition is truthy, Python runs the indented block under that if. If the condition is falsey, Python skips that block and looks for an attached elif or else.
score = 82
if score >= 90:
grade = 'A'
elif score >= 80:
grade = 'B'
elif score >= 70:
grade = 'C'
else:
grade = 'review'
print(grade)
This prints B. Notice that score >= 70 is also true, but Python never reaches that branch because the earlier elif score >= 80 already succeeded. In one chain, exactly one branch can run: the first true branch, or the else branch, or none if there is no else.
If versus elif
Independent if statements are different from an if/elif/else chain. Each independent if is checked separately.
x = 10
if x > 0:
print('positive')
if x > 5:
print('large')
else:
print('small')
This prints positive and large. The else belongs only to the second if, not to the first one. The exam trap is reading this as one combined chain when it is really two decisions.
Indentation is structure
Python uses indentation to decide block membership. A line with the same indentation as if is outside the branch. A line indented under if runs only if that branch runs.
ready = False
if ready:
print('start')
print('done')
This prints only done. The second print is not inside the if block.
Trace-table method
Use this table for branch snippets:
| Step | Condition | Result | Action |
|---|---|---|---|
| 1 | score >= 90 | False | skip A branch |
| 2 | score >= 80 | True | run B branch, stop chain |
| 3 | later elif | not checked | no action |
This method prevents two common mistakes: checking branches after the first true branch, and attaching an else to the wrong if.
Ordering conditions
When ranges are involved, branch order matters. Broad tests should not appear before narrow tests unless that is intentional.
points = 95
if points >= 60:
label = 'pass'
elif points >= 90:
label = 'honor'
The label becomes pass, not honor, because points >= 60 is tested first. If the intended outcome is honor, the >= 90 test must come first.
Nested decisions
Nested if statements create a second decision inside a branch. Trace the outer condition first. Only if that branch runs should you evaluate the inner condition. This matters when the inner branch uses a variable that was assigned inside the outer branch, or when an else is indented under the inner if. On PCEP, match each else with the nearest compatible if at the same indentation level.
Exam traps
Watch for missing colons after if, elif, and else; using = instead of ==; lowercase else if instead of Python's elif; and an else that looks visually connected to one branch but is actually connected by indentation to another. Also remember that a condition can be any expression with a truth value. if name: means the branch runs when name is not empty.
What is printed by this code? x = 12 if x > 5: print('A') elif x > 10: print('B') else: print('C')
What is printed by this code? x = 3 if x > 0: print('P') if x > 5: print('L') else: print('S')
Which statement about an else clause is correct?