Topic 2.8 Notes – For Loops
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:
- Run initialization (once).
- Check condition.
- If false → skip the loop entirely.
- Execute loop body.
- Run update.
- Go back to step 2.
Picture the control flow like this as the loop runs:
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
ntimes. - Common for arrays.
- Accesses indices
0throughn - 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
0toarr.length - 1. i < arr.lengthpreventsArrayIndexOutOfBoundsException.
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.