6m left·0%
Reading Time: 6 min
Last Updated: July 14, 2026
Main Ideas: 5
Reading Time: 6 min
Last Updated: July 14, 2026
Main Ideas: 5

Topic 2.4 Notes – Nested if Statements

Verified for 2027 AP® Computer Science A Exam
Read aloud
Nested if statements let you build layered decisions in Java. Instead of checking one condition and stopping, you can check a second condition only if the first one passes. This is how you model “if this, and inside that, if something else” logic. You’ll also see this idea in multiway if-else-if chains that choose exactly one path out of several.

1. What Nested If Statements Are

A nested if statement is any if, if-else, or if-else-if placed inside another one.

Here’s the basic shape:

if (outerCondition) {

    if (innerCondition) {
        // runs only if BOTH are true
    } else {
        // outer true, inner false
    }

} else {
    // outer false
}

The key rule you must remember:

The inner Boolean expression is evaluated only if the outer condition is true.

So think in layers:

  • Outer condition = first filter
  • Inner condition = more specific check that only matters after passing the first filter

Visualizing the Logic

This flowchart shows how control moves through an outer test and, only if that test is true, into a nested test.

Study guide illustration

Flowchart of a nested if-else structure

If the outer test expression is false, execution goes straight to the outer else and the nested decision is never checked. On code tracing questions, this is where students lose points.

2. Multiway Selection with if-else-if

A multiway selection uses an if-else-if chain to choose one block from several possibilities.

if (condition1) {
    // block 1
} else if (condition2) {
    // block 2
} else if (condition3) {
    // block 3
} else {
    // default block
}

How it runs:

  1. Check condition1.
  2. If true → execute block 1 and stop.
  3. If false → check condition2.
  4. Continue down the chain.
  5. If none are true and there’s an else, that block runs.

Important facts:

  • Conditions are evaluated top to bottom.
  • No more than one block executes.
  • The first true condition “wins.”

Order Matters

int score = 92;

if (score >= 70) {
    System.out.println("C");
} else if (score >= 90) {
    System.out.println("A");
}

This prints C, because score >= 70 is true first. The >= 90 check never happens.

For ranges, order from most specific to most general:

if (score >= 90) {
    System.out.println("A");
} else if (score >= 80) {
    System.out.println("B");
} else if (score >= 70) {
    System.out.println("C");
} else {
    System.out.println("F");
}

On tests, they love giving overlapping ranges just to see if you understand this.

3. How Nested Branching Executes

When tracing nested conditionals, slow down and go one layer at a time.

Example:

int x = 5;
int y = 10;

if (x < 10) {
    if (y > 20) {
        System.out.println("A");
    } else {
        System.out.println("B");
    }
} else {
    System.out.println("C");
}

Step-by-step:

  1. x < 10 → true
  2. Enter outer block.
  3. y > 20 → false
  4. Print "B"

You never even consider "C" because the outer condition was true.

Nested vs Separate ifs

Compare:

if (a > 0) {
    if (b > 0) {
        System.out.println("Both positive");
    }
}

vs.

if (a > 0) {
    System.out.println("a positive");
}
if (b > 0) {
    System.out.println("b positive");
}
  • Nested version checks b only if a > 0.
  • Separate ifs check both independently.

This difference shows up constantly in multiple choice tracing problems.

4. Writing Correct Nested Logic

You nest when a second decision only makes sense after passing the first one.

Example:

if (balance >= amount) {
    if (amount > 0) {
        balance = balance - amount;
    }
}

The withdrawal only happens if:

  • There’s enough money.
  • The amount is positive.

Avoiding Logical Errors

Unreachable branch:

if (num >= 50) {
    // ...
} else if (num > 50) {
    // never runs
}

The second branch can never run: any num greater than 50 is also >= 50, so it already took the first branch. Order range checks from most specific to least specific.

Variable Scope Reminder

If you declare a variable inside a block, you can’t use it outside:

if (x > 0) {
    int result = x * 2;
}
// result not accessible here

Declare it before the if if you need it later.

5. Common AP Mistakes

  • Forgetting that the inner if runs only when the outer is true.
  • Thinking multiple else-if blocks can execute.
  • Writing conditions in the wrong order.
  • Missing a final else when every case should be handled.
  • Confusing nested ifs with separate ifs.
  • Creating overlapping or unreachable conditions.

When tracing on a quiz, literally mark which branch runs and stop evaluating once a true else-if executes. That habit alone saves a lot of points.

Key Takeaways

The inner condition in a nested if is evaluated only if the outer condition is true.
In an if-else-if chain, at most one block runs (exactly one if there is a final else), and it’s the first true condition.
Order matters in multiway selection, especially with ranges like >=.
Nested ifs create dependent decisions, while separate ifs create independent ones.
Unreachable or overlapping conditions are common sources of lost points.

AP® is a trademark registered by the College Board, which is not affiliated with, and does not endorse this website.

Notes

1 credit used · 5/5 remaining