So far every line you have written runs at most once. That is a serious limit: a program that adds up twenty marks would need twenty nearly identical lines, and a program that keeps asking until the user types something valid could not be written at all. A loop solves both. It is a block of code the computer runs over and over while some condition stays true, so one block of code can do a job any number of times — including a number you do not know until the program is already running.
The while loop
A while loop reads almost like English: while this is true, keep doing this. Java checks the condition first, and only then runs the body. If the condition is false the very first time, the body never runs at all — not even once. That is the defining behaviour of while, and it is exactly what you want whenever "zero times" is a legitimate answer.
public class CountUp {
public static void main(String[] args) {
int count = 1; // 1. initialize
while (count <= 5) { // 2. condition
System.out.println(count);
count++; // 3. update
}
System.out.println("Done");
}
}| count before the test | count <= 5 | Printed | count after count++ |
|---|---|---|---|
| 1 | true | 1 | 2 |
| 2 | true | 2 | 3 |
| 3 | true | 3 | 4 |
| 4 | true | 4 | 5 |
| 5 | true | 5 | 6 |
| 6 | false | nothing — the loop ends | 6 |
The do-while loop
int count = 10;
do {
System.out.println(count);
count++;
} while (count <= 5); // semicolon is required here- The condition is checked before the body
- The body runs zero or more times
- Use it when there may be nothing to do at all
- Reading a file that might be empty
- Repeating while a total stays under a limit
- The condition is checked after the body
- The body runs one or more times
- Use it when the first pass must happen regardless
- Showing a menu, then asking whether to show it again
- Asking for input, then checking whether it was valid
Sentinel and validation loops
Often you do not know how many values the user will enter — that is their choice, not yours. The standard solution is a sentinel: an agreed stop value meaning "I am finished" rather than being real data. It must be a value real data could never take, which is why 0 works for a list of positive prices but would be a terrible sentinel for a list of Ontario winter temperatures. The loop condition tests the value, so the value must already exist before the first test — that first read is called the priming read, and the matching read at the bottom of the body is the update.
import java.util.Scanner;
public class SumUntilZero {
public static void main(String[] args) {
Scanner input = new Scanner(System.in);
int total = 0;
System.out.print("Enter a number (0 to stop): ");
int value = input.nextInt(); // priming read
while (value != 0) {
total = total + value;
System.out.print("Enter a number (0 to stop): ");
value = input.nextInt(); // update: read the next one
}
System.out.println("Total: " + total);
}
}Scanner input = new Scanner(System.in);
int mark;
do {
System.out.print("Enter a mark from 0 to 100: ");
mark = input.nextInt();
} while (mark < 0 || mark > 100);
System.out.println("Accepted: " + mark);