Unit 2 Cycle 2 Day 25: Iteration + Selection (nested condition)

Unit 2, Selection & Iteration • Cycle 2
Day 25 Advanced Practice • Harder Difficulty
Focus: Diagonal Match Hard Iteration + Selection (nested condition)

Advanced Practice Question

Format: Iteration + Selection (nested condition)

What is printed?
"apcs-keyword">int count = "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 (i == j)
        {
            count++;
        }
    }
}

System.out.println(count);
Difficulty: Hard  |  Topic: Iteration + Selection (nested condition)  |  Cycle: 2 (Advanced)
Why This Answer?

Matches occur when (i, j) is (1,1), (2,2), (3,3): 3 matches total.

Common Mistake
Watch Out!

Thinking every pair increments (that would be 9) and missing the `if (i == j)` filter.

AP Exam Strategy

When there is an `if` inside nested loops, count only the iterations that satisfy the condition.

Master This Topic

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

  • Understanding diagonal match
  • 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.