While loops
Design a loop that makes progress toward a clear stopping condition.
On this page
The concept
A while loop checks its condition before every iteration. It may execute zero times. Unlike for, it is driven by state rather than a supplied sequence. Identify the initial state, the continuation condition, and the update that eventually makes the condition false.
The digit-counting example repeatedly removes the final decimal digit using floor division by ten. It assumes a positive integer: zero and negative numbers need a separately defined policy. This is a useful reminder that a working loop still has an input contract.
Worked example
counter = 1
while counter <= 5:
print("Attempt:", counter)
counter += 1
number = 1234
digit_count = 0
while number > 0:
digit_count += 1
number //= 10
print("Digit count:", digit_count)Trace the result
- counter begins at 1 and increases after each printed attempt.
- When counter becomes 6, the condition fails and the first loop ends.
- For 1234, floor division produces 123, 12, 1, and 0; digit_count increases four times.
Attempt: 1
Attempt: 2
Attempt: 3
Attempt: 4
Attempt: 5
Digit count: 4Line-by-line walkthrough
Line 1: counter = 1
Evaluate 1, then bind or update counter with the result. This literal is prepared example data. Assignment itself does not imply a deep copy.
Lines 2–4: while counter <= 5:
Check counter <= 5 before each pass. The loop runs only while the condition is truthy. Identify the state update or exit that makes it stop; its body may execute zero times.
Line 3: print("Attempt:", counter)
Evaluate the print arguments from the current state and display them. sep controls separators and end controls the ending when those options are provided. Displaying a value does not return that value from the surrounding function.
Line 4: counter += 1
Update the current target using counter += 1. Trace its value before and after this line; later statements see the updated state.
Line 5: number = 1234
Evaluate 1234, then bind or update number with the result. This literal is prepared example data. Assignment itself does not imply a deep copy.
Line 6: digit_count = 0
Evaluate 0, then bind or update digit_count with the result. This literal is prepared example data. Assignment itself does not imply a deep copy.
Lines 7–9: while number > 0:
Check number > 0 before each pass. The loop runs only while the condition is truthy. Identify the state update or exit that makes it stop; its body may execute zero times.
Line 8: digit_count += 1
Update the current target using digit_count += 1. Trace its value before and after this line; later statements see the updated state.
Line 9: number //= 10
Update the current target using number //= 10. Trace its value before and after this line; later statements see the updated state.
Line 10: print("Digit count:", digit_count)
Evaluate the print arguments from the current state and display them. sep controls separators and end controls the ending when those options are provided. Displaying a value does not return that value from the surrounding function.
Common mistake
If the state update is omitted, the condition may stay true forever. A retry loop should also have an explicit maximum attempt count.