When a loop is placed entirely within the body of another loop, it is called nested iteration. AP CSA Topic 2.11 focuses on tracing nested loops, determining execution counts, and understanding how the inner loop behaves for each iteration of the outer loop.
In this interactive practice set, you will work through 10 multiple-choice questions covering nested loop execution, dependent loop bounds, tracing, and output patterns.
Key Concepts to Remember Before You Start:
- Total Executions: If the outer loop runs x times and the inner loop runs y times independently for each outer iteration, the inner statement executes x × y times.
- Resetting the Inner Loop: The inner loop starts again from its initialization whenever the outer loop begins another iteration.
- Dependent Bounds: If the inner loop's boundary or initialization depends on the outer loop's variable, the number of inner-loop executions can change from one outer iteration to the next.
Test your knowledge with the interactive quiz below. Careful tracing is required—work through the values of both loop variables systematically.
AP CSA 2.11: Nested Iteration
"*" be printed?for (int i = 0; i < 3; i++) {
for (int j = 0; j < 4; j++) {
System.out.print("*");
}
}i = 0, 1, 2.For each outer iteration, the inner loop runs 4 times:
j = 0, 1, 2, 3.Total executions = 3 × 4 = 12.
for (int r = 1; r <= 3; r++) {
for (int c = 1; c <= r; c++) {
System.out.print(c + " ");
}
System.out.println();
}r = 1, the inner loop prints 1.When r = 2, it prints 1 2.When r = 3, it prints 1 2 3.The
println() at the end of the outer loop moves the
output to a new line, creating a triangular pattern.int count = 0;
for (int x = 1; x <= 5; x++) {
for (int y = 5; y > x; y--) {
count++;
}
}
System.out.println(count);x = 1: y goes 5, 4, 3, 2 → 4 timesx = 2: y goes 5, 4, 3 → 3 timesx = 3: y goes 5, 4 → 2 timesx = 4: y goes 5 → 1 timex = 5: y > 5 is false → 0 timesTotal = 4 + 3 + 2 + 1 = 10.
int total = 0;
for (int i = 0; i < 2; i++) {
for (int j = 1; j < 4; j++) {
total += (i + j);
}
}
System.out.println(total);i = 0:(0 + 1) + (0 + 2) + (0 + 3) = 6.
When
i = 1:(1 + 1) + (1 + 2) + (1 + 3) = 9.
Total = 6 + 9 = 15.
for (int a = 1; a <= 3; a++) {
int b = 1;
while (b <= 2) {
System.out.print(a + "" + b + " ");
b++;
}
}while loop executes
twice.a = 1: prints "11 " and "12 "a = 2: prints "21 " and "22 "a = 3: prints "31 " and "32 "int k = 0;
for (int i = 0; i < 5; i++) {
for (int j = 0; j < 5; j++) {
if (i == j) {
k++;
}
}
}
System.out.println(k);i == j is true only when both
variables have the same value:(0,0), (1,1), (2,2), (3,3), and (4,4).
Therefore,
k is incremented 5 times.int sum = 0;
for (int i = 0; i < 3; i++) {
for (int j = i; j < 3; j++) {
sum++;
}
}
System.out.println(sum);j depends on i.i = 0: j goes 0, 1, 2 → 3 iterationsi = 1: j goes 1, 2 → 2 iterationsi = 2: j goes 2 → 1 iterationTotal iterations = 3 + 2 + 1 = 6.
for (int i = 0; i < 5; i++) {
for (int j = 0; j < 5; j++) {
if (i + j == 4) {
System.out.print("X");
} else {
System.out.print("O");
}
}
System.out.println();
}i + j == 4 is true for:(0,4), (1,3), (2,2), (3,1), and (4,0).
These positions form a diagonal running from the top-right corner to the bottom-left corner of the grid.
int n = 4;
int result = 0;
for (int i = 0; i < n; i++) {
int p = 1;
for (int j = 0; j <= i; j++) {
p *= 2;
}
result += p;
}
System.out.println(result);i = 0: p = 2, result = 2i = 1: p = 4, result = 6i = 2: p = 8, result = 14i = 3: p = 16, result = 30Therefore, the program prints 30.
For example, a
while loop can be nested inside a
for loop, and a for loop can be nested
inside a while loop.Unit 2.11 Nested Iteration Practice Complete!
Why Nested Iteration Matters for AP CSA
Nested iteration requires you to trace an inner loop while keeping track of the current value of the outer-loop variable. The number of inner-loop executions may remain constant or may change depending on the outer-loop value.
The same tracing skills are useful when working with rows and columns of two-dimensional arrays later in Unit 4: Data Collections. Being comfortable with nested loops will help you follow algorithms that process multiple dimensions of data.
Continue Your AP CSA Practice
After practicing nested iteration, continue with the other Unit 2: Selection and Iteration topics.
Review the Unit 2 Selection & Iteration Practice Assessment for additional mixed practice covering the currently available iteration topics.