01 / 21

Nested
Conditionals

Learn how to place if statements inside other if statements to build complex decision-making logic in your programs.

AP Computer Science Principles

What Is a Conditional?

A conditional statement lets your program choose between different paths of execution based on whether a Boolean condition evaluates to true or false. It is the foundation of all decision-making in code.

IF (condition)
{
// runs when condition is true
}
ELSE
{
// runs when condition is false
}

What Makes It "Nested"?

A nested conditional is an if statement placed inside another if statement. The inner conditional only runs when the outer condition is true, creating a multi-level decision tree.

IF (outer condition) TRUE FALSE IF (inner condition) Else Block A TRUE FALSE Block B Block C 3 possible outcomes from 2 conditions

Why Use Nested Conditionals?

Sometimes a single true/false check is not enough. Nested conditionals let you refine a decision by asking a follow-up question only when the first answer is already known.

Multi-step decisions
Check eligibility first, then determine the specific tier or category.
Guard conditions
Validate input before performing a calculation that depends on it.
Priority logic
Handle the most important condition first, then branch into sub-cases.

The Structure of Nested Conditionals

IF (conditionA)
{
IF (conditionB)
{
// Both A and B are true
}
ELSE
{
// A is true, B is false
}
}
ELSE
{
// A is false (B is never checked)
}

Counting Execution Paths

Each level of nesting can at most double the number of unique paths through your code. Understanding how many paths exist helps you test every scenario.

1 level
2 paths: true or false
2 levels (nested)
Up to 3 paths: outer-false, inner-true, inner-false
3 levels (deep)
Up to 4 paths: grows with each nesting level

Nested vs. Compound Conditionals

Nested IF Compound (AND/OR)
Structure IF inside another IF Single IF with AND / OR
Readability Each condition on its own line One line, can get long
Evaluation Inner only runs if outer is true Both sides evaluated (short-circuit possible)
Use when Different actions per sub-case Same action for combined condition
Example IF \( \text{age} \ge 16 \) THEN IF \( \text{hasPermit} \)... IF \( \text{age} \ge 16 \) AND \( \text{hasPermit} \)

How to Write Nested Conditionals

1
Identify all conditions. List every Boolean check your program needs to make.
2
Determine the order. Which condition should be checked first? Put the broadest or most important check on the outside.
3
Write the outer IF/ELSE. Handle the top-level decision and its false branch.
4
Insert the inner IF/ELSE inside the true (or false) branch of the outer conditional.
5
Trace every path. Walk through each combination of true/false to verify the correct output.

Example 1: Grade Classifier

A teacher wants to display "Pass with Honors", "Pass", or "Fail" based on a student's score.

score ← 85

IF (\( \text{score} \ge 60 \))
{
IF (\( \text{score} \ge 90 \))
{
DISPLAY("Pass with Honors")
}
ELSE
{
DISPLAY("Pass")
}
}
ELSE
{
DISPLAY("Fail")
}

Tracing the Grade Classifier

\( \text{score} \ge 60? \)
TRUE FALSE
\( \text{score} \ge 90? \)
"Fail" TRUE FALSE "Pass with Honors" "Pass"
\( \text{score} = 85 \rightarrow \text{TRUE} \rightarrow \text{FALSE} \rightarrow \text{"Pass"} \)

Example 2: Ticket Pricing

A movie theater charges differently based on age and whether it is a matinee showing.

age ← 25
isMatinee ← true

IF (\( \text{age} < 13 \))
{
price ← 8
}
ELSE
{
IF (isMatinee)
{
price ← 10
}
ELSE
{
price ← 15
}
}

Example 3: Three Levels Deep

A login system checks: (1) is the username valid? (2) is the password correct? (3) is two-factor authentication enabled?

IF (validUser)
{
IF (correctPassword)
{
IF (twoFactorEnabled)
{
  DISPLAY("Enter 2FA code")
}
ELSE
{
  DISPLAY("Login successful")
}
}
ELSE
{
DISPLAY("Wrong password")
}
}
ELSE
{
DISPLAY("User not found")
}

Visualizing Three Levels

validUser? T F correctPassword? "User not found" T F twoFactorEnabled? "Wrong password" T F "Enter 2FA code" "Login successful" 4 outcomes from 3 nested conditions

When to Flatten Your Logic

Deep nesting (3+ levels) can hurt readability. Sometimes you can simplify by combining conditions with Boolean operators or by using early returns.

Keep Nested
When each sub-branch has different actions: different messages, different calculations, or different function calls.
Flatten with AND
When the inner and outer conditions lead to the same else action. Use IF (A AND B) instead.

Common Mistakes

Wrong Bracket Pairing
Mismatched curly braces cause the else to attach to the wrong if. Always indent consistently and count your brackets.
Unreachable Code
Placing contradictory conditions (e.g., checking \( x > 10 \) inside a branch where \( x \le 5 \) is guaranteed) means code that can never execute.
Unnecessary Nesting
Using nested IF when a single compound condition (AND/OR) would produce the same result, making code harder to read for no benefit.

Summary Table

Concept Details
Nested Conditional An IF inside another IF (or ELSE)
Why use it Different actions for each sub-case
Paths (\(n\) levels) Up to \(n + 1\) execution paths
Alternative Compound conditions (AND / OR)
Best practice Keep nesting to 2-3 levels max
Testing Trace every path to verify correctness

Question 1 of 5

What is displayed when this code runs with \( x = 15 \)?

Question 2 of 5

How many distinct execution paths does a program have if it contains one IF/ELSE with a nested IF/ELSE inside the true branch?

Question 3 of 5

What is displayed when \(\text{temp} = 75\) and \(\text{raining} = \text{true}\)?

Question 4 of 5

Which of the following is equivalent to this nested conditional?

Question 5 of 5

A programmer writes 3 levels of nested IF/ELSE (each with an else branch). What is the maximum number of distinct execution paths?