Sub topic 3
Run once before testing with do-while
A loop repeats a set of statements. A do-while loop runs those statements first, then checks whether to repeat them. This order matters: the loop runs its body at least once, even if its condition is false at the start.
Use do-while when the first run is needed before you can decide whether to run again. For example, a program might ask a user for an answer, then check whether the answer is valid.
This chapter assumes you have seen Java variables, Boolean expressions, and while loops.
The parts of a do-while loop
A condition is an expression that is either true or false. The body is the group of statements inside the braces. One run through the body is an iteration.
In a do-while loop, Java follows this order:
- Run the body.
- Test the condition.
- If the condition is
true, run the body again. - If the condition is
false, continue after the loop.
A regular while loop tests its condition before the body. A do-while loop tests it after the body.
Count from zero
Save this program in a file named Main.java:
public class Main {
public static void main(String[] args) {
int count = 0;
do {
System.out.println(count);
count++;
} while (count < 3);
}
}Compile and run it:
javac Main.java
java MainThe program prints 0, 1, and 2, each on its own line.
Here is what each part does:
int count = 0;creates a whole-number variable namedcountand gives it a starting value of0.dostarts the loop body.System.out.println(count);prints the current value ofcount.count++;adds1tocount.while (count < 3);tests whethercountis less than3.
Notice the semicolon after while (count < 3). It is a required part of the do-while statement.
Trace the loop
To trace a loop is to follow its steps and track values as they change. In this example, the condition is tested after count increases.
| Iteration | Value printed | Value after `count++` | Is `count < 3`? |
|---|---|---|---|
| 1 | 0 | 1 | True |
| 2 | 1 | 2 | True |
| 3 | 2 | 3 | False |
After the third test, the condition is false, so the loop ends. The program does not print 3: it reaches that value after the last print statement, then stops at the condition.
Try a condition that starts false
Change the starting value in the program from 0 to 3. Leave the rest of the program unchanged, then compile and run it again.
The program prints 3 once. Here is why:
countstarts at3.- The body prints
3, then increasescountto4. - Java tests
4 < 3. The result is false, so the loop ends.
The expression count < 3 would already have been false when count was 3. But do-while does not test it before the first run.
Choose a regular while loop instead when the body must be skipped if the condition is false at the start.
Avoid common mistakes
A loop needs a way to reach its stopping point. In the counting program, count++ moves count toward 3. Without that statement, count stays at 0, so count < 3 stays true. The program would keep printing until it is stopped. This is an infinite loop: a loop that does not end on its own.
Also check the boundary of the condition. A boundary is the value where the result of a comparison changes. With count < 3, the loop stops when count reaches 3. If you change the condition to count <= 3, it also prints 3 before stopping.
Finally, remember the semicolon after the condition. Leaving it out causes a Java error, and the program will not compile.
Practice
Use the counting program above for these questions. Treat each change as separate from the others.
Exercise
If count starts at 5 and the condition stays count < 3, what does the program print?
Show answer
It prints 5 once. The body runs before Java tests the condition.
Exercise
If count starts at 1 and the condition stays count < 3, which values does the program print?
Show answer
It prints 1 and 2. After printing 2, count increases to 3, so the condition is false.
Exercise
Why does the original loop not stop if count++ is removed?
Show answer
Count stays at 0, so count < 3 remains true after every iteration.