Sub topic 4
Initialize, test, and update with for
A for loop repeats instructions while a condition is true. It is useful when you know how a count should start, when it should stop, and how it should change each time.
Each pass through the repeated instructions is an iteration. For example, a loop that prints three numbers has three iterations.
Write a counting loop
Save this program in a file named ForDemo.java:
public class ForDemo {
public static void main(String[] args) {
for (int i = 0; i < 3; i++) {
System.out.println(i);
}
}
}Compile and run it with these commands:
javac ForDemo.java
java ForDemoThe program prints these numbers, each on its own line:
012
The line starting with for is the loop header. It has three parts, separated by semicolons:
- Initialization:
int i = 0creates an integer variable namediand gives it the starting value0. An integer is a whole number. - Condition:
i < 3checks whetheriis less than3. A condition is an expression that is either true or false. - Update:
i++adds1toiafter each iteration.
The code between the braces is the loop body: the instructions that repeat. Here, System.out.println(i) prints the current value of i.
Follow the order
The initialization runs once, before the loop begins. Then Java tests the condition. If it is true, Java runs the body and then runs the update. It tests the condition again and repeats this process.
This table traces the example. To trace code means to follow its values and actions step by step.
| Value of `i` at the test | Is `i < 3` true? | Body | Value after update |
|---|---|---|---|
| `0` | Yes | Prints `0` | `1` |
| `1` | Yes | Prints `1` | `2` |
| `2` | Yes | Prints `2` | `3` |
| `3` | No | Does not run | No update |
When i reaches 3, the condition is false. The loop ends without printing 3.
Change the start and end
The three parts of the header work together. To count from 1 through 3, replace the header in the program with:
for (int i = 1; i <= 3; i++)
Compile and run the program again. It prints 1, 2, and 3, each on its own line.
The condition i <= 3 means “i is less than or equal to 3.” That lets the body run when i is 3. On the next test, i is 4, so the condition is false.
Compare these conditions:
| Condition | Meaning | Last value printed when counting up from `0` by `1` |
|---|---|---|
| `i < 3` | Less than `3` | `2` |
| `i <= 3` | Less than or equal to `3` | `3` |
The difference is whether 3 is included. Check that choice whenever you write a loop.
Change the update
An update does not have to add 1. For example, replace the header with:
for (int i = 0; i < 6; i += 2)
The update i += 2 adds 2 to i each time. The program prints 0, 2, and 4. After 4 is printed, the update changes i to 6. The condition is then false.
You can count down instead:
for (int i = 3; i >= 1; i--)
Here, i >= 1 means “i is greater than or equal to 1,” and i-- subtracts 1. The program prints 3, 2, and 1.
Choose an update that moves the count toward a value where the condition becomes false. That gives the loop a way to finish.
Practice
Exercise
What does for (int i = 2; i <= 6; i += 2) { System.out.println(i); } print?
Show answer
It prints 2, 4, and 6 on separate lines. After printing 6, the update changes i to 8, so the condition is false.
Exercise
What header would count from 1 through 4, adding 1 each time?
Show answer
for (int i = 1; i <= 4; i++). The condition includes 4, and the update adds 1 after each iteration.