Skip to main content

Inner Loop in AP Computer Science A

In AP Computer Science A, the inner loop is a loop placed in the body of another (outer) loop. It must finish all of its iterations before the outer loop moves to its next iteration, so it restarts from the beginning on every outer pass (Topic 2.11, Nested Iteration).

This definition is part of Fiveable's AP Computer Science A review, which also has guided practice questions.

Verified for the 2027 AP Computer Science A exam•Last updated October 2026

What is the Inner Loop?

An inner loop is a loop that lives inside the body of another loop, called the outer loop. Together they form nested iteration. The CED spells out the key rule in EK 2.11.A.1. When a loop is nested inside another loop, the inner loop must complete all its iterations before the outer loop can continue to its next iteration.

Think of a clock. The minute hand (inner loop) has to go all the way around before the hour hand (outer loop) ticks forward one spot. Then the minute hand starts over at zero. That "starts over" part trips people up. The inner loop's control variable is usually reinitialized every time the outer loop runs, so the inner loop runs its full course again and again, once per outer iteration. Often the inner loop's bounds depend on the outer variable (like j <= i). In that case the inner loop runs a different number of times on each outer pass.

Why the Inner Loop matters in AP® Computer Science A

The inner loop sits in Unit 2: Selection and Iteration, specifically Topic 2.11 Nested Iteration. It directly supports learning objective AP Comp Sci A 2.11.A, which asks you to develop code to represent nested iterative processes and determine the result of these processes. In practice that means two skills. You need to write nested loops that do a job, and you need to trace nested loops to predict output or a final variable value. Getting the inner loop right is the whole game here. Most tracing mistakes happen because someone forgets the inner loop resets, or miscounts how many times it runs when its bound depends on the outer variable. This is also the pattern you'll reuse any time you process grid-shaped data row by row, which is exactly what 2022 FRQ Q4 asked for.

How the Inner Loop connects across the course

Outer Loop (Unit 2)

The outer loop controls how many times the inner loop gets launched. If the outer loop runs 3 times and the inner loop runs 4 times per pass, the inner body runs 3 × 4 = 12 times total. The outer loop sets the rounds, and the inner loop does the full work inside each round.

Nested Loops (Unit 2)

"Nested loops" names the whole structure, and "inner loop" names one piece of it. When a question says "nested loops," your first move is to find the inner loop and figure out its bounds. That's where the output pattern comes from.

Iteration (Unit 2)

An inner loop is just a regular for or while loop that happens to be placed inside another loop. Everything you know about single loops still applies, including initialization, the loop condition, the update, and off-by-one errors. The only new idea is that the whole loop runs again on every outer pass.

Tracing Dependent Bounds (Unit 2)

When the inner loop's start or end uses the outer variable (like j <= i or j = i), the inner loop changes size each round. That produces triangle-shaped output and sums that aren't a simple multiplication. Spotting the dependency is the trick behind many Topic 2.11 MCQs.

Is the Inner Loop on the AP® Computer Science A exam?

Inner loops show up mostly as code-tracing MCQs. A typical stem asks "What is printed as a result of executing this code segment?" or "What is the value of result after this code executes?"

Here's a classic example:

</>Java
for (int i = 1; i <= 3; i++) {
  for (int j = 1; j <= i; j++) {
    System.out.print(j + " ");
  }
  System.out.println();
}

The inner loop runs 1, then 2, then 3 times, and j restarts at 1 every pass. So it prints 1, then 1 2, then 1 2 3 on separate lines. Notice the println() sits in the outer loop, after the inner loop finishes. That placement is why each row ends with a line break.

Another common style accumulates a value. If result += i * j runs inside an inner loop that starts at j = i and goes to 4, with i from 1 to 4, you add 10 + 18 + 21 + 16 = 65. A shrinking inner loop like this is a favorite trap for anyone who assumes 16 total iterations.

On FRQs, you'll write inner loops. 2022 FRQ Q4 involved a two-dimensional array of integers in a Data class. Processing every element of a grid like that calls for an outer loop over rows and an inner loop over the columns of each row. Tracing a small table by hand (outer value, inner value, what happens) is the most reliable strategy for both question types.

The Inner Loop vs Outer Loop

The outer loop is the one on the outside. Each of its iterations triggers a complete run of the inner loop. The inner loop is the one inside the body, and it finishes all its iterations before control returns to the outer loop. A quick test is to ask which variable changes fastest. The inner loop's variable cycles through its full range many times, while the outer loop's variable moves one step at a time.

Key things to remember about the Inner Loop

  • An inner loop is a loop written inside the body of another loop, and the two together are called nested iteration (Topic 2.11).

  • Per EK 2.11.A.1, the inner loop must complete all of its iterations before the outer loop can continue to its next iteration.

  • The inner loop's control variable is usually reinitialized on every outer pass, so the inner loop starts over from the beginning each time.

  • If the inner loop runs a fixed number of times, total inner iterations equal outer iterations times inner iterations.

  • If the inner loop's bounds depend on the outer variable (like j <= i), the inner loop runs a different number of times each round, so you have to trace it instead of multiplying.

  • Statements placed after the inner loop but still inside the outer loop, like System.out.println(), run once per outer iteration, not once per inner iteration.

Frequently asked questions about the Inner Loop

What is an inner loop in AP Computer Science A?

An inner loop is a loop nested inside the body of another loop. Each time the outer loop runs once, the inner loop runs through all of its iterations. It's covered in Topic 2.11 Nested Iteration under learning objective 2.11.A.

Does the inner loop variable keep its value between outer loop passes?

Usually no. In a typical for loop like for (int j = 1; ...), j is re-declared and reset at the start of every outer iteration, so the inner loop starts over each time. Forgetting this reset is one of the most common tracing mistakes on nested loop MCQs.

What's the difference between an inner loop and an outer loop?

The outer loop is on the outside and runs fewer, slower steps. The inner loop is inside its body and completes its full range on every single outer step. With i from 1 to 3 outside and j from 1 to 4 inside, i changes 3 times while the inner body runs 12 times.

How many times does an inner loop run in nested loops?

If the bounds are fixed, multiply them, so an outer loop of 3 and an inner loop of 4 gives 12 runs of the inner body. If the inner bound depends on the outer variable, add up each pass instead. For i from 1 to 3 with j <= i, that's 1 + 2 + 3 = 6.

Are inner loops used on the AP CSA free-response questions?

Yes. Any FRQ that processes every element of a grid needs nested loops. 2022 FRQ Q4, for example, centered on a two-dimensional array of integers in a Data class, and that kind of row-by-row processing is exactly what an outer loop paired with an inner loop handles.