Unit 2 Cycle 2 Day 26: Iteration (break in nested loops)

Unit 2, Selection & Iteration • Cycle 2
Day 26 Advanced Practice • Harder Difficulty
Focus: Break Scope Hard Iteration (break in nested loops)

Advanced Practice Question

Format: Iteration (break in nested loops)

What is printed?
"apcs-keyword">int c = "apcs-number">0;

"apcs-keyword">for ("apcs-keyword">int i = "apcs-number">1; i <= "apcs-number">3; i++)
{
    "apcs-keyword">for ("apcs-keyword">int j = "apcs-number">1; j <= "apcs-number">3; j++)
    {
        "apcs-keyword">if (j == "apcs-number">2)
        {
            break;
        }

        c++;
    }
}

System.out.println(c);
Difficulty: Hard  |  Topic: Iteration (break in nested loops)  |  Cycle: 2 (Advanced)
Why This Answer?

For each i, the inner loop increments c when j=1, then breaks at j=2. That's 1 increment per outer iteration. Outer runs 3 times → c = 3.

Common Mistake
Watch Out!

Thinking `break` exits both loops (it exits only the inner loop).

AP Exam Strategy

`break` only exits the loop it is directly inside.

Master This Topic

This Cycle 2 HARD question tests break scope. Review Unit 2 concepts to build mastery of selection and iteration.

  • Understanding break scope
  • Tracing code execution accurately
  • Avoiding common pitfalls
View Unit 2 Study Guide

Ready for More Challenges?

Cycle 2 questions prepare you for the hardest AP CSA exam questions.

Study Games Practice FRQs
Back to blog

Leave a comment

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