Start with the recap, study the fully worked examples, then use the practice problems to
check your understanding of Nested Conditionals.
This page combines explanation, solved examples, and follow-up practice so you can move
from recognition to confident problem-solving in CS Thinking.
Concept Recap
Conditional statements placed inside other conditional statements, creating multiple levels of decision-making. The inner condition is only evaluated when the outer condition is true, allowing programs to model complex, multi-step decisions.
Nested conditionals are like a decision tree โ first you ask one question, and depending on the answer, you ask a follow-up question.
Read the first worked example with the solution open so the structure is clear.
Try the practice problems before revealing each solution.
Use the related concepts and background knowledge badges if you feel stuck.
What to Focus On
Core idea:Nesting adds precision to decisions but increases complexity. More than 3 levels deep is usually a sign to restructure your logic.
Common stuck point:Deep nesting makes code hard to read. Consider using elif/else-if chains or combining conditions with AND/OR instead.
Sense of Study hint:When writing nested conditionals, draw a decision tree first to visualize the logic. If the nesting goes more than 2-3 levels deep, consider refactoring: combine conditions with AND/OR, use elif chains, or extract inner logic into separate functions.
Worked Examples
Example 1
easy
Trace the following code when age = 15 and hasTicket = TRUE: IF age >= 12 THEN: IF hasTicket THEN: OUTPUT 'Enter'. ELSE: OUTPUT 'Buy a ticket'. ELSE: OUTPUT 'Too young'.
Answer
Output: 'Enter'. Both conditions (age >= 12 AND hasTicket) are true.
First step
1
Step 1: Check outer condition: age >= 12? 15 >= 12 is TRUE, so we enter the outer IF block.
Full solution
2
Step 2: Check inner condition: hasTicket? TRUE, so we enter the inner IF block.
3
Step 3: OUTPUT 'Enter'. The nested IF is only reached after the outer condition passes โ this creates a two-level check.
Nested conditionals place one IF inside another, creating multi-level decision trees. The inner condition is only evaluated when the outer condition is true.
Example 2
medium
Rewrite this nested IF as a single condition using AND: IF temperature > 20 THEN: IF NOT raining THEN: OUTPUT 'Go outside'. Why might you prefer one style over the other?
Example 3
medium
A login system: must have valid user, then valid password, then account not locked. Sketch nested conditionals and identify each error message.
Example 4
medium
Refactor `if a: if b: if c: do()` using guard clauses for early exit.
Example 5
medium
Show how `if a and b: X` differs from `if a: if b: X` if evaluating b has a side effect (e.g., prints).
Example 6
hard
Rewrite the dangling-else trap: `if a: if b: X else: Y`. Which `if` does the `else` bind to, and how do braces fix ambiguity?
Example 7
hard
Trace: a=3, b=7, c=2. `if a<b: if b<c: print('asc') else: if a<c: print('mid') else: print('a<b,c<a')`. Output?
Example 8
challenge
Trace: x=8,y=8. `if x>0: if y>0: if x==y: print('eq pos') else: print('diff pos') else: print('y nonpos') else: print('x nonpos')`. Output and depth of taken path?
Practice Problems
Try these problems on your own first, then open the solution to compare your method.
Example 1
medium
Write nested conditionals for a cinema discount: Seniors (age >= 65) get 50% off. Children (age < 12) get 30% off. Everyone else pays full price, but students with a valid ID get 20% off. Trace for age=20, isStudent=TRUE.
Example 2
hard
The following deeply nested code is hard to read. Refactor it to be clearer: IF a > 0 THEN: IF b > 0 THEN: IF c > 0 THEN: OUTPUT 'All positive'. ELSE: OUTPUT 'c not positive'. ELSE: OUTPUT 'b not positive'. ELSE: OUTPUT 'a not positive'.
Example 3
easy
Trace: x = 5. `if x > 0: if x > 10: print('big') else: print('small')`. What prints?
A 5-level nested if has 2 boolean checks at each level. How many distinct combinations of input booleans does it cover?
Example 40
hard
Code duplicates `log()` in both inner branches: `if a: if b: log(); X else: log(); Y`. How can you eliminate the duplication?
Example 41
challenge
For inputs with n independent booleans, a fully nested if-else tree covers 2n leaves. Why does this argue against using nesting for large n, and what's a typical alternative?