6m left·0%
Reading Time: 6 min
Last Updated: February 26, 2026
Main Ideas: 5
Reading Time: 6 min
Last Updated: February 26, 2026
Main Ideas: 5

Topic 2.8 Notes – For Loops

Verified for 2027 AP® Computer Science A Exam
Read aloud
For loops are Java’s go-to tool when you know how many times something should repeat. They package the setup, the stopping rule, and the change each round into one clean line. In AP CSA, they show up constantly in counting, array traversal, and 2D array problems.

1. What a For Loop Is

A for loop is an iterative statement that repeats code based on a condition. Its structure:

for (initialization; condition; update) {
    // loop body
}

All three control pieces live in the header.

Initialization

  • Runs once, before anything else.
  • Often declares a loop control variable, like int i = 0.
  • This variable controls how long the loop runs.

Condition

  • A Boolean expression.
  • Checked before every iteration.
  • If true, the body runs.
  • If false, the loop stops immediately.

Update

  • Runs after the entire loop body finishes.
  • Usually changes the loop control variable.
    • i++
    • i--
    • i += 2
  • Moves the variable toward making the condition false.

Here’s a labeled example:

for (int i = 0; i < 4; i++) {
    System.out.println(i);
}
  • Initialization → int i = 0
  • Condition → i < 4
  • Update → i++

2. How a For Loop Executes

The execution order is predictable. When tracing on a quiz, this order matters.

Exact sequence:

  1. Run initialization (once).
  2. Check condition.
    • If false → skip the loop entirely.
  3. Execute loop body.
  4. Run update.
  5. Go back to step 2.

Picture the control flow like this as the loop runs:

Study guide illustration

For loop control flow

Start leads to the condition check. If the condition is true, the statements run, then the update runs, and the flow returns to the condition. If the condition is false, the loop ends.

Key consequences:

  • The update does not run before the first iteration.
  • If the condition is false right away, the body never runs.
  • The condition is checked before every iteration.

Example trace:

int total = 0;

for (int i = 1; i <= 3; i++) {
    total += i;
}

Iteration values of i:

i = 1 → total = 1
i = 2 → total = 3
i = 3 → total = 6
i becomes 4 → condition false → stop

Final total is 6.

On multiple choice, they love slipping in a <= or changing the starting value. Always write out the loop variable values.

3. Standard For Loop Patterns

These patterns show up constantly on FRQs.

Counting Up

for (int i = 0; i < n; i++)
  • Runs n times.
  • Common for arrays.
  • Accesses indices 0 through n - 1.

Counting Down

for (int i = n - 1; i >= 0; i--)
  • Reverse traversal.
  • Used for reversing strings or arrays.

Array Traversal

for (int i = 0; i < arr.length; i++) {
    System.out.println(arr[i]);
}

Remember:

  • Valid indices are 0 to arr.length - 1.
  • i < arr.length prevents ArrayIndexOutOfBoundsException.

Nested For Loops

for (int row = 0; row < 2; row++) {
    for (int col = 0; col < 3; col++) {
        System.out.println(row + "," + col);
    }
}

Order of output:

0,0
0,1
0,2
1,0
1,1
1,2

The inner loop completes fully before the outer loop increments. In 2D array FRQs, this pattern is everywhere.

4. Converting Between For and While Loops

A for loop can always be rewritten as a while loop.

Original:

for (int i = 2; i < 6; i++) {
    System.out.println(i);
}

Equivalent while loop:

int i = 2;          // initialization
while (i < 6) {     // condition
    System.out.println(i);
    i++;            // update
}

Rules:

  • Initialization goes before the while loop.
  • Update goes inside, at the bottom.

If you understand this conversion, you understand how the loop actually runs. Sometimes the AP disguises a for loop as a while loop to test whether you see they’re equivalent.

5. Common AP Mistakes

Off-by-One Errors

Wrong:

for (int i = 0; i <= arr.length; i++)

Correct:

for (int i = 0; i < arr.length; i++)

<= arr.length goes one step too far.

Also check:

  • Starting value
  • Ending condition
  • Increment direction

Wrong Update Direction

for (int i = 5; i >= 1; i++)  // infinite loop

The condition decreases, but the update increases. The loop never ends.

The update must move the variable toward making the condition false.

Scope of Loop Variable

for (int i = 0; i < 3; i++) {
    System.out.println(i);
}

i only exists inside the loop.

If you need it later:

int i;
for (i = 0; i < 3; i++) {
    System.out.println(i);
}
System.out.println(i);  // now valid

This scope detail shows up in tricky multiple choice questions.

Key Takeaways

Initialization runs once, condition is checked before each iteration, update runs after the body.
For arrays, use i < arr.length, never <=.
The update must push the loop variable toward making the condition false.
Nested loops finish the entire inner loop before changing the outer loop variable.
A for loop is just a while loop with initialization and update moved into the header.

AP® is a trademark registered by the College Board, which is not affiliated with, and does not endorse this website.

Notes

1 credit used · 5/5 remaining