How does a for loop package initialisation, condition and update into one header, and when do you use it?
Topic 4.2 for Loops: use a for loop, whose header combines initialisation, a boolean condition and an update, to repeat a block a controlled number of times, and convert between for and while loops.
A focused answer to AP CSA Topic 4.2, covering for-loop header syntax (init; condition; update), the order in which the three parts run, counting iterations, equivalence with while loops, common patterns, and tracing, with a fully worked example.
Reviewed by: AI editorial process; not yet individually human-reviewed
Have a quick question? Jump to the Q&A page
Jump to a section
What this topic is asking
The College Board (Topic 4.2) wants you to write and trace a for loop: a loop whose header gathers the three loop-control parts - initialisation, condition, and update - into one line. The for loop is the natural choice when you know how many times to repeat or are counting through a range. You should also be able to convert between a for loop and an equivalent while loop, since they express the same logic.
The for header
for (int i = 0; i < 5; i++) {
System.out.println(i);
}
The header has three parts separated by semicolons:
- Initialisation
int i = 0runs once, before the loop begins. The control variable declared here is scoped to the loop. - Condition
i < 5is a boolean tested before each iteration. - Update
i++runs at the end of each iteration, after the body.
The exact order of execution
This ordering is why the loop above prints 0 1 2 3 4: it tests i < 5, runs the body, then increments, and stops when i reaches 5 before the body runs again.
for and while are equivalent
For example:
// for version
for (int i = 1; i <= 3; i++) { System.out.println(i); }
// equivalent while version
int i = 1;
while (i <= 3) { System.out.println(i); i++; }
Counting iterations
To count how many times a for loop runs, list the values the control variable takes. for (int k = 0; k < n; k++) runs n times (k = 0 ... n-1). for (int k = 1; k <= n; k++) also runs n times (k = 1 ... n). for (int k = 0; k <= n; k++) runs n + 1 times. The ++ and -- operators step by one; k = k + 2 steps by two. When the step is larger than one, do not assume the count - write out the sequence of values the control variable actually takes and stop at the last one that still satisfies the condition. This is the same careful counting you will use for informal code analysis in Topic 4.5, where the step size and bounds together determine the iteration count.
Try this
Q1. How many times does for (int i = 0; i < 6; i++) run? [1 point]
- Cue. Six times, for
i = 0, 1, 2, 3, 4, 5.
Q2. Rewrite for (int i = 1; i <= n; i++) { sum += i; } as an equivalent while loop. [2 points]
- Cue.
int i = 1; while (i <= n) { sum += i; i++; }- initialisation before the loop, update at the end of the body.
Exam-style practice questions
Practice questions written in the style of College Board exam questions on this dot point, with worked answer explainers. The year tag is the paper they imitate, not the source.
AP 2021 (style)1 marksMultiple choice. Consider the following code segment.
```java
int total = 0;
for (int k = 2; k <= 8; k = k + 2) {
total = total + k;
}
System.out.println(total);
```
What is printed?
(A) `20`
(B) `18`
(C) `12`
(D) `30`
(E) `8`
Show worked answer →
The answer is (A).
The loop starts k at 2, continues while k <= 8, and adds 2 to k each pass, so k takes the values 2, 4, 6, 8. The body accumulates: total = 2 + 4 + 6 + 8 = 20. After k becomes 10, 10 <= 8 is false, so the loop ends. (B) drops the 2 or the 8; (C) stops too early; (D) includes 10.
Markers reward listing the control-variable values (2, 4, 6, 8) and summing the body across exactly those iterations.
AP 2020 (style)4 marksFree response (code writing). A positive `int` variable `n` is given. Write a code segment using a for loop that computes and prints the factorial of `n` (the product 1 * 2 * 3 * ... * n). For example, if `n` is 4, the output is 24. Assume the result fits in an int.
Show worked answer →
A 4-point question testing a counting for loop with a product accumulator.
int product = 1;
for (int i = 1; i <= n; i++) {
product = product * i;
}
System.out.println(product);
Point 1: initialise the accumulator product to 1 (not 0, which would zero the product). Point 2: the for header runs i from 1 to n inclusive with i <= n. Point 3: the body multiplies product by each i. Point 4: print after the loop. Starting product at 0, or using i < n, would give a wrong result.
Related dot points
- Topic 4.1 while Loops: use a while loop to repeat a block of statements while a boolean condition remains true, with correct initialisation, condition and update to avoid infinite or off-by-one loops.
A focused answer to AP CSA Topic 4.1, covering while loop syntax, the initialise/test/update pattern, why each loop control variable must change, infinite loops and off-by-one errors, and how to trace iterations, with a fully worked example.
- Topic 4.4 Nested Iteration: write and trace nested loops, where an inner loop runs in full on each pass of an outer loop, and count the total number of inner-loop iterations.
A focused answer to AP CSA Topic 4.4, covering nested loops, how the inner loop completes fully on each outer pass, counting total iterations (including triangular patterns where the inner bound depends on the outer variable), and tracing nested output, with a fully worked example.
- Topic 4.5 Informal Code Analysis: determine the number of times a statement executes in a loop or nested loop by counting iterations, without using formal big-O notation.
A focused answer to AP CSA Topic 4.5, covering how to count statement executions in single and nested loops, the effect of step size and start/end bounds, conditional statements inside loops, and triangular versus rectangular counts, with a fully worked counting example.
- Topic 4.3 Developing Algorithms Using Strings: traverse a String with a loop using length, substring and indexOf to implement standard algorithms such as counting characters, searching for a pattern and building a new String.
A focused answer to AP CSA Topic 4.3, covering String traversal with a for loop, extracting one character with substring, the standard counting, searching and accumulation patterns, the half-open index range, and bounds safety, with a fully worked example.
- Topic 1.3 Expressions and Assignment Statements: evaluate arithmetic expressions using operator precedence, integer division and the modulo operator, and assign results to variables.
A focused answer to AP CSA Topic 1.3, covering arithmetic operators, operator precedence, integer division truncation, the modulo operator, and how assignment statements store results, with a fully traced worked evaluation.
Sources & how we know this
- AP Computer Science A Course and Exam Description — College Board (2025)