AP Computer Science A · Unit 2 lecture · Part 10 of 12 · 8 min

2.11 Nested iterationLoops inside loops

Mr. Thompkins
Think of a clock. The minute hand goes all the way around once for every single step of the hour hand. A nested loop works the same way, and tracing one is just a matter of being patient.

A nested loop is a loop inside the body of another loop. For every single pass of the outer loop, the inner loop starts over and runs all of its passes. Only when the inner loop finishes does the outer loop move to its next pass. Nested loops are how you handle anything with two dimensions: rows and columns, every pair of items, or every starting point and every length.

1for (int row = 1; row <= 4; row++)12{3    for (int col = 1; col <= row; col++)24    {5        System.out.print(col);6    }37    System.out.println();8}
  1. line 1 Outer loop: one pass per row
  2. line 3 Inner loop restarts at 1 on every row
  3. line 6 After the inner loop ends: next line
output › 1 12 123 1234
A triangle of numbers, run under Java 17. The inner loop's bound depends on the outer loop variable.
?Wait, what's the difference between print and println again?
print writes its value and stays on the same line. println writes its value (or nothing) and then moves to a new line. In the triangle, the inner loop uses print so the numbers line up in a row, and the println after the inner loop ends each row.
rowcol takesinner passesline printed
1111
21, 2212
31, 2, 33123
41, 2, 3, 441234
total10
Tracing the triangle: for each value of row, list every value col takes. The inner loop runs 1 + 2 + 3 + 4 = 10 times in all.

To trace a nested loop, write the outer variable on the left and, for each of its values, list every value the inner variable takes. Watch for inner loops whose start or end depends on the outer variable, like col <= row or j = i. Those change from row to row.

?Wait, why does the inner loop restart at 1 each row?
Because the inner loop's initialization, int col = 1, is part of the inner for statement, which is inside the outer loop's body. Every time the outer body runs, it reaches that for statement again and starts a brand-new inner loop from its initialization.
Pause and think
An outer loop runs 4 times. Inside it, an inner loop runs 6 times. How many times does a statement inside the inner loop run?
I thought about it. Show me
24 times. Each of the 4 outer passes runs all 6 inner passes: 4 × 6 = 24. With fixed bounds, you multiply.
Trace a nested loop

What is printed?

int total = 0;
for (int i = 1; i <= 3; i++)
{
    for (int j = i; j <= 3; j++)
    {
        total += i * j;
    }
}
System.out.println(total);
  1. The inner loop starts at j = i, so it gets shorter each time. List the pairs row by row.
    Why?
    An inner bound that depends on the outer variable is the thing to watch in every nested loop question.
  2. i = 1: j is 1, 2, 3. Products 1, 2, 3, which add 6. total is 6.
  3. i = 2: j is 2, 3. Products 4 and 6, which add 10. total is 16.
  4. i = 3: j is 3 only. Product 9. total is 25. Then i becomes 4, 4 <= 3 is false, and the outer loop ends.

Answer: 25

Stuck? Get a hint
  1. Make a row for each value of i, and list the j values in that row.
  2. j starts at i, not at 1.
  3. Add i * j for every pair: 6, then 10, then 9.
Check yourself
How many times is "*" printed?
for (int r = 1; r <= 4; r++)
{
    for (int c = 1; c < r; c++)
    {
        System.out.print("*");
    }
}
  1. A8
  2. B6
  3. C12
  4. D9
Show the answer

B. The inner loop runs while c < r. For r = 1 it runs 0 times, r = 2 once, r = 3 twice, r = 4 three times: 0 + 1 + 2 + 3 = 6. Choice C multiplies 4 × 3 as if both bounds were fixed, but the inner bound depends on r.

  • The inner loop runs all its passes for every single pass of the outer loop.
  • Trace one outer value at a time, listing every inner value.
  • Watch inner bounds that use the outer variable (j = i, col <= row).
  • print inside the inner loop, println after it: one line per outer pass.