Skip to main content
United StatesComputer ScienceSyllabus dot point

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.

Generated by Claude Opus 4.811 min answer

Reviewed by: AI editorial process; not yet individually human-reviewed

Have a quick question? Jump to the Q&A page

Jump to a section
  1. What this topic is asking
  2. The for header
  3. The exact order of execution
  4. for and while are equivalent
  5. Counting iterations
  6. Try this

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 = 0 runs once, before the loop begins. The control variable declared here is scoped to the loop.
  • Condition i < 5 is 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

Sources & how we know this