While
After this: Repeat for as long as a condition is true, and recognise a loop that never ends.
On this page · 6 sections · 4 checks
Start with 100. Print it, halve it, and keep going until nothing is left. How many passes is that? You could work it out with a pencil, but you should not have to. What you know is the stopping rule: keep going while the number is bigger than 0.
The loops so far needed a row of values or a start and an end number. This lesson adds a loop that needs only a condition.
Repeat while a condition is true
A has a condition, exactly like an if. Before each pass the computer checks the condition. If it is true, the lines run once more, and the computer checks again. If it is false, the loop ends and the program moves on to the line below it.
The smallest example, in the trace below, is a countdown: 3, 2, 1, Go. Read its code, then work through it.
nis 3. Isn > 0? Yes. Print 3, then makenone smaller: 2.- Is 2 bigger than 0? Yes. Print 2, and
nbecomes 1. - Is 1 bigger than 0? Yes. Print 1, and
nbecomes 0. - Is 0 bigger than 0? No. The loop ends, and Go is printed.
n = 3while n > 0: print(n) n = n - 1print("Go")run the program to see its boxes
nothing printed yetPress Run it, then step through the program one line at a time.
- lines
- 0
- prints
- 0
- calls
- 0
The trace narrates the end of a while loop the way it narrates an if whose answer was no: "The condition was false, so the program jumps to line 58".
When you do not know the count
Back to the halving. n starts at 100. Each pass prints n and then halves it with whole-number division, which throws the fraction part away: /// on two whole numbers, from Your first program.
| Pass | printed | n after the pass |
|---|---|---|
| 1 | 100 | 50 |
| 2 | 50 | 25 |
| 3 | 25 | 12 |
| 4 | 12 | 6 |
| 5 | 6 | 3 |
| 6 | 3 | 1 |
| 7 | 1 | 0 |
Before an eighth pass the computer checks 0 > 0, which is false. Seven numbers are printed, and nobody had to count them in advance.
- Run the programRun it. Before each pass the condition is checked. Count the passes, and watch what happens when n reaches 0.
n = 100while n > 0: print(n) n = n // 2print("Done")run the program to see its boxes
nothing printed yetPress Run it, then step through the program one line at a time.
- lines
- 0
- prints
- 0
- calls
- 0
The rule: a while loop checks its condition before every pass, including the first. If the condition is false the first time, the lines never run at all. And something inside the loop must change what the condition looks at, or the answer can never become no.
Run it
Change 100 to 1000 and run again. Count the numbers.
The loop that never ends
Look at the countdown again. The line n = n - 1n = n - 1; is what moves the loop towards its end. Without it, n stays 3, the condition 3 > 0 is true before every pass, and the loop never stops. This is an . It is the most common mistake with while loops, and every programmer has written one.
On this site, the Run button stops a program after five seconds and says so. Try it on purpose: in the block below, delete the line n = n - 1n = n - 1;, press Run, and read the note that appears. Then press Reset to get the line back.
Check yourself
When does a while loop stop?
What does this print?
n = 1while n < 20: print(n) n = n * 3int n = 1;while (n < 20) { System.out.println(n); n = n * 3;}Complete line 4 so that the loop counts down 3, 2, 1 and then stops. Without it, the loop would never end.
n = 3while n > 0: print(n) ____________________ # ← make n smallerprint("Go")int n = 3;while (n > 0) { System.out.println(n); ____________________ // ← make n smaller}System.out.println("Go");The line n = n - 1n = n - 1; is deleted from the countdown, so the loop is only while n > 0: print(n)while (n > 0) { System.out.println(n); }. What happens when it runs?
Honesty notes
- Write a while loop: a condition checked before every pass, and lines that run while it is true.
- Use a while loop when the number of passes is not known in advance: halving 100 prints seven numbers.
- Make sure something inside the loop changes what the condition looks at.
- Recognise an endless loop, and know that the Run button on this site stops one after five seconds.