Sub topic 7
Loops: Repeating Work
What Is a Loop?
A loop is a statement that repeats a block of code several times. Instead of writing the same line five times, you write it once inside a loop and tell Java how many times to repeat it. Loops matter because most programs repeat work: processing every item in a list, retrying a download, or counting down a timer.
Java has three main loops: for, while, and do-while.
The for Loop
A for loop repeats a block a fixed number of times. It has three parts in its header: a start, a condition that is checked before each round, and a step that runs after each round.
public class ForLoop {
public static void main(String[] args) {
for (int i = 1; i <= 5; i++) {
System.out.println("Round " + i);
}
}
}Output:
Round 1
Round 2
Round 3
Round 4
Round 5Reading the header: int i = 1 creates a counter starting at 1. Before each round, Java checks i <= 5. If true, the body runs. Then i++ adds 1 to i.
The while Loop
A while loop repeats as long as a condition stays true. Use it when you don't know in advance how many rounds you need.
public class WhileLoop {
public static void main(String[] args) {
int cups = 3;
while (cups > 0) {
System.out.println("Drinking a cup. Left: " + cups);
cups = cups - 1;
}
System.out.println("All done!");
}
}The condition cups > 0 is checked first. If it is false at the start, the body never runs at all.
The do-while Loop
A do-while loop runs the body first, then checks the condition. This guarantees at least one round.
public class DoWhileLoop {
public static void main(String[] args) {
int number = 10;
do {
System.out.println("Value is " + number);
number++;
} while (number < 3);
}
}This prints Value is 10 once, then stops, because the check fails after the first round. Use do-while when the body must run at least once, such as showing a menu.
Common Mistakes
- Infinite loops. If you forget the step (like
i++), the condition never becomes false and the program runs forever. Press Ctrl+C in the terminal to stop it. - Off-by-one errors.
i <= 5runs 5 times (1 through 5), buti < 5runs only 4 times. Decide carefully which you want. - Semicolon after the loop.
while (x < 5);with a semicolon creates an empty body, and the loop does nothing useful (or never ends).
Picking a Loop
| Situation | Loop to use |
|---|---|
| Known number of rounds | `for` |
| Unknown rounds, check first | `while` |
| Body must run at least once | `do-while` |
How the Three Loops Compare
The left side is a while loop: the check comes first, so the body may run zero times. The right side is a do-while: the body runs first, so it always runs at least once.
Loops Build Toward Arrays
Loops and arrays (collections of values stored in order) work together: a loop visits each element one by one. That pairing is the next chapter's topic.
Exercise
How many times does the body of for (int i = 2; i <= 6; i += 2) run?
Show answer
Three times, when i is 2, 4, and 6. The condition fails when i becomes 8.
Exercise
Which loop should you use to ask a user for a password until they enter a valid one, ensuring the question is asked at least once?
Show answer
A do-while loop, because its body always runs once before the condition is checked.