Decisions inside decisions: nesting ifs with clean indentation, flattening to elif ladders, and reading other people's branching code.
01
Learning objectives
Write a nested if with correct indentation for both levels.
Trace nested branches with a decision table (outer verdict × inner verdict).
Flatten a nested if into an elif ladder using and, and choose the clearer form.
Explain when nesting is genuinely better than a ladder (dependent checks).
Read a multi-branch program and state its output for given inputs.
02
Lesson notes
A nested condition is simply an if (or if/else) written inside the block of another if. The outer question is asked first; only if it passes does the inner question even get asked. Nesting expresses dependent logic: 'IF the student is enrolled, THEN check whether attendance is sufficient — but if they are not enrolled, attendance is irrelevant.' The indentation grows one level per nesting, which is why Python's four-space rule earns its keep: the visual shape of the code IS the shape of the logic.
Tracing nested branches calls for a slightly bigger tool than the one-row table: a decision table. List the outer condition's outcomes as rows and the inner condition's outcomes as columns; each cell names the actions that run in that combination. For the enrolled/attendance example: (enrolled True, attendance True) → 'allowed'; (enrolled True, attendance False) → 'too many absences'; (enrolled False, ·) → 'not enrolled' regardless of the second column. Building the table before coding turns nested spaghetti into a checklist.
Nesting is often chosen when a ladder would do — and that is usually a mistake. if marks >= 60: { if marks >= 80: A else: B } else: Fail can be flattened to a ladder: if marks >= 80: A elif marks >= 60: B else: Fail. The ladder has one indent level, reads top to bottom, and scales to five bands without deepening. The rule of thumb: if the inner condition does not depend on passing the outer one (it would still make sense asked alone), flatten it with and into a ladder or a compound condition.
When IS nesting right? When the inner check is genuinely dependent or expensive. Validating a form field: only check 'is this a valid email?' after 'did the user type anything?' — asking the second on empty text is meaningless. Division safety: inside if n != 0: it is finally legal to compute total / n. Performance: skip a heavy database lookup when the cheap validation already failed. In each case the inner block exists because the outer condition held — that dependence is the signal to nest.
Reading nested code is a skill with its own technique: re-indent mentally, name each level aloud ('outer: enrolled?', 'inner: attendance?'), and mark where each block ENDS before starting the next. Python's consistent indentation makes this far easier than brace-matching in other languages — the block is literally the visible shape. This lesson closes the decision-making module: from here, every Labs challenge in the conditionals tier is one of the three shapes you now own — if/else, ladder, or a deliberate nest.
03
Worked code examples
Nesting for dependent checks — lab entry
The program is fed y, then 61. The attendance question is only asked when enrolled passed — it depends on it. A fed n would answer 'Not enrolled' without ever reading attendance.
enrolled = input() == "y"
if enrolled:
attendance = int(input())
if attendance >= 75:
print("Enter the lab")
else:
print("Attendance too low")
else:
print("Not enrolled")
Output
Attendance too low
Flattening a nest into a ladder
The inner question (>= 80) makes sense on its own, so the nest can flatten. Both versions print identically — but the ladder needs one indent level and scales to more bands painlessly.
marks = 84
# Nested version
if marks >= 60:
if marks >= 80:
print("Grade A")
else:
print("Grade B")
else:
print("Fail")
# Flattened — same behaviour, one level
if marks >= 80:
print("Grade A (ladder)")
elif marks >= 60:
print("Grade B (ladder)")
else:
print("Fail (ladder)")
Output
Grade A
Grade A (ladder)
A decision table becomes code — shipping fee
Fed 1500 and n, the decision table gives the (small, non-member) cell: 150. The table has four cells — (big, member) → 0, (big, non-member) → 0, (small, member) → 0, (small, non-member) → 150 — and the code covers each exactly once.
total = int(input())
member = input() == "y"
if total >= 2000:
fee = 0 # big orders: free always
else:
fee = 0 if member else 150 # small orders: members free
print("Delivery fee:", fee)
Output
Delivery fee: 150
04
Key terminology
nested condition
An if/else written inside another if's block; the inner question is asked only after the outer passes.
decision table
A grid of every condition combination and the actions for each — the design tool for nested logic.
flatten
Rewriting a nest as an elif ladder (using and where needed) so the code stays one indent deep.
dependent check
A question that only makes sense after another passed — the correct reason to nest.
guard block
An outer if that protects the inner block from running in an unsafe or meaningless case.
05
Common misconceptions
Common mistake
Nested code is always more 'advanced' and therefore better.
Correct understanding
Nesting is for dependent checks only. A flat ladder is clearer, scales better, and is what examiners prefer for grade-band problems.
Common mistake
The inner if runs even when the outer condition is False.
Correct understanding
The inner block belongs to the outer block — if the outer fails, the inner question is never asked.
Common mistake
Each indent level may use any number of spaces as long as it looks neat.
Correct understanding
Lines in the SAME block must match exactly, and the convention is four spaces per level. Mixed depths raise IndentationError (or silently change the logic).
06
Check your understanding
1. In a nested if, when is the inner condition evaluated?
2. Which structure suits 'if marks >= 80 print A, elif >= 60 print B, else Fail'?
3. n = 0. What happens with:
if n != 0:
print(100 / n)
else:
print("safe")