AP CSA Unit 2.4: Nested If Statements Practice

Unit 2, Section 2.4
Day 4 Practice • January 10, 2026
🎯 Focus: Nested if Statements

Practice Question

Consider the following code segment:
int x = 5;
int y = 8;

if (x < 10) {
    if (y > 5) {
        x = x + y;
    } else {
        x = x - y;
    }
}

System.out.println(x);
What is printed as a result of executing this code segment?

What This Tests: Nested if statements require evaluating the outer condition first, then the inner condition. Both must be traced carefully.

Step-by-Step Trace

Condition Evaluation Action
x < 10 5 < 10 = true Enter outer if
y > 5 8 > 5 = true Enter inner if
x = x + y 5 + 8 = 13 x becomes 13

Output: 13

Common Mistakes

Mistake: Answer A (5)

This assumes the outer if is false. But 5 < 10 is true, so we DO enter the outer block.

Mistake: Answer C (-3)

This executes the else branch (x - y = 5 - 8 = -3). But y > 5 is true (8 > 5), so we execute x + y, not x - y.

Practice Technique

Trace Outside-In
  1. Evaluate the outermost condition first
  2. If true, move to the inner condition
  3. Keep going until you find the executing block
Difficulty: Medium • Time: 2-3 minutes • AP Skill: 2.B - Determine code results

Ready to Level Up Your AP CSA Skills?

Get personalized help or access our complete question bank

Premium Question Bank - Coming Soon! Schedule 1-on-1 Tutoring
Back to blog

Leave a comment

Please note, comments need to be approved before they are published.