When a loop is placed entirely within the body of another loop, it is called nested iteration. Topic 4.4 challenges your ability to mentally track two different loop variables simultaneously. This topic is the gateway to understanding Unit 8 (2D Arrays), so mastering it now is critical.
In this interactive practice set, we cover Topic 4.4: Nested Iteration. These 10 multiple-choice questions will test your ability to calculate total executions, trace matrix-style printed patterns, and manage inner-loop conditions that depend on outer-loop variables.
Key Concepts to Remember Before You Start:
- Total Executions: If the outer loop runs x times and the inner loop runs y times (independently), the inner statement executes x * y times.
- Resetting the Inner Loop: The inner loop completes its entire cycle and its variable resets to the initial value for every single iteration of the outer loop.
- Dependent Bounds: If the inner loop’s boundary relies on the outer loop’s variable (e.g.,
for (int j = 0; j < i; j++)), the execution count changes dynamically (often forming triangular patterns).
Test your knowledge with the interactive quiz below. Careful tracing is required—grab some scratch paper before you begin!
AP CSA 4.4: Nested Iteration
"*" be printed?for (int j = 0; j < 4; j++) {
System.out.print(“*”);
}
}
for (int c = 1; c <= r; c++) {
System.out.print(c + ” “);
}
System.out.println();
}
println() at the end of the outer loop moves the output to a new line, creating a triangle.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 times)
x=2: y goes 5, 4, 3 (3 times)
x=3: y goes 5, 4 (2 times)
x=4: y goes 5 (1 time)
x=5: y > 5 is false (0 times)
Total: 4 + 3 + 2 + 1 = 10.
for (int i = 0; i < 2; i++) {
for (int j = 1; j < 4; j++) {
total += (i + j);
}
}
System.out.println(total);
When i=1: add (1+1) + (1+2) + (1+3) = 12.
Total = 6 + 12 = 18.
int b = 1;
while (b <= 2) {
System.out.print(a + “” + b + ” “);
b++;
}
}
a=1: prints “11 “, “12 “
a=2: prints “21 “, “22 “
a=3: prints “31 “, “32 ”
for (int i = 0; i < 5; i++) {
for (int j = 0; j < 5; j++) {
if (i == j) {
k++;
}
}
}
System.out.println(k);
i == j only evaluates to true when both variables are equal: (0,0), (1,1), (2,2), (3,3), and (4,4). This happens 5 times.for (int i = 0; i < 3; i++) {
for (int j = i; j < 3; j++) {
sum++;
}
}
System.out.println(sum);
i=0: j goes 0, 1, 2 (3 iterations)
i=1: j goes 1, 2 (2 iterations)
i=2: j goes 2 (1 iteration)
Total iterations = 3 + 2 + 1 = 6.
for (int j = 0; j < 5; j++) {
if (i + j == 4) {
System.out.print(“X”);
} else {
System.out.print(“O”);
}
}
System.out.println();
}
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: j goes 0. p = 2. result = 2.
i=1: j goes 0, 1. p = 4. result = 2 + 4 = 6.
i=2: j goes 0, 1, 2. p = 8. result = 6 + 8 = 14.
i=3: j goes 0, 1, 2, 3. p = 16. result = 14 + 16 = 30.
Prints 30.
Unit 4.4 Nested Iteration Complete!
Why Nested Loops Bridge Unit 4 and Unit 8
Nested iteration is one of the most conceptually challenging parts of Unit 4. You must be able to hold the state of the outer loop steady in your mind while the inner loop rapidly cycles through all its values. This exact same mental mechanism is required in Unit 8: 2D Arrays to process grids of data.
Ready for the Ultimate Challenge?
You have completed all the core iteration practice modules! It is time to test your overall mastery of Unit 4 with a comprehensive exam.
Continue your AP CSA review by proceeding to the Unit 4 Mock Exam, featuring 15 timed, mixed-concept questions spanning while loops, for loops, algorithms, and nested iteration.