Nested
Iteration
Master loops inside loops: understand how nested iteration works, trace execution counts, and build powerful patterns in Java.
What Is Iteration?
Iteration means repeating a block of code multiple times using a loop. In Java, the most common loops are the for loop and the while loop. A single loop lets you repeat an action \( n \) times.
What Is Nested Iteration?
Nested iteration is when you place one loop inside another. The inner loop runs completely for every single iteration of the outer loop. This is how you process grids, tables, and multi-dimensional data.
Visualizing Execution Flow
The Iteration Count Rule
The total number of times the inner body executes in a nested loop is the product of the iteration counts of each loop:
A restorer repairs a luminous pixel mosaic
Grid algorithms turn a visual pattern into explicit row and column operations. Bounds checks determine which neighboring cells can be accessed.Trace a 4-by-5 nested loop
Four outer-loop iterations each run five inner iterations: 20 body executions in row-major order. The inner counter is initialized four times.Why This Matters: \( O(n^2) \)
When both loops iterate \( n \) times, the inner body runs \( n \times n = n^2 \) times. This means nested loops often produce quadratic time complexity.
Independent vs Dependent Inner Loop
| Independent | Dependent | |
|---|---|---|
| Inner bound | Fixed constant (e.g., col < 5) | Uses outer variable (e.g., col < row) |
| Inner count | Same every pass | Changes each pass |
| Total iterations | \( m \times n \) | \( \frac{n(n-1)}{2} \) (triangular) |
| Pattern shape | Rectangle | Triangle |
| Time complexity | \( O(n^2) \) | \( O(n^2) \) |
Dependent Loop: Triangle Pattern
How to Trace Nested Loops
print stays on the same line; println moves to the next line: this controls the output shape.Example 1: Rectangle Output
Example 2: Number Grid
Example 3: Accumulator in Nested Loops
Nested Loops and 2D Arrays
The most important use of nested iteration in AP CSA is traversing 2D arrays. The outer loop selects the row, and the inner loop selects the column.
Row-Major vs Column-Major
Common Mistakes
< vs <= or starting at 0 vs 1 changes the iteration count. Always trace the boundary values.i when you meant the inner variable j, or vice versa.println() inside the inner loop (too many newlines) or forgetting it after the inner loop (everything on one line).Nested Iteration Summary
| Key Idea | Formula / Pattern | |
|---|---|---|
| Independent loops | Inner bound is constant | \( m \times n \) iterations |
| Dependent loops | Inner bound uses outer var | \( \frac{n(n \pm 1)}{2} \) iterations |
| Row-major 2D | Outer = row, Inner = col | grid[r][c], left-to-right |
| Column-major 2D | Outer = col, Inner = row | grid[r][c], top-to-bottom |
| Time complexity | Two nested loops of size n | \( O(n^2) \) |
Question 1 of 5
How many times does the inner print statement execute?
Question 2 of 5
What is the output of this code?
Question 3 of 5
What value of sum is printed?
Question 4 of 5
Which traversal order visits elements top-to-bottom within each column before moving to the next column?
Question 5 of 5
How many total iterations does the inner loop body execute?