AP CSP Topic 3.7 Coding Practice - Nested Conditionals

Big Idea 3: Algorithms and Programming · Topic 3.7 · Coding Practice

Nested Conditionals — Code It Yourself

Write real code and check it against the expected output. Pick Python or JavaScript; the AP pseudocode reference is there when you need it.

You warmed these up on paper in Trace the Gate and Design the Gate. Now build and run them, hardest last. Every problem is the same ride-gate idea — a decision inside a decision — but the nest gets deeper as you go, ending with one you write from scratch. Predict the output first, then run it and let the computer check you. Remember the rule: the inner condition is tested only when the outer branch is taken.

Problem 1 of 4

Ride gate. The rider is 50 inches tall and age 15. Write a nested conditional: if height is at least 48, then if age is at least 12 print Enjoy the ride, otherwise print Ride with an adult; if height is under 48, print Too short to ride. Print exactly the correct message.

Expected output: Enjoy the ride

Show AP pseudocode reference
height ← 50
age ← 15
IF(height ≥ 48)
{
  IF(age ≥ 12)
  {
    DISPLAY("Enjoy the ride")
  }
  ELSE
  {
    DISPLAY("Ride with an adult")
  }
}
ELSE
{
  DISPLAY("Too short to ride")
}
The age check goes INSIDE the height branch, so it runs only when height is at least 48. With height 50 the outer is true, and with age 15 the inner is true, so the innermost message prints.

Problem 2 of 4

Same ride gate, new rider: 44 inches tall and age 30. Use the exact same nested structure and print the message the gate gives this rider. Predict it before you run: does the age even matter here?

Expected output: Too short to ride

Show AP pseudocode reference
height ← 44
age ← 30
IF(height ≥ 48)
{
  IF(age ≥ 12)
  {
    DISPLAY("Enjoy the ride")
  }
  ELSE
  {
    DISPLAY("Ride with an adult")
  }
}
ELSE
{
  DISPLAY("Too short to ride")
}
44 >= 48 is false, so control jumps straight to the outer ELSE. The inner age condition is never evaluated — even though 30 >= 12 is true, the program never gets there.

Problem 3 of 4

Add a third level. For a rider who is tall enough (height at least 48) AND at least 12, add a front-row rule: if they are at least 16 print Enjoy the ride - front row unlocked, otherwise print Enjoy the ride - back rows only. Riders age 12 up to 15 still ride, just not up front. Keep Ride with an adult (age under 12) and Too short to ride (height under 48). The rider is 52 inches, age 14. Print the correct message.

Expected output: Enjoy the ride - back rows only

Show AP pseudocode reference
height ← 52
age ← 14
IF(height ≥ 48)
{
  IF(age ≥ 12)
  {
    IF(age ≥ 16)
    {
      DISPLAY("Enjoy the ride - front row unlocked")
    }
    ELSE
    {
      DISPLAY("Enjoy the ride - back rows only")
    }
  }
  ELSE
  {
    DISPLAY("Ride with an adult")
  }
}
ELSE
{
  DISPLAY("Too short to ride")
}
Trace outside-in through three levels: 52 >= 48 true, 14 >= 12 true, then 14 >= 16 is false, so the deepest ELSE runs. The front-row check is reached only because both conditions above it were true.

Problem 4 of 4

Write from scratch. A different ride, The Kraken, has these rules: a rider must weigh 300 pounds or less to board. Among riders who can board, if they have a wristband print Board now, otherwise print Get a wristband. A rider over 300 pounds prints Cannot ride. This rider weighs 280 pounds and does NOT have a wristband (hasBand is false). Write the nested conditional and print the correct message.

Expected output: Get a wristband

Show AP pseudocode reference
weight ← 280
hasBand ← false
IF(weight ≤ 300)
{
  IF(hasBand)
  {
    DISPLAY("Board now")
  }
  ELSE
  {
    DISPLAY("Get a wristband")
  }
}
ELSE
{
  DISPLAY("Cannot ride")
}
Weight is the gating question, so it is the outer IF. Nest the wristband check inside the can-board branch. Here 280 <= 300 is true and hasBand is false, so the inner ELSE prints "Get a wristband".

Your code runs on a secure external service. Answers are checked automatically — nothing is stored.

Get in Touch

Whether you're a student, parent, or teacher — I'd love to hear from you.

Just want free AP CS resources?

Enter your email below and check the subscribe box — no message needed. Students get daily practice questions and study tips. Teachers get curriculum resources and teaching strategies.

Typically responds within 24 hours

Message Sent!

Thanks for reaching out. I'll get back to you within 24 hours.

🏫 Welcome, fellow educator!

I offer curriculum resources, practice materials, and study guides designed for AP CS teachers. Let me know what you're looking for — whether it's classroom materials, a guest speaker, or Teachers Pay Teachers resources.

Email

[email protected]

📚

Courses

AP CSA, CSP, & Cybersecurity

Response Time

Within 24 hours

Prefer email? Reach me directly at [email protected]