Branching Statements
A while loop is a control structure for condition-based repetition.
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…
The Belief That Loops Run Forever
Many learners first meet a loop and assume that once it starts, the statement inside it will run forever. That impression is understandable, because the word loop suggests something that goes around and around. However, that is not how a while loop works. Repetition is controlled by a condition, and the statement is repeated only while that condition remains true.
In this article you will see exactly what a while loop repeats, how its condition decides when repetition stops, and why a condition that never becomes false leads to a loop that does not end. The examples are short on purpose, so that you can trace every pass by hand before you rely on the loop in a larger program.
What a While Loop Repeats
A while loop is a control structure for condition-based repetition. Its statement repeats only while the specified condition remains true.
A while loop has two parts that you must keep separate in your mind. The condition is the test that is evaluated to decide whether repetition continues. The statement is the work that gets repeated. The statement may be a single statement, but it is common for it to be a block of several statements that run together on each pass.
The condition controls the repetition. The body does not decide on its own when to stop; the loop keeps returning to the condition to see whether it still holds.
Reading the Basic Structure
The example below uses a counter variable named count. The line that begins with while holds the condition, which is count < 3. The indented lines beneath it form the statement part, the block that repeats. The last line inside the block changes count, which is what eventually makes the condition false.
0
1
2
3- count = 0 sets the starting value before the loop begins
- while count < 3: is the condition part, tested before each pass
print(count)and count = count + 1 are the indented statement part that repeats- The final print runs only after the condition has become false
The flowchart shows the rule that governs every while loop. The condition is checked before each pass. If it is true, the body runs once and control returns to the check. If it is false, the program leaves the loop and continues with the statement that follows it.
Tracing the Counter Through Each Pass
The best way to predict a loop is to follow the variable that the condition depends on. In the counter example, the condition depends on count. Before the loop starts, count is 0. Each pass checks count < 3, runs the body, and then increases count by one. The table of states below records what happens on every check.
Predicting When the Counter Loop Stops
Starting with count equal to 0 and the condition count < 3, on which check does the condition first become false, and what is printed?
- 1Check 1
count is 0. The condition 0 < 3 is true, so the body runs. It prints 0 and count becomes 1.
- 2Check 2
count is 1. The condition 1 < 3 is true, so the body runs. It prints 1 and count becomes 2.
- 3Check 3
count is 2. The condition 2 < 3 is true, so the body runs. It prints 2 and count becomes 3.
- 4Check 4
count is 3. The condition 3 < 3 is false, so the body does not run and the loop exits.
The loop prints 0, 1, and 2. The condition first becomes false on check 4, when count is 3. The final print then shows 3.
Predicting the Exit Point
The expected stopping point of a while loop depends on the condition becoming false. You do not predict the stop by counting how many times the statement has been written in the code. You predict it by asking when the condition will first fail. Once you know which variable the condition uses and how the body changes that variable, you can work out the exit point before running the program.
When the Condition Never Turns False
If the condition never becomes false, the result is an infinite loop. The loop keeps returning to the check, the check keeps finding the condition true, and the body keeps running. The following example is included only to show the pattern. It should not be run as written, because it does not end.
- flag = True makes the condition true at the start
- The body prints flag but never changes it
- Because flag stays True, the check never fails and the loop never exits
Mistakes Beginners Make with While Loops
Common mistake Believing the statement inside the loop repeats forever once the loop starts
What it looks likeAssuming the print inside the counter loop would keep printing after the third pass
Why it failsRepetition is controlled by the condition. The statement repeats only while the condition remains true.
Do this insteadTrace the condition on each pass and find the pass where it first becomes false.
Common mistake Forgetting that the condition is checked before each pass
Why it failsThe loop returns to the condition every time the body finishes, so the check decides whether another pass happens.
Do this insteadTreat the condition as a gate that is tested again before every pass, not only at the start.
Common mistake Writing a body that never changes the variable the condition depends on
What it looks likeA loop with the condition count < 3 whose body prints count but never increases count
Why it failsThe condition stays true, so the check never fails and the result is an infinite loop.
Do this insteadMake sure a statement in the body moves the condition toward false, such as count = count + 1.
Common mistake Counting the number of printed lines instead of checking the condition when debugging
Practice Tracing a Loop
Consider the code below. Before you look at any answer, trace it by hand. Write down the value of n before each check, decide whether the condition is true or false, and state which values are printed and what n equals when the loop exits.
n = 5
while n > 2:
print(n)
n = n - 1Hints
- Start with n equal to 5. The first check is 5 > 2, which is true.
- Each pass subtracts 1 from n, so the values checked are 5, 4, 3, and then 2.
- The condition first becomes false when n is 2.
Key Takeaways
- A while loop is a control structure for condition-based repetition. Its statement repeats only while the specified condition remains true.
- The basic structure has a condition part, which is tested before each pass, and a statement part, which may be a single statement or a block that repeats.
- To predict when a loop stops, trace the variable the condition depends on, pass by pass, until the condition first becomes false.
- If the condition never becomes false, the result is an infinite loop, so the body must change the value the condition relies on.