While Loops: Repeating Code Until a Condition Changes
An infinite loop never terminates because its condition never becomes false.
Try it: while Loops, break and continue
How a while loop re-evaluates its condition before every pass, how break leaves the loop at once, how continue jumps back to the condition, how a sentinel value ends a while True loop, and why a loop whose condition never becomes False runs forever.
How it works
- Evaluate the condition. If it is False, skip the body and continue after the loop.
- If it is True, run the body from the top, then go back and evaluate the condition again.
- continue skips the rest of the body and goes straight back to the condition check.
- break leaves the loop immediately; the condition is not checked again (while True loops rely on it, e.g. stopping at a sentinel like 'done').
- If nothing in the body can make the condition False, the loop never ends; the lab's guard stops it after N iterations and says so.
Default run (21 steps): Ready: Countdown. A while loop checks its condition before every pass. … Finished after 5 iterations and 6 condition checks.
Simplified: The lab's own small Python interpreter runs a fixed, hand-written script (you choose the variant and the values, never the code); nothing is executed as real Python. Every script was run in CPython 3.12 with the same inputs and the lab reproduces its output and error messages. input() returns the lines you type in the list, one per call (the prompt is not printed), and raises EOFError when they run out, like a closed keyboard stream. The guard is the lab's, not Python's.
Loading the simulation…
Why Repetition Needs Control
Imagine printing the numbers 5, 4, 3, 2, and 1, followed by “Blastoff!” Writing one print statement for each number is possible, but it becomes impractical when the countdown starts at 1000. A loop lets you write a small block of code once and have Python execute it repeatedly, with values changing between passes.
A while loop repeats its body as long as a condition is true. Python checks the condition before every iteration. A true result runs the body; a false result stops the loop and continues with the next statement after it.
Tracing a Countdown
Following n from 5 to 0
Determine what happens during each pass of the countdown loop.
- 1Initial check
n starts at 5. The condition n > 0 is true, so the body runs.
- 2First iteration
The loop prints 5 and changes n to 4.
- 3Continuing iterations
The same pattern continues for n values 4, 3, 2, and 1. Each pass prints the current value and decreases n by 1.
- 4Final check
After the pass for n equal to 1, n becomes 0. The condition 0 > 0 is false, so the loop body does not run again.
- 5After the loop
Execution continues to the next statement, which prints “Blastoff!”
The loop body executes five times and prints 5, 4, 3, 2, 1. Then the program prints “Blastoff!”
5
4
3
2
1
Blastoff!The Iteration Variable
An iteration is one complete execution of the loop body. The iteration variable is the variable that changes inside the body and eventually causes the condition to become false.
In the countdown, n is both part of the condition and the value changed by the body. The condition checks whether n is greater than zero, while the body decreases n. This relationship is what moves the loop toward termination. A terminating loop has a finite starting value, a condition that checks that value, and code that moves it toward the stopping point.
When Conditions Never Change
An infinite loop never terminates because its condition never becomes false. One cause is a missing iteration-variable change. For example, if n begins greater than zero and the loop only prints n without decreasing it, n remains greater than zero on every check, so the loop continues forever.
Predicting Termination
What do you think happens?
Will this loop terminate, and what value causes it to stop?
n = 3
while n > 0:
print(n)
n = n - 1Reveal answer
It terminates when n becomes 0.
The body decreases n on every iteration. The condition remains true for n values 3, 2, and 1. After the iteration for 1, n becomes 0, making n > 0 false.
For each loop, identify the iteration variable, state whether it changes, and predict whether the loop terminates.
1. n = 2
while n > 0:
n = n - 12. n = 2
while n > 0:
print(n)3. while True:
print("running")
Hints
- Compare each condition with the value used in it.
- Look for a body statement that changes the controlling value.
- A constant True condition does not become false on its own.
Debugging a Runaway Loop
When a while loop produces unexpected output or keeps running, trace it step by step. Before each iteration, write down the controlling variable and the result of the condition. Then check whether the body changes that variable in a direction that can eventually make the condition false.
Common mistake Forgetting to change the iteration variable.
What it looks liken = 5 while n > 0: print(n)Why it failsThe condition depends on n, but n never changes. If it starts greater than zero, every check remains true.
Do this insteadChange n inside the body in a way that moves it toward the value that makes n > 0 false.
Common mistake Assuming that running the loop body automatically ends the loop.
What it looks likeA body can print output repeatedly without changing the condition.
Why it failsOnly the condition controls termination.
Do this insteadTrace the condition before every iteration and identify the body statement that can make it false.
Common mistake Using while True without identifying an exit path.
What it looks likewhile True: print("running")Why it failsThe condition is the constant True, so it does not become false by itself.
Do this insteadDetermine the specific event or stopping mechanism intended to end the loop.
Before running a while loop, answer three questions: Which variable or value appears in the condition? Does the loop body change it? How does that change eventually make the condition false? If you cannot answer the third question, inspect the loop before allowing it to run.
Key Takeaways
- 1A while loop checks its condition before each iteration and runs its body only when the condition is true.
- 2A terminating loop uses a finite iteration variable whose changing value eventually makes the condition false.
- 3An infinite loop occurs when the condition never becomes false, often because the controlling value does not change or because the condition is the constant True.
- 4The body executes once per iteration, and tracing the controlling value before each check reveals why a loop stops or continues.
- 5When debugging a runaway loop, identify the condition, track its variables, and find the missing or ineffective change.
At a glance
- A while loop repeats code while its condition remains true.
- The iteration variable must change toward a value that makes the condition false.
- A missing variable change or a constant True condition can produce an infinite loop.
- Tracing the condition and controlling variable one iteration at a time is a practical way to diagnose runaway loops.