Sub topic 1
How loops repeat work
Why use a loop?
A loop is a way to repeat a group of instructions. Loops are useful when the same task must happen more than once. For example, you might print a message three times or check each item in a list.
Instead of writing the same instructions again and again, you write them once inside the loop. Java then runs them repeatedly.
An iteration is one pass through the repeated instructions. A loop that prints a message three times has three iterations.
A condition is a test that has a result of either true or false. A loop can use a condition to decide whether to repeat. In this chapter, the loop checks its condition before each iteration.
Follow one loop
This program prints three numbers. It then prints Done after the loop finishes.
public class LoopDemo {
public static void main(String[] args) {
int count = 0;
while (count < 3) {
System.out.println(count);
count++;
}
System.out.println("Done");
}
}Save the program in a file named LoopDemo.java. You need the Java Development Kit (JDK), which provides tools to prepare and run Java programs. In a terminal, a window where you type commands, run javac LoopDemo.java, then run java LoopDemo.
The program produces these lines, in order:
012Done
The name count is a variable: a named place that holds a value. The line int count = 0; gives it the starting value 0. The word int means it holds a whole number. Here, count is also a counter: a variable used to track repetition.
The condition count < 3 asks whether count is less than 3. The symbol < means “less than.” This condition is a Boolean expression: an expression whose result is either true or false.
The instructions inside the braces after while form the loop body. The body has two jobs:
- Print the current value of
count. - Add
1tocountwithcount++.
After those jobs, Java checks the condition again. When the condition is false, Java leaves the loop and prints Done.
Trace the steps
To trace a program is to follow its steps and record what happens. Start with count at 0. Each row below shows a condition check.
| Check | Value of `count` | Is `count < 3` true? | What happens? |
|---|---|---|---|
| 1 | 0 | Yes | Print `0`, then change `count` to `1`. |
| 2 | 1 | Yes | Print `1`, then change `count` to `2`. |
| 3 | 2 | Yes | Print `2`, then change `count` to `3`. |
| 4 | 3 | No | Leave the loop and print `Done`. |
The first three checks lead to iterations. The last check does not: because the condition is false, Java does not run the body again.
This diagram shows the same path. Each time the body finishes, Java returns to the condition.
The order matters. The program prints count before it adds 1. On the first iteration, it prints 0, not 1.
What makes the loop stop?
After each iteration, count has a larger value. Eventually it reaches 3, so count < 3 becomes false. The loop then ends, and the program continues with the instruction after it.
The loop does not print 3. When count is 3, the condition is already false, so the body does not run.
The starting value matters too. If you change int count = 0; to int count = 3;, the first condition check is false. The loop body runs zero times, and the program prints only Done.
A loop needs a way for its condition to become false if it is meant to finish. In this example, count++ makes progress toward that point. Without it, count would stay at 0, and the condition would keep being true.
An infinite loop is a loop that never ends on its own. If you run a program that keeps printing, you can usually stop it by pressing Ctrl+C in the terminal.
Check your understanding
Exercise
In the example, change the starting value to 1. Which numbers print, and what value does count have when the loop ends?
Show answer
The loop prints 1 and 2. It ends when count is 3.
Exercise
If the starting value is 4, how many times does the loop body run? What does the program print?
Show answer
The body runs zero times because 4 < 3 is false. The program prints only Done.