APrep Open the app

AP Computer Science Principles · Unit 3: Algorithms & Programming

3.7 Nested Conditionals

What is a nested conditional?

A nested conditional is a selection statement (IF/ELSE) placed inside another selection statement's body. This lets a program check additional conditions only after an outer condition has been evaluated, creating more precise decision paths. Nesting can go multiple levels deep, but too many levels can make code hard to read.

Nested vs. compound conditions

Nested conditionals differ from compound Boolean expressions (using AND/OR) even though they can sometimes produce the same result. `IF(a) { IF(b) {...} }` only runs the inner block when both a AND b are true — logically similar to `IF(a AND b)`, but nesting evaluates conditions in stages rather than as one combined expression.

Order matters

In nested conditionals, the outer condition is checked first; the inner condition is only evaluated if the program enters that branch. Changing the order of nested conditions, or which branch (IF/ELSE) they're placed in, can change which paths of code execute and produce different program behavior.

Key terms

Nested conditional
A selection statement (IF/ELSE) placed inside the body of another selection statement.
Outer condition
The first condition checked in a nested conditional; determines whether the inner condition gets evaluated at all.
Inner condition
A condition evaluated only if program flow enters the branch of the outer conditional containing it.
Nested IF vs. compound Boolean (AND)
Nested IFs check conditions in stages; a compound expression (a AND b) combines them into one check — they can produce equivalent results.
Risk of deep nesting
Too many nested levels can make code difficult to read, trace, and debug, even though it's syntactically valid.

Try an AP-style question

Code

x ← 8
IF (x > 5)
{
   IF (x > 10)
   {
      DISPLAY("A")
   }
   ELSE
   {
      DISPLAY("B")
   }
}
ELSE
{
   DISPLAY("C")
}

What is displayed?

  1. C
  2. B
  3. A
  4. Nothing
Show the answer

B. 8 > 5 but not > 10.

Practice 8 AP-style questions with instant explanations, flashcards and spaced review.

Study 3.7 free in APrep