In AP Computer Science A, nested loops are a loop placed inside the body of another loop, where the inner loop runs completely for every single iteration of the outer loop. They are the standard tool for traversing 2D arrays and producing row-by-column patterns.
Nested loops mean exactly what the name says. You write one loop (the inner loop) inside the body of another loop (the outer loop). Every time the outer loop runs once, the inner loop runs from start to finish. So if the outer loop runs 3 times and the inner loop runs 2 times per pass, the innermost code executes 3 × 2 = 6 times total. The total count multiplies, it doesn't add.
The mental model that makes this click is a clock. The outer loop is the hour hand and the inner loop is the minute hand. The minute hand has to sweep all the way around before the hour hand ticks forward once. In Java, each loop gets its own loop control variable (usually i for the outer and j for the inner), and the inner loop's variable resets and recounts on every outer iteration. That row-then-column rhythm is exactly why nested loops are the go-to structure for traversing 2D arrays, where i walks the rows and j walks the columns within each row.
Nested loops first show up in the Iteration unit (Unit 4) of AP CSA, where you learn to trace them, count their executions, and use them for pattern-building problems. Then they become unavoidable in Unit 8, because traversing a 2D array in Java requires one loop for rows and a second loop inside it for columns. The CED expects you to write nested iteration, trace it by hand, and analyze how many times statements execute. That last skill, counting executions, is a classic MCQ target. If you can't multiply the outer count by the inner count, you'll miss easy points. And on the free-response section, the 2D array question is essentially a nested-loops question wearing a costume.
Outer Loop and Inner Loop (Unit 4)
These are the two halves of every nested loop. The outer loop controls how many full cycles happen, and the inner loop does a complete run inside each one. On trace questions, mixing up which variable belongs to which loop is the fastest way to a wrong answer.
Traverse (Units 6 & 8)
Traversing means visiting every element of a structure. A single loop traverses a 1D array, but a 2D array needs nested loops, with the outer loop picking the row and the inner loop walking across that row's columns.
Loop control variable (Unit 4)
Each loop in a nest has its own control variable, conventionally i and j. The inner variable resets every time the outer loop advances. Knowing the current value of both variables at any moment is the whole game when tracing nested code.
Algorithm efficiency (Unit 4)
Nested loops multiply work. Two nested loops over n elements means roughly n² statement executions, which is why the CED's statement-counting questions love nested structures. A single loop grows linearly; a nested loop grows much faster.
Multiple-choice questions love nested loops in two flavors. The first is the trace question, where you're given a code segment and asked for the output or a final value. For example, a nested loop that adds i * j for i from 1 to 3 and j from 1 to 2 produces 18, and you only get there by patiently tracking both variables. The second flavor is the execution-count question, where you multiply the outer iterations by the inner iterations to find how many times a statement runs. On the FRQ side, no released free-response question asks you to define nested loops by name, but the 2D array FRQ effectively requires you to write them. Filling a matrix, searching it, or summing a row all follow the same skeleton, an outer loop on matrix.length and an inner loop on matrix[0].length. Memorize that skeleton and you walk into FRQ 4 with half the structure already done.
Two loops written one after the other run independently, so their iteration counts add (n + m total passes). Nested loops run one inside the other, so the counts multiply (n × m executions of the inner body). If an MCQ asks how many times a statement executes, first check whether the loops are nested or sequential, because the math is completely different.
A nested loop is a loop inside another loop, and the inner loop runs completely for every single iteration of the outer loop.
Total executions of the innermost statement equal the outer loop's count multiplied by the inner loop's count, not added.
The inner loop's control variable resets and recounts from its starting value on every pass of the outer loop.
Traversing a 2D array requires nested loops, with the outer loop on rows (matrix.length) and the inner loop on columns (matrix[0].length).
When tracing nested loop output by hand, write down the value of both control variables at every step instead of trying to do it in your head.
Nested loops are how the exam tests algorithm efficiency intuition, since nesting two n-length loops creates roughly n² work.
Nested loops are one loop written inside the body of another. The inner loop runs from start to finish on every single iteration of the outer loop, which makes them the standard pattern for working through rows and columns of a 2D array.
They multiply. If the outer loop runs 3 times and the inner loop runs 2 times per pass, the inner body executes 6 times total. Two separate back-to-back loops would add instead (3 + 2 = 5).
Two loops in a row run independently and one finishes before the other starts. In a nested loop, the inner loop restarts every time the outer loop advances, so the inner loop's variable cycles through all its values repeatedly. That difference changes both the output and the execution count.
Almost certainly yes. FRQ 4 is the 2D array question, and traversing, searching, or filling a 2D array in Java is done with an outer loop over rows and an inner loop over columns. Writing that skeleton correctly is worth real rubric points.
Yes, and the exam tests this. The inner loop's condition or body can depend on the outer variable, like for (int j = 0; j < i; j++), which makes the inner loop run a different number of times on each outer pass. These triangle-shaped loops are common in trace questions.