The for loop: counted repetition.
When you know how many times to repeat, the for loop is the cleanest, most readable choice in Java.
A for loop packages three pieces of loop control — initialization, condition, and update — into one tidy header. The loop body runs as long as the condition is true.
For loops are the workhorse of array and String traversal in AP CSA. They appear in nearly every FRQ that involves processing a sequence of values.
Tracing a for loop accurately — and knowing how many iterations it runs — is one of the most heavily tested skills on the exam.
Three parts, one header.
Understand exactly when each part runs.
Syntax
for (initialization; condition; update) {
// loop body
}
Execution order
- Initialization runs once before the loop begins.
- Condition is checked before every iteration; if
false, the loop exits. - Body runs when the condition is
true. - Update runs after each body execution, then the condition is re-checked.
Counting iterations
for (int i = 0; i < 5; i++) {
System.out.println(i);
}
This prints 0 1 2 3 4. The loop runs five times, not six. A for loop of the form for (int i = 0; i < n; i++) always runs exactly n times.
Traversing an array
int[] nums = {3, 7, 1, 9};
for (int i = 0; i < nums.length; i++) {
System.out.println(nums[i]);
}
Using nums.length keeps the loop safe even if the array changes size in your code.
Loop terminology you must know.
Used directly in AP question stems and FRQ rubrics.
- Loop control variable — the counter declared in the initialization (often
i). - Initialization — sets up the loop variable; runs once.
- Condition — boolean expression checked before each iteration.
- Update — modifies the loop variable after each body run.
- Iteration — one full pass through the loop body.
- Off-by-one error — running one too many or one too few iterations, usually from
<vs<=. - Enhanced
forloop —for (int n : nums) { ... }; iterates without an index, but you cannot modify array contents through it.
How for loops are tested.
Iteration counts, off-by-one traps, and array traversal dominate.
MCQ patterns to expect:
- How many times does the body run? Watch
<vs<=and the start value. - What is the final value of
i? After the loop ends, the loop variable is one past the last value used inside. - Step sizes other than 1 like
i += 2ori--. Trace these carefully. - Enhanced
forloop limitations. Reassigning the loop variable inside an enhancedfordoes not change the array.
FRQ tip: when traversing an array, always use arr.length rather than a hard-coded number — graders look for this.
Count iterations carefully.
Avoid the off-by-one trap by tracing the first and last iteration.
Problem. What is printed by the following loop, and how many times does the body run?
int total = 0;
for (int i = 2; i <= 10; i += 2) {
total += i;
}
System.out.println(total);
Step 1. Initialization: i = 2.
Step 2. Check i <= 10. Trace each iteration:
- i = 2 → total = 2
- i = 4 → total = 6
- i = 6 → total = 12
- i = 8 → total = 20
- i = 10 → total = 30
Step 3. After the body, i becomes 12. 12 <= 10 is false, so the loop exits.
Answer. The loop runs 5 times and prints 30.
Why this matters. Because the condition is <= (not <), the loop includes i = 10. Misreading this is the most common AP MCQ mistake.
Errors that cost loop questions.
Most loop bugs are counting bugs, not syntax bugs.
- Off-by-one errors.
i < nrunsntimes;i <= nrunsn + 1times when starting at0. - Wrong bound for arrays. Use
i < arr.length, noti <= arr.length— the latter causes anArrayIndexOutOfBoundsException. - Modifying the loop variable inside the body in unexpected ways, breaking iteration counts.
- Semicolon after the header:
for (int i = 0; i < 5; i++);creates an empty loop and the next block runs only once. - Using enhanced
forto "change" array values. The loop variable is a copy; assigning to it does not affect the array.
For loop patterns at a glance.
The patterns AP rewards in code and rubrics.
for Loop Reference
AP Quick Reference| Pattern | Meaning | AP Exam Tip |
|---|---|---|
for (int i = 0; i < n; i++) |
Runs exactly n times |
Standard array traversal pattern. |
for (int i = 1; i <= n; i++) |
Runs n times starting at 1 |
Useful when counting from one. |
for (int i = n - 1; i >= 0; i--) |
Reverse traversal | Required when shifting array elements right. |
for (int x : arr) |
Enhanced for loop |
Read-only over elements; no index available. |
Step i += 2 |
Skips every other value | Halves the iteration count. |
| After loop ends | Loop variable is one past the last used | Test asks "what is i after?" — be exact. |
How to master the for loop.
Trace, then write, then refactor.
Use the three practice tools below this lesson in order:
- MCQ Practice — for every loop, write out the first iteration, last iteration, and total count before choosing an answer. This kills the off-by-one trap.
- Java Lab — practise array traversal with
i, summing values, finding maxima, and reverse loops. Always usearr.lengthas the bound. - FRQ Practice — write loops that solve full AP-style problems like counting matches or building a new array. Comment your loop header to explain the count.
If you can trace a loop without running it, you can write one under exam pressure.
Practice — attempt these now.
AP-style assessments aligned to this lesson. Time them.