Nested
Conditionals
Learn how to place if statements inside other if statements to build complex decision-making logic in your programs.
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.
{
// 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.
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.
The Structure of Nested Conditionals
{
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.
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
Example 1: Grade Classifier
A teacher wants to display "Pass with Honors", "Pass", or "Fail" based on a student's score.
IF (\( \text{score} \ge 60 \))
{
IF (\( \text{score} \ge 90 \))
{
DISPLAY("Pass with Honors")
}
ELSE
{
DISPLAY("Pass")
}
}
ELSE
{
DISPLAY("Fail")
}
Tracing the Grade Classifier
Example 2: Ticket Pricing
A movie theater charges differently based on age and whether it is a matinee showing.
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 (correctPassword)
{
IF (twoFactorEnabled)
{
DISPLAY("Enter 2FA code")
}
ELSE
{
DISPLAY("Login successful")
}
}
ELSE
{
DISPLAY("Wrong password")
}
}
ELSE
{
DISPLAY("User not found")
}
Visualizing Three Levels
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.
Common Mistakes
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
x ← 15 IF (\( x > 10 \)) { IF (\( x > 20 \)) { DISPLAY("A") } ELSE { DISPLAY("B") } } ELSE { DISPLAY("C") }
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
temp ← 75 raining ← true IF (\( \text{temp} > 80 \)) { DISPLAY("Stay inside, too hot") } ELSE { IF (raining) { DISPLAY("Bring an umbrella") } ELSE { DISPLAY("Enjoy the outdoors") } }
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?
IF (\( \text{age} \ge 18 \)) { IF (hasTicket) { DISPLAY("Enter") } ELSE { DISPLAY("Buy ticket") } } ELSE { DISPLAY("Buy ticket") }
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?