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.

Nested conditionals are like a decision tree โ€” first you ask one question, and depending on the answer, you ask a follow-up question.

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

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
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 4

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