Concepts / Loop Statements

Loop Statements

A while loop is a control structure for condition-based repetition.

  • Programming
Interactive lab

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

  1. Evaluate the condition. If it is False, skip the body and continue after the loop.
  2. If it is True, run the body from the top, then go back and evaluate the condition again.
  3. continue skips the rest of the body and goes straight back to the condition check.
  4. 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').
  5. 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.

Educational simulation

Loading the simulation…

Does a Loop Run Forever Once Started?

What do you think happens?

When a while loop starts, does its statement keep repeating forever no matter what?

  • Yes, once started it never stops

  • No, it repeats only while its condition remains true

Reveal answer
Answer:

No, it repeats only while its condition remains true

A common first impression is that starting a while loop means the statement inside it will run forever. That is not the rule. Repetition is controlled by a condition: the statement is repeated only while that condition remains true.

A while loop is a control structure for condition-based repetition. Instead of saying how many times something should happen, you describe a test that is checked again and again. As long as the test is true, the loop keeps going. The moment the test becomes false, the loop stops and the program moves on to whatever comes after it. This article explains what gets repeated, how the test controls the repetition, and how to predict where a loop will end.

What a While Loop Repeats

Definition

A while loop is a control structure for condition-based repetition. Its statement repeats only while the specified condition remains true.

Two parts work together. The condition is the test that decides whether another repetition happens. The statement is the work that gets repeated. A while loop can contain one statement, but it is common to have a block of several statements that all repeat together on each pass. The loop does not decide on its own when to stop; it asks the condition every time before it runs the body.

Because the condition is checked before each repetition, the order of events on every pass is fixed. The program first asks whether the condition is true. If it is, the body runs once. Then the program returns to the condition and asks again. If the condition is false at any check, the body is skipped and execution continues past the loop. The diagram below shows this path.

Loop reachedCondition true?Run body statementsContinue after loopcheckyescheck againno
After the condition is checked, which path does execution take: back into the body, or past the loop? The check happens again before every repetition.

Condition and Body in the Structure

Every while loop has two parts you should be able to point to. The condition sits in the header line and is the expression tested before each repetition. The statement body is the indented block underneath, which is what repeats while the condition holds. When you read a loop, first find the condition, then find the body, and ask yourself what the body changes that could eventually make the condition false.

Code walkthrough
count = 3
while count > 0:
    print(count)
    count = count - 1
print("Done")
  1. 1
    Line 1 stores 3 in count.
  2. 2
    Line 2 is the while header; its condition is count > 0.
  3. 3
    Lines 3 and 4 form the body. They print the current value and then reduce count by one.
  4. 4
    Line 5 sits outside the loop and runs only after the condition has become false.
  • count = 3 sets the starting value the condition will test.
  • count > 0 is the condition, checked before every pass.
  • The indented lines are the body that repeats while the condition is true.
  • print("Done") is not indented, so it runs once after the loop has finished.
Output
3
2
1
Done

Tracing the Condition Pass by Pass

The most reliable way to predict a loop is to track the value the condition tests across each pass. Write down the value before the check, decide whether the condition is true, and then apply the body's change. The loop ends at the first check where the condition is false. The diagram below plots the value of count and the result of the check for the example above.

Worked example

Predicting the stop point of a countdown loop

Starting with count = 3, the body prints count and then subtracts 1. At which check does the loop stop, and what is printed?

  1. 1
    Check 1

    count is 3. Is 3 > 0? Yes, so the body runs. It prints 3, and count becomes 2.

  2. 2
    Check 2

    count is 2. Is 2 > 0? Yes, so the body runs. It prints 2, and count becomes 1.

  3. 3
    Check 3

    count is 1. Is 1 > 0? Yes, so the body runs. It prints 1, and count becomes 0.

  4. 4
    Check 4

    count is 0. Is 0 > 0? No, so the body is skipped and the loop stops.

  5. 5
    After the loop

    The statement outside the loop runs and prints Done.

Result

The body runs three times, printing 3, 2, and 1. The loop stops at check 4, because the condition becomes false when count reaches 0. Then Done is printed.

Check 1count = 3, trueCheck 2count = 2, trueCheck 3count = 1, trueCheck 4count = 0, falsebody subtracts 1body subtracts 1body subtracts 1
How does the value the condition tests change on each pass, and at which check does it first become false?

Finding Where a Loop Stops

The expected stopping point depends on the condition becoming false. That is the key idea. When you need to know when a loop ends, do not count how many times the body has run. Ask instead what the condition tests and how the body changes that value. The loop stops at the first check where the condition evaluates to false, and that check is often the moment a value crosses a boundary.

When the Condition Never Becomes False

If the condition never becomes false, the result is an infinite loop. The loop keeps returning to the condition, finds it true, runs the body, and then checks again. Nothing ever ends the cycle. This happens when the body does not change the value the condition tests, or changes it in a direction that never reaches the stopping point.

python
Notes on the code
  • The condition count > 0 is true at the start.
  • The body prints a message but never changes count.
  • Every check sees count = 3, so the condition stays true forever.
Condition count > 0evaluates trueBody prints messagecount still 3trueback to check
If nothing inside the body changes what the condition tests, what state does the program keep returning to, and why does it never exit?

Mistakes When Reading While Loops

  • Common mistake

    Believing that starting a while loop means its statement runs forever.

    Why it fails

    Repetition is controlled by the condition. The statement repeats only while that condition remains true.

    Do this instead

    Find the condition first and ask when it becomes false.

  • Common mistake

    Counting repetitions to predict the stop point instead of checking the condition.

    What it looks like

    With count = 3 and count > 0, the body runs three times because the stop comes from count reaching 0.

    Why it fails

    The loop stops at the first check where the condition is false, not after a fixed number of body runs you assumed.

  • Common mistake

    Writing a body that never changes the value the condition tests.

    What it looks like

    A body that only prints a message while count stays at 3 keeps the condition true forever.

    Why it fails

    The condition stays true on every check, so the loop never reaches the point where it becomes false.

Practice Predicting a Stop

EASY

Suppose count starts at 5, the while condition is count > 2, and the body subtracts 1 from count each pass. How many times does the body run, and what is the value of count when the loop stops?

Hints
  • Write the value of count before each check, one row per check.
  • Mark each check true or false, and remember the body is skipped on a false check.
  • The body runs on the checks where count is 5, 4, and 3. Check where count is 2.

Key Takeaways

  • A while loop repeats its statement only while the specified condition remains true.
  • The condition is checked before each repetition, and the loop stops at the first check where it is false.
  • To predict when a loop stops, track the value the condition tests across each pass rather than counting repetitions.
  • If the condition never becomes false, the result is an infinite loop, so debug by asking whether the condition is still true.