Loops: for, while, and do-while
Laps around a running track: you know when you start, you know what counts as one lap, and you know when to stop.
Say you want to print the numbers 1 to 5. You could write five println lines. Now say you want 1 to 1,000. Or you want to keep asking a user for a password until they get it right, and you have no idea how many tries that will take.
Copying lines doesn't work anymore. You need a way to say "do this again, and again, until...". That's a loop, and Java has three kinds.
Laps around a track
Picture a coach timing you around a running track. Before you start, the coach sets the lap counter to zero. Before each lap, the coach checks: "Have you done 5 yet?" If not, you run another lap, and the counter goes up by one. When the answer is yes, you stop.
Every loop has those same three ingredients:
- A starting point, like the lap counter at zero.
- A condition that's checked before every lap. While it's
true, the loop keeps going. - A step that moves things forward, so the condition eventually becomes
false.
Forget the third one, and you'll be running laps forever. Programmers call that an infinite loop.
The while loop
The simplest loop is while. It repeats its block as long as a condition is true:
int lap = 1;
while (lap <= 5) {
System.out.println("Lap " + lap);
lap++;
}
System.out.println("Done!");Step by step:
lapstarts at 1.- Java checks
lap <= 5. It's true, so the block runs: it printsLap 1, andlapbecomes 2. - Back to the top.
2 <= 5is true, so it printsLap 2... - Eventually
lapis 6.6 <= 5is false, so the loop ends andDone!prints.
Each pass through the block is called an iteration. This loop has five.
The for loop
Counting loops like that are so common that Java has a special shape for them. A for loop puts all three ingredients on one line:
for (int lap = 1; lap <= 5; lap++) {
System.out.println("Lap " + lap);
}Inside the parentheses, separated by semicolons:
int lap = 1is the start. It runs once, before anything else.lap <= 5is the condition, checked before every iteration.lap++is the step, run after every iteration.
This does exactly the same thing as the while loop above, but everything that controls the loop is in one place, where you can see it at a glance.
Programmers usually name a plain counter i (for "index"), and usually start counting at 0:
for (int i = 0; i < 5; i++) {
System.out.println("Iteration " + i);
}
// prints Iteration 0 through Iteration 4i = 0; i < 5 runs exactly five times, with i taking the values 0, 1, 2, 3, and 4. You'll see this pattern everywhere, because it lines up perfectly with how strings and arrays are numbered from zero.
The step doesn't have to be ++. Count by twos, or count down:
for (int i = 2; i <= 10; i += 2) {
System.out.print(i + " ");
}
// 2 4 6 8 10
for (int i = 3; i >= 1; i--) {
System.out.println(i + "...");
}
System.out.println("Liftoff!");Looping through a string
Combine a for loop with length() and charAt() from Working with Strings, and you can visit every character:
String word = "JAVA";
for (int i = 0; i < word.length(); i++) {
System.out.println(i + ": " + word.charAt(i));
}Notice the condition is i < word.length(), with <, not <=. The last index is length() - 1.
Running totals
A very common loop job is adding things up. Create a variable before the loop to hold the total, and add to it on every iteration:
int total = 0;
for (int i = 1; i <= 100; i++) {
total += i;
}
System.out.println("1 + 2 + ... + 100 = " + total); // 5050The total has to be declared outside the loop. Declare it inside, and it would be reset to 0 on every lap. You'll see exactly why in Variable Scope.
The do-while loop
A while loop checks its condition before each lap, so if the condition starts out false, the block never runs at all. Sometimes you want the block to run at least once, no matter what. That's what do-while is for:
int number = 10;
do {
System.out.println("Number is " + number);
number++;
} while (number < 5);Even though 10 < 5 is false from the start, this prints Number is 10 once, because the check happens at the end. Note the semicolon after the while (...) at the bottom. It's required here, unlike in the other loops.
do-while is perfect for menus and input checks, where you have to ask at least once before you can know whether to ask again. This one uses a Scanner from Reading User Input with Scanner, so remember the import java.util.Scanner; line at the top of the file:
Scanner input = new Scanner(System.in);
int age;
do {
System.out.print("Enter your age (1 to 120): ");
age = Integer.parseInt(input.nextLine());
} while (age < 1 || age > 120);This keeps asking until the answer is in range.
Which loop should you use?
forwhen you know how many times to repeat: 5 laps, 100 numbers, every character in a string.whilewhen you repeat until something happens, and you don't know how many times that will take.do-whilewhen the block must run at least once, like showing a menu or asking a question.
Any of them can do any job. Picking the one that matches the situation makes your code easier to read.
Two classic bugs
The infinite loop. Forget the step, and the condition never changes:
int lap = 1;
while (lap <= 5) {
System.out.println("Lap " + lap);
// forgot lap++
}This prints Lap 1 forever. If it happens to you, don't panic. Press Ctrl + C in the terminal, or the stop button in your editor, to end the program. Then check that something inside the loop moves it toward its end.
Off by one. The loop runs one time too many or one time too few. It's the most common loop bug of all:
for (int i = 0; i <= 5; i++) {
System.out.print(i + " ");
}
// 0 1 2 3 4 5 (six numbers, not five!)When a loop runs the wrong number of times, check two things: where the counter starts (0 or 1?) and whether the condition uses < or <=. A useful habit: pick a tiny case, like 3 iterations, and trace it by hand.
Try it
This program uses all three loops: a countdown with for, a savings goal with while, and a menu that runs once with do-while. Predict every line.
Running Java here
Running Java right in the browser is coming to Syntaxia soon. On your own computer, save this program as Main.java and run it with java Main.java, or use your editor's Run button.
Check your prediction
3...
2...
1...
Go!
Sum of 1 to 5: 15
Reached 600 pesos after 4 weeks
LOOP backwards is POOL
Menu shown (choice is 0)The while loop only checks the goal before each week, so the savings overshoot 500 and stop at 600. The do-while shows the menu once even though choice != 0 is false from the start.
Try it yourself
- Change the countdown so it starts at 10 and only prints even numbers:
10... 8... 6...and so on. - Change the savings loop to save 120 pesos a week. How many weeks does it take now, and how much is saved?
- Write a
forloop that prints the 7 times table, from7 x 1 = 7to7 x 10 = 70.
Check your understanding
Up next
Sometimes you want to leave a loop early, like when you've found what you were looking for, or skip one iteration without stopping the whole loop. Java has two small keywords for that: break and continue.
