Article
While loops
You pour water into a glass until it is full. How many pours? You do not know. You keep going while the glass is not full. Some loops work this way: they do not repeat a set number of times. They repeat as long as something is true.
Keep going while it is true
Python has a loop for this, called a while loop. After the word while comes a condition, which is a Boolean expression that is either true or false (like n > 0). The body runs again and again as long as the condition is true.
Run it. It prints 3, 2, 1 and then Go!. The computer checks the condition before every pass, including the first one:
| Check | n | Is n > 0? | What happens |
|---|---|---|---|
| 1 | 3 | true | show 3, then n becomes 2 |
| 2 | 2 | true | show 2, then n becomes 1 |
| 3 | 1 | true | show 1, then n becomes 0 |
| 4 | 0 | false | the loop ends, and the program goes on to Go! |
A good while loop has three parts:
- A starting value before the loop (
n = 3). - A condition that is true while the work is not done (
n > 0). - A change inside the body that moves the condition toward false (
n = n - 1).
Check your understanding
A Python while loop checks its condition before every pass. When does the loop stop?
It might not run at all, or it might never stop
Because the check comes first, a while loop can run zero times. If n starts at 0, then n > 0 is false straight away, and nothing is printed.
The opposite problem is a loop that never stops. This is an infinite loop. It happens when the condition stays true forever, usually because the body forgets to change the variable:
n = 3
while n > 0:
print(n)This prints 3 again and again, because n never changes, so n > 0 is always true. Do not run it. The fix is the missing line n = n - 1. When you write a while loop, always ask: "What line makes the condition false in the end?"
Check your understanding
What happens when this Python code runs?
n = 5
while n > 0:
print(n)Worked example: doubling past 100
Start at 1 and keep doubling while the amount is 100 or less. How many doublings do you need? You cannot tell without trying, which is why this is a while loop.
The amount goes 1, 2, 4, 8, 16, 32, 64. At 64 the condition 64 <= 100 is true, so it doubles to 128. Then 128 <= 100 is false and the loop ends. It prints 128.
The AP exam has no while. It has REPEAT UNTIL, which you will meet in the next article. Here is the same program. Look at the condition:
amount ← 1
REPEAT UNTIL(amount > 100)
{
amount ← amount * 2
}
DISPLAY(amount)Python says when to keep going (amount <= 100). AP says when to stop (amount > 100). The two conditions are opposites.
Which loop should I use?
| You know... | Use | Python |
|---|---|---|
| exactly how many passes | REPEAT n TIMES | for i in range(n) |
| only when to stop | REPEAT UNTIL(...) | while ... (opposite condition) |
Common mistakes
- Forgetting to change the variable. The condition never becomes false and the loop runs forever.
- Changing it in the wrong direction.
n = n + 1in the countdown makesn > 0stay true forever. - Thinking the check happens at the end. It happens before every pass, so a loop can run zero times.
- Thinking the loop ends the moment the condition becomes false in the middle of the body. The rest of that pass still finishes. The condition is only checked again at the top.
- Writing the condition for stopping. In Python,
whileneeds the condition for continuing.
On the exam
The exam never shows while. It tests the same idea with REPEAT UNTIL. Still, learn Python's version well: it is the one you will type in the Create task, and the only change you need for the exam is to flip the condition.
Key words
While loop: a Python loop that repeats its body while a condition is true.
Condition: a Boolean expression, true or false, that decides whether to go on.
Infinite loop: a loop that never ends because its condition never changes to stop it.