Practice Nested Conditionals in CS Thinking

Use these practice problems to test your method after reviewing the concept explanation and worked examples.

Quick 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.

Showing a random 20 of 50 problems.

Example 1

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 2

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 3

medium
Trace: u_logged_in=True, u_admin=False. `if u_logged_in: if u_admin: print('panel') else: print('home') else: print('login')`. Output?

Example 4

medium
How many distinct outputs can `if a: if b: P() else: Q() else: R()` produce, and what are they?

Example 5

easy
In `if A: if B: X`, X runs only when which boolean expression is true?

Example 6

medium
Trace: x=-2. `if x>=0: if x==0: print('zero') else: print('pos') else: if x>=-5: print('small neg') else: print('big neg')`. Output?

Example 7

medium
Trace: x=4, y=4. `if x==y: if x>0: print('equal pos') else: print('equal nonpos') else: print('diff')`. Output?

Example 8

challenge
Trace: n=6. `if n>0: if n%2==0: if n>4: print('A') else: print('B') else: print('C') else: print('D')`. Output, and how deep is the taken path?

Example 9

medium
Trace: a = 7, b = 2. `if a > 5: if b > 5: print('both') else: print('a only') else: print('neither')`. Output?

Example 10

medium
Given `if A: if B: X else: Y else: Z`, with A=True, B=False, which runs and is Z evaluated?

Example 11

medium
Refactor `if a: if b: if c: do()` using guard clauses for early exit.

Example 12

easy
Trace: x = -3. `if x >= 0: print('non-neg') else: if x < -5: print('very neg') else: print('neg')`. Output?

Example 13

medium
Trace: t=5 (hour). `if t<12: if t<6: print('night') else: print('morning') else: print('afternoon')`. Output?

Example 14

medium
Trace: n=15. `if n%3==0: if n%5==0: print('FizzBuzz') else: print('Fizz') else: if n%5==0: print('Buzz') else: print(n)`. Output?

Example 15

easy
Trace: n=4. `if n%2==0: if n>10: print('big even') else: print('small even')`. Output?

Example 16

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 17

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'.

Example 18

easy
Trace: x=10. `if x>0: if x>5: print('big') else: print('small')`. Output?

Example 19

medium
Trace: a=2,b=8,c=5. `if a<b: if b<c: print('asc') else: if a<c: print('a<c<b') else: print('other')`. Output?

Example 20

easy
Rewrite `if rain: if cold: stay_in()` as one if-statement using AND.