Repeat as long as a question stays True: counters, sentinel input loops, trace tables, and the two ingredients that stop an infinite loop.
01
Learning objectives
Write a while loop whose condition eventually becomes False.
Identify the two stopping ingredients: a condition that can fail and a body that moves toward it.
Build counter-controlled loops (counting up and down) and predict the final counter value.
Write a sentinel-controlled input loop (e.g. stop when the user types 0 or a negative).
Trace a loop with a table (pass, condition, variable values) and count the iterations.
02
Lesson notes
The while loop is the if statement's repeating cousin. Where an if asks its question once, a while asks it again after every pass: while condition: runs the indented body, then re-checks the condition, then runs the body again — for as long as the condition stays True. The moment the re-check returns False, the loop ends and execution continues on the first line after the block. One question, asked over and over, is the entire mechanism.
That mechanism carries a warning every beginner must hear once: if the condition never becomes False, the loop never ends — the infinite loop. The classic is count = 1; while count <= 5: print(count) with nothing moving count upward: the condition is re-checked forever with the same True answer. A healthy while loop therefore has two ingredients: a condition that CAN fail, and a body that works toward that failure — usually by updating the very variable the condition reads. In the browser playground a runaway loop is killed by a time limit; on your own machine it freezes the terminal until Ctrl+C.
The most common shape is the counter-controlled loop, which repeats a known number of times by hand: start a counter before the loop, test it in the condition, update it as the last line of the body. i = 1; while i <= 5: print(i); i += 1 prints 1 through 5 — and the compound operator += (add and reassign) is the idiomatic way to write the update. Counting down is the same skeleton with i -= 1 and a reversed condition. Remember the exam favourite: after this loop ends, i is 6 — the variable keeps the value that FAILED the test.
The second great shape is the sentinel-controlled loop, which repeats an unknown number of times until a special value appears. Reading numbers until the user types 0, or lines until an empty line: the sentinel is the agreed stop signal, and the pattern reads the first value BEFORE the loop (the priming read), then re-reads at the bottom of the body. This order matters — it is the standard board question on while loops — because the condition must see a real value the first time it is checked.
Finally, the tracing discipline that turns while loops from scary to mechanical: draw a table with one row per pass and columns for the condition's verdict and each variable's value after the body. Ten seconds of table beats ten minutes of staring, and it produces the two things papers ask for — the printed output and the iteration count. Trace loops 1 and 2 below yourself before running them; the table you draw should match the console exactly.
A while loop repeats while its condition is true
03
Worked code examples
Counter-controlled: countdown with += and -=
The body runs while t > 0; t -= 1 moves t toward failing that test. When t reaches 0 the condition is False and Lift off! prints. The loop ran exactly 5 times.
t = 5
while t > 0:
print(t, "seconds left")
t -= 1
print("Lift off!")
Output
5 seconds left
4 seconds left
3 seconds left
2 seconds left
1 seconds left
Lift off!
Sentinel-controlled: read numbers until 0
The program is fed 12, 8, 5 and finally the sentinel 0. The 0 is never added — the priming read and the bottom re-read feed the condition, and the body adds only real values. Sentinel loops handle inputs of unknown length.
total = 0
n = int(input()) # priming read
while n != 0:
total += n
n = int(input())
print("Total:", total)
Output
Total: 25
Re-asking until valid — while as a guard
Fed 300 first, then 17: the condition repeats the question whenever the value is unacceptable — input validation is the most practical while loop in everyday programs.
age = int(input())
while age < 1 or age > 120:
print("Out of range, try again.")
age = int(input())
print("Accepted:", age)
Output
Out of range, try again.
Accepted: 17
04
Key terminology
iteration
One complete pass through a loop's body.
infinite loop
A loop whose condition never becomes False — usually a missing update inside the body.
counter
A variable that tracks how many passes have happened, updated each iteration.
sentinel
The special agreed value (0, -1, empty line) that ends a sentinel-controlled loop.
priming read
The input taken BEFORE the loop so the condition has a real value to test on the first check.
05
Common misconceptions
Common mistake
The condition is checked once, at the start.
Correct understanding
It is re-checked after EVERY pass of the body — that re-checking is precisely what makes it a loop.
Common mistake
An infinite loop eventually stops by itself.
Correct understanding
It never does — nothing changes the condition's verdict. One variable must move toward making the condition False.
Common mistake
After the loop ends, the counter still holds its last valid value (e.g. 5).
Correct understanding
It holds the value that FAILED the test: after while i <= 5, i is 6. Papers test this constantly.
06
Check your understanding
1. i = 1
while i <= 3:
print(i)
i += 1
How many lines does this print, and what is i afterwards?
2. What is missing from:
n = 5
while n > 0:
print(n)
3. In a sentinel loop reading numbers until -1, what is the priming read for?