AP Computer Science A · Unit 2 lecture · Part 7 of 12 · 8 min

2.8 for loopsCounting loops on one line

Mr. Thompkins
Most loops count: do this 10 times, or once for every letter. A for loop packs the three parts of a counting loop into one line, so you can see the whole plan at a glance.

A for loop header has three parts, separated by semicolons: for (initialization; condition; update). The initialization runs once, before anything else. The condition is checked before every pass, exactly like a while loop. The update runs after every pass, and then the condition is checked again.

for (int i = 0; i < 4; i++)int i = 0i < 4 ?i++1. runs once2. before each pass4. after each passtrue3. bodyfalseafter the loopbody runs with i = 0, 1, 2, 3i < 4 is checked 5 times
The order a for loop runs: 1 once, then 2, 3, 4, 2, 3, 4, and so on, until the check in step 2 is false.

A variable declared in the initialization, like int i, exists only inside the loop. After the loop ends, using i doesn't compile. If you need the final value, declare the variable before the loop.

Every for loop can be rewritten as a while loop with the same three parts: put the initialization before the loop, the condition in the while, and the update at the end of the body. for (int i = 0; i < n; i++) { ... } is int i = 0; while (i < n) { ...; i++; }. The two run exactly the same passes.

?Wait, what does i++ do?
It adds 1 to i: it's short for i = i + 1 (Unit 1, compound assignment). In a for header you'll also see i--, i += 2 and i -= 3. On the AP exam, ++ and -- always appear as their own statement or as the update, never inside a bigger expression.
1for (int i = 1; i <= 5; i++)12{3    System.out.print(i * i + " ");4}5System.out.println();6for (int k = 10; k > 0; k -= 3)27{8    System.out.print(k + " ");9}10System.out.println();
  1. line 1 <= includes 5: five passes
  2. line 6 Counting down by 3: 10, 7, 4, 1, then -2 fails
output › 1 4 9 16 25 10 7 4 1
Two for loops, run under Java 17. Each print adds a space after the number, and println ends the line.

Counting passes without tracing is a skill the exam rewards. for (int i = a; i < b; i++) runs b - a times. With <=, for (int i = a; i <= b; i++) runs b - a + 1 times. For other steps, list the values: for (int i = 3; i < 20; i += 4) uses 3, 7, 11, 15 and 19, so it runs 5 times.

?Wait, why b - a and not b - a + 1?
Count a small case. for (int i = 2; i < 5; i++) uses 2, 3 and 4: that's 3 passes, and 5 - 2 = 3. The < stops before 5. With <=, 5 is included too, so you get one more: 4 passes, which is 5 - 2 + 1.
Pause and think
How many times does for (int i = 0; i <= 10; i += 2) run, and what values does i take?
I thought about it. Show me
6 times, with i = 0, 2, 4, 6, 8 and 10. The next value, 12, fails i <= 10. Counting a list of values is always safe when the step isn't 1.
Passes and totals

What is printed?

int sum = 0;
int passes = 0;
for (int k = 10; k > 0; k -= 3)
{
    sum += k;
    passes++;
}
System.out.println(sum + " " + passes);
  1. Initialization: k is 10. 10 > 0, so pass 1: sum is 10, passes is 1. Update: k is 7.
  2. 7 > 0: pass 2, sum is 17, passes is 2. k is 4. 4 > 0: pass 3, sum is 21, passes is 3. k is 1.
  3. 1 > 0: pass 4, sum is 22, passes is 4. k is -2. -2 > 0 is false, so the loop ends.
    Why?
    The update always runs after the last pass, and then the condition fails. That's why the loop variable ends one step past the last value used.
  4. It prints 22 4.

Answer: 22 4

Stuck? Get a hint
  1. List the values k takes: start at 10, subtract 3 each time.
  2. 10, 7, 4, 1. Is -2 > 0?
  3. Add the four values for sum, and count them for passes.
Check yourself
A loop should print the numbers 1 through 10, one per line. Which header does that?
  1. Afor (int i = 1; i < 10; i++)
  2. Bfor (int i = 0; i <= 10; i++)
  3. Cfor (int i = 1; i <= 10; i++)
  4. Dfor (int i = 0; i < 10; i++)
Show the answer

C. Starting at 1 and stopping after 10 needs i <= 10 (10 passes). Choice A stops at 9. Choice B starts at 0, printing 11 numbers. Choice D runs 10 times, but prints 0 through 9 unless you print i + 1. Off-by-one errors live in exactly these differences.

  • for (init; condition; update): init once, condition before every pass, update after every pass.
  • for (int i = a; i < b; i++) runs b - a times; with <=, b - a + 1 times.
  • A variable declared in the header doesn't exist after the loop.
  • Any for loop can be rewritten as a while loop with the same three parts.