Unit 2 Day 11: Nested Loops

Unit 2: Selection and Iteration
Day 11 Practice - January 16, 2026
Focus: Nested Loops

Practice Question

Consider the following code segment:
int count = 0;

for (int i = 0; i < 3; i++)
{
 for (int j = 0; j < 4; j++)
 {
 count++;
 }
}

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

What This Tests: This question tests your understanding of how nested loops execute and how to count total iterations.

Key Concept: Nested Loop Iterations

In nested loops, the inner loop completes ALL its iterations for EACH iteration of the outer loop. Total iterations = outer × inner.

// Outer loop runs 3 times (i = 0, 1, 2)
// Inner loop runs 4 times for EACH outer iteration

// i=0: j runs 0,1,2,3 -> count becomes 4
// i=1: j runs 0,1,2,3 -> count becomes 8
// i=2: j runs 0,1,2,3 -> count becomes 12

// Total: 3 × 4 = 12

Common Mistakes

Mistake: Answer A - Adding instead of multiplying

3 + 4 = 7 is wrong. The inner loop runs 4 times FOR EACH of the 3 outer iterations, so it's 3 × 4 = 12.

Mistake: Answer C or D - Only counting one loop

Don't ignore one of the loops. Both loops contribute to the total count.

Counting Nested Iterations

Quick Formula for Total Iterations

Total iterations = (outer loop count) × (inner loop count)

For independent loops (inner doesn't depend on outer variable):

- Outer: 0 to n-1 -> n iterations

- Inner: 0 to m-1 -> m iterations

- Total: n × m

Example: i < 3 gives 3 iterations, j < 4 gives 4 iterations -> 12 total

Difficulty: Medium - Time: 1-2 minutes - AP Skill: 1.A - Determine output

Want More Practice?

Master AP CSA with guided practice and expert help

Schedule 1-on-1 Tutoring Practice FRQs

Back to blog

Leave a comment

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