A loop with its controls in one place
The while loop of Topic 2.7 needs three ingredients: an initial value, a test and an update. In a while loop they are scattered: the initial value is above the loop, the test is in the brackets, and the update is hidden somewhere in the body. A for loop brings all three together in one header, which makes counting loops easier to read and harder to get wrong.
A for loop is a type of iterative statement. The header has three parts, separated by semicolons:
for (initialization; Boolean expression; update) {
loop body
}
- The initialization declares and gives a starting value to the loop control variable, for example
int i = 0. - The Boolean expression decides whether the body runs, for example
i < 5. - The update changes the loop control variable, for example
i++.
The order of execution
This is the fact you must know exactly.
- The initialization is executed once, before the first Boolean expression is evaluated.
- The Boolean expression is evaluated. If it is false, the loop ends.
- If it is true, the entire body executes.
- The update is executed.
- Go back to step 2.
In each iteration the update runs after the whole body and before the Boolean expression is evaluated again.
public class ForBasics {
public static void main(String[] args) {
for (int i = 0; i < 3; i++) {
System.out.println("i = " + i);
}
System.out.println("finished");
}
}
Output:
i = 0
i = 1
i = 2
finished
Trace: i starts at 0 and 0 < 3 is true, so the body prints and i++ makes it 1. Then 1, then 2. After the body with i = 2, the update makes i equal to 3; 3 < 3 is false and the loop ends. The body ran 3 times, for the values 0, 1 and 2.
Counting up, counting down, stepping
The update can be anything that moves the variable toward a false test.
| Header | Values of the loop variable | Iterations |
|---|---|---|
for (int i = 0; i < 5; i++) |
0, 1, 2, 3, 4 | 5 |
for (int i = 1; i <= 5; i++) |
1, 2, 3, 4, 5 | 5 |
for (int i = 10; i > 0; i--) |
10, 9, …, 1 | 10 |
for (int i = 2; i <= 20; i += 6) |
2, 8, 14, 20 | 4 |
for (int i = 0; i < 10; i += 3) |
0, 3, 6, 9 | 4 |
To count iterations, list the values for which the test is true. The most common form, for (int i = 0; i < n; i++), runs exactly n times, with i from 0 to n - 1.
Accumulating inside a for loop
int sum = 0;
for (int i = 1; i <= 4; i++) {
sum = sum + i * i;
}
System.out.println(sum);
Output:
30
The loop adds 1, 4, 9 and 16, giving 30. The accumulator sum is declared before the loop so that it is still available afterwards.
A for loop is a while loop in disguise
Any for loop can be rewritten as an equivalent while loop, and the reverse. The mapping is mechanical.
for loop |
Equivalent while loop |
|---|---|
for (int i = 1; i <= 3; i++) { body} |
int i = 1;while (i <= 3) { body i++;} |
Put the initialization before the loop, the Boolean expression in the while, and the update as the last statement of the body. The one difference is scope: a variable declared in the for header exists only inside the loop, whereas the while version leaves i visible after the loop (with the value that failed the test).
public class ForWhile {
public static void main(String[] args) {
for (int i = 1; i <= 3; i++) {
System.out.print(i + " ");
}
System.out.println();
int j = 1;
while (j <= 3) {
System.out.print(j + " ");
j++;
}
System.out.println();
System.out.println("j after the loop: " + j);
}
}
Output:
1 2 3
1 2 3
j after the loop: 4
Scope of the loop control variable
If the variable is declared in the header, it exists only for the loop. Using it afterwards is a compile-time error:
for (int i = 0; i < 3; i++) {
System.out.println(i);
}
System.out.println(i);
The last line does not compile: i is out of scope. If you need the final value, declare the variable before the loop (int i; or int i = 0;) and leave the initialization out of the header (or use it as the first part of the header without int).
Errors to watch for
A semicolon after the header.
for (int i = 0; i < 5; i++);
{
System.out.println("x");
}
Output:
x
The semicolon is an empty statement and forms the whole loop body. The loop spins five times doing nothing, then the block runs once.
Changing the loop control variable inside the body. This is legal but easy to misjudge.
for (int i = 0; i < 10; i++) {
System.out.print(i + " ");
i += 2;
}
Output:
0 3 6 9
Trace: i = 0, print 0, body makes i 2, update makes 3. Print 3, body makes 5, update 6. Print 6, body 8, update 9. Print 9, body 11, update 12, and 12 < 10 is false. The body ran 4 times, not 10.
< versus <=. for (int i = 1; i < 5; i++) visits 1 to 4; for (int i = 1; i <= 5; i++) visits 1 to 5. The boundary decides whether the last value is included.
Worked example 1: counting and tracing
Question. (a) How many times does the body of for (int i = 1; i <= 20; i += 4) run? (b) What does the loop below print, and what is the value of total afterwards?
public class StepSum {
public static void main(String[] args) {
int total = 0;
for (int i = 1; i <= 20; i += 4) {
total += i;
System.out.print(i + " ");
}
System.out.println();
System.out.println("total = " + total);
}
}
Output:
1 5 9 13 17
total = 45
Solution. (a) The values of i for which i <= 20 is true are 1, 5, 9, 13 and 17. The next value, 21, is not at most 20, so the loop ends. The body runs 5 times.
(b) Trace:
| Iteration | i at the test |
i <= 20? |
total after the body |
|---|---|---|---|
| 1 | 1 | true | 1 |
| 2 | 5 | true | 6 |
| 3 | 9 | true | 15 |
| 4 | 13 | true | 28 |
| 5 | 17 | true | 45 |
| (test) | 21 | false | loop ends |
Check. 1 + 5 + 9 + 13 + 17 = 45. The values form an arithmetic sequence with 5 terms, first 1 and last 17, so the sum is 5 x (1 + 17) / 2 = 45.
Worked example 2: rewriting and countdown
Question. Rewrite the following while loop as a for loop, then trace it.
int c = 10;
while (c > 0) {
System.out.print(c + " ");
c -= 3;
}
Output:
10 7 4 1
Solution. The initialization is int c = 10, the Boolean expression is c > 0, and the update is c -= 3. As a for loop:
for (int c = 10; c > 0; c -= 3) {
System.out.print(c + " ");
}
Output:
10 7 4 1
Trace: c takes the values 10, 7, 4, 1, and then -2, for which -2 > 0 is false. So the body runs 4 times and the output is 10 7 4 1. Check: the number of iterations is the number of values in the sequence 10, 7, 4, 1, and the next value, -2, is not positive. If we had written c >= 0, the sequence would still stop at -2, because it never hits exactly 0, which is a reminder that an inequality such as >= is safer than testing for an exact value.
How this appears in exam questions
Because a for loop packs its controls into one line, questions about it often probe the order of execution and the scope of the variable. Expect to be asked how many times a body runs for a header with a step other than 1, what a loop prints when the body also changes the control variable, or why a segment does not compile or does not behave as intended (a variable used after the loop, a semicolon after the header, a loop that counts the wrong way). You may also be asked to rewrite a for loop as an equivalent while loop. Practise reading a header aloud in the order it executes: set, test, run the body, update, test again. Writing the sequence of values of the control variable before you answer is the quickest way to avoid an off-by-one slip.
Common misconceptions
- “The update runs before the first iteration.” It runs after the body, so the first iteration uses the initial value.
- “The initialization runs on every iteration.” It runs once.
- “A for loop is faster than a while loop.” They are equivalent in behaviour; the for loop is simply a tidier way to write a counting loop.
- “The loop variable can be used anywhere afterwards.” If declared in the header, it is out of scope after the loop.
- “
i < nandi <= nboth run n times.”i < nfrom 0 runs n times;i <= nfrom 0 runs n + 1 times. - “A semicolon after the header is harmless.” It becomes an empty loop body, and the block after it runs once.
- “Changing the loop variable inside the body is the same as changing the update.” It also affects the test, so the number of iterations changes in a way that is easy to misjudge. Trace such loops carefully.
Where this leads
In Topic 2.9 you will use loops (both kinds) to write standard algorithms: finding divisors, extracting digits, counting, summing, finding a minimum or maximum. In Topic 2.11 you will put one loop inside another. Practise now with the practice questions, recap with the revision notes and tick off the checklist. Then read the Topic 2.9 study guide. For loops that are not counting loops, revisit the Topic 2.7 study guide.
Quick questions
When does the update statement in a for loop run?
After the entire loop body has run and before the Boolean expression is tested again. It never runs before the first iteration.
How do I count the iterations of a for loop?
List the values of the loop control variable for which the Boolean expression is true. For example, for (int i = 2; i <= 20; i += 6) has i equal to 2, 8, 14 and 20, so 4 iterations.
Why does a semicolon after the for header cause a bug?
The semicolon is an empty statement and becomes the whole loop body. The loop runs its header all the way through doing nothing, and the block after it runs once.
Can I use the loop variable after the loop?
Not if it was declared in the header: its scope is the loop. Declare the variable before the loop if you need its final value afterwards.