Quarry School

Trace a while loop

Subject: Computing
Level 1 · FoundationsAbout 6 min read2 new terms
How this was estimated

Programmatic estimate. Rung levels, worked steps, prerequisites, terms and sentence length contribute to this level. It describes the lesson, not your ability.

Reading counts the whole lesson, including folded explanations, at 180 words a minute. Practice allows two minutes per item. These are planning assumptions; take the time you need.

AI estimate unavailable.

Path to here

Open the unit’s lesson list to follow the lesson order.

    Quick review (2 minutes)

    In plain words

    Someone filling a bucket from a tap looks at it before each scoop: if it is not full yet, one more scoop goes in, and then they look again.

    Rule

    A while loop tests its condition before every pass. While the condition is true, it runs the indented body and, when the body reaches its end, goes back to the test; a break ends it sooner. When the condition is false, the program carries on with the first line after the body, and a condition that is false at the first test gives no passes.

    Step

    1. Write each name the condition uses, with its value before the loop.

    2. Test the condition with those values. If it is false, skip to the first line after the body.

    3. If it is true, run the body's lines in order and write each name's new value.

    4. Find what can make the condition false at a later test: a line in the body that changes a name it reads, or a new value it reads each time, such as a reply from input(). If nothing can, and nothing else ends the loop, such as the break of this unit's last lesson, the loop will not end on its own.

    5. Go back to the test with the new values, and repeat until a test is false or something ends the loop sooner.

    Trap

    Leave out the line that changes the name, and the condition never becomes false. A countdown without seconds = seconds - 1 displays 3 again and again and never ends. Press Control-C, the interrupt key, to stop it; Python stops the program with a KeyboardInterrupt.

    The other slip is the edge of the condition. seconds > 0 stops before 0 is displayed, and seconds >= 0 displays 0 as well. Trace the last two tests to see which values get a pass.

    1 In plain words

    Someone filling a bucket from a tap looks at it before each scoop: if it is not full yet, one more scoop goes in, and then they look again. Once it is full, they stop.

    A while loop repeats in the same way. It tests its condition, and when the condition is true it runs the indented body. Then it goes back and tests the condition again.

    When the test gives false, the loop ends and Python carries on with the first line after the body. That can happen at the first test, and then the body never runs.

    Remember one sentence: a while loop tests before every pass. To look it up, see section 3.2 of the Python 3.13 tutorial, First Steps Towards Programming.

    2 Picture it

    Testsecondsseconds > 013True22True31True40False

    A table of the four tests of a countdown loop. When seconds is 3, 2 and 1 the condition seconds > 0 is True, and the body runs. When seconds is 0 the condition is False, and the loop ends. These are the tests of the countdown worked example. The last row is marked.

    The four tests of the countdown loop

    3 Why

    Each pass has two parts: the test, then the body. Python tests the condition first. If it is true, Python runs the body, and when the body reaches its end it goes back to the test.

    For the test to end the loop, the condition has to become false at some test; otherwise the loop goes on until something else ends it, such as the break of this unit's last lesson. A line in the body can change a name the condition reads: in the guessing example, guess = input() gives guess a new reply on each pass. In the countdown program the line seconds = seconds - 1 makes the number smaller on each pass. When it reaches 0, the test seconds > 0 gives False and the loop ends.

    A while loop fits when the number of passes is not known in advance: keep asking until a reply is right, or keep adding until a total reaches a goal. A for loop fits when there is a known sequence to work through.

    4 The rule

    A while loop tests its condition before every pass.

    While the condition is true, it runs the indented body and, when the body reaches its end, goes back to the test; a break ends it sooner. When the condition is false, the program carries on with the first line after the body, and a condition that is false at the first test gives no passes.

    5 How

    1. Write each name the condition uses, with its value before the loop.
    2. Test the condition with those values. If it is false, skip to the first line after the body.
    3. If it is true, run the body's lines in order and write each name's new value.
    4. Find what can make the condition false at a later test: a line in the body that changes a name it reads, or a new value it reads each time, such as a reply from input(). If nothing can, and nothing else ends the loop, such as the break of this unit's last lesson, the loop will not end on its own.
    5. Go back to the test with the new values, and repeat until a test is false or something ends the loop sooner.

    6 Worked example

    Count down to Go

    Python code
    seconds = 3
    while seconds > 0:
        print(seconds)
        seconds = seconds - 1
    print("Go")
    

    Standard input

    Empty

    Standard output

    3
    2
    1
    Go
    

    Predict the four lines first. Then save the code as countdown.py and run the file, or follow the trace below.

    Save until the goal is reached

    Python code
    balance = 50
    weeks = 0
    while balance < 100:
        balance = balance + 20
        weeks = weeks + 1
    print(weeks)
    print(balance)
    

    Standard input

    Empty

    Standard output

    3
    110
    

    The program does not know in advance how many weeks it takes. After 3 weeks the balance is 110. That is not below 100, so the loop ends, one week past 90.

    Ask again until the reply is right

    Python code
    guess = input()
    while guess != "blue":
        print("Try again")
        guess = input()
    print("Yes")
    

    Standard input

    red
    green
    blue
    

    Standard output

    Try again
    Try again
    Yes
    

    Type red, green and blue, one a line. The first two replies keep the condition true. blue makes it false, so the loop ends and Yes is displayed.

    Trace

    Trace the countdown program
    LinesecondsOutputExplanation
    seconds = 33—The name the condition uses gets its first value.
    Test, then pass 13 then 233 > 0 is True. The body displays 3, then makes seconds 2.
    Test, then pass 22 then 122 > 0 is True. The body displays 2, then makes seconds 1.
    Test, then pass 31 then 011 > 0 is True. The body displays 1, then makes seconds 0.
    Test0—0 > 0 is False, so the loop ends.
    print("Go")0GoThe first line after the body runs once.

    7 The trap

    Leave out the line that changes the name, and the condition never becomes false. A countdown without seconds = seconds - 1 displays 3 again and again and never ends. Press Control-C, the interrupt key, to stop it; Python stops the program with a KeyboardInterrupt.

    The other slip is the edge of the condition. seconds > 0 stops before 0 is displayed, and seconds >= 0 displays 0 as well. Trace the last two tests to see which values get a pass.

    Quick checks

    Quick check 1 of 2

    Predict what this program displays. The program reads no input.

    Python code
    count = 10
    while count > 4:
        count = count - 3
    print(count)
    Show the steps and the answer
    Answer

    It displays 4.

    Why: 10 > 4 is true, so count becomes 7. 7 > 4 is true, so count becomes 4. 4 > 4 is false, so the loop ends and 4 is displayed.

    Source: Python Software Foundation, Python 3.13 Language Reference, 8.2 The while statement

    Quick check 2 of 2

    Predict what this program displays. The program reads no input.

    Python code
    x = 5
    while x < 3:
        print("x")
        x = x + 1
    print("done")
    Show the steps and the answer
    Answer

    It displays only done.

    Why: 5 < 3 is false at the first test, so the body never runs. The line after the body runs once.

    Source: Python Software Foundation, Python 3.13 Language Reference, 8.2 The while statement

    Sources

    1. Python 3.13 Language Reference, 8.2 The while statement. Python Software Foundation. Python Software Foundation License Version 2. Read 2 Dec 2026.
    2. Python 3.13 Tutorial, 3.2 First Steps Towards Programming. Python Software Foundation. Python Software Foundation License Version 2. Read 2 Dec 2026.
    3. Python 3.13 Tutorial, 4.2 for Statements. Python Software Foundation. Python Software Foundation License Version 2. Read 2 Dec 2026.
    4. Python 3.13 Library Reference, Built-in Functions: input. Python Software Foundation. Python Software Foundation License Version 2. Read 2 Dec 2026.
    5. Python 3.13 Library Reference, Built-in Exceptions: KeyboardInterrupt. Python Software Foundation. Python Software Foundation License Version 2. Read 2 Dec 2026.