University of Free Knowledge
QA 76.73 · fol. 7

Repeat While It Holds

A while loop repeats its body as long as a condition stays true, so the body must eventually make that condition false. · 12 min

Until now, every line of your program has run at most once. A while loop breaks that limit. It reads a test, and as long as the test is True, it runs a block of lines called the body, then goes back and checks the test again. Run, recheck, run, recheck — the loop keeps circling until the test finally comes out False. That last part is the whole responsibility of a while loop: something inside the body must change, step by step, until the condition stops being true. If nothing changes, the loop circles forever, and the program never moves on.

Guess before you learn

Read this loop before you run it in your head: count = 1 while count <= 3: print(count) count = count + 1 What is the last number it prints?

THE DEPTH DIAL — the same idea, younger or deeper
9–12

9–12

Three ingredients make a well-formed counting loop: an initialisation before the loop, a condition tested each pass, and an update inside the body that moves toward making the condition false. i = 0 / while i < n: / i = i + 1 is the template.

The loop runs as long as the condition holds, so to be sure it ends you check that each pass brings the condition closer to false. A loop with no such progress — a forgotten update, or a condition that can never fail — never terminates.

loop body

The indented block of lines beneath a while header. Python runs the whole body each pass, then rechecks the condition.

TruerecheckFalsecount <= 3 ?run the bodyleave the loop
PLATE I A while loop circles: test, body, test again — and exits the moment the test is False.
Retrieval Gate — answer before you continue 0 / 4

1.How often does a while loop test its condition?

2.Trace this loop. What is the value of n after it finishes? n = 0 while n < 10: n = n + 3

3.Which loop never stops?

4.In one sentence, why must the body of a while loop change the value the condition depends on?

The danger and the discipline are the same fact seen twice: a while loop runs until its condition turns false, so you must build that turn into the body. The most common shape is a counter — a variable that starts at one value and climbs or falls a fixed step each pass. Watching that counter change, pass by pass, is how you predict when the loop ends.

Ink That Thinks — guess first; the answer draws itself.
This loop steps a counter upward: n = 0 while n < 10: n = n + 2 Before reading on, place a point for the value of n after each pass through the body — pencil your guesses, then see exactly where it stops.

012345602.557.510pass numbervalue of n
Tap to place each point.
PLATE II A counter climbing by 2 — guess in graphite, truth in ink.

Trace this loop, one pass at a time — the steps fade as you master them

1
Start: i = 1, total = 0. Test i <= 3 is True, so run the body. First line: total = total + i = 0 + 1. What is total now?
total = 1
2
Second body line: i = i + 1. What is i now?
i = 2
3
Recheck: 2 <= 3 is True. Run the body again: total = total + i = 1 + 2. What is total now?
total = 3
4
After another pass i is 4 and total is 6. Recheck: 4 <= 3 is False, so the loop ends. What is the final value of total?
total = 6
Why is this true?

Why can a while loop run zero times — not even once?

Because the condition is tested before the first pass, not after. If it is already false when the loop is reached, Python skips the body entirely and moves on. A while loop runs its body only while the test is true, and while can mean never if it starts out false.

PASSII <= 3TOTAL AFTER BODY11True122True333True644False6 (loop ends)
PLATE III A trace table: one row per pass, reading the loop the way Python runs it.
Retrieval Gate — answer before you continue 0 / 4

1.Trace this loop and give the final value of total. total = 0 k = 1 while k <= 4: total = total + k k = k + 1

2.How many times does the body run? i = 0 while i < 5: i = i + 1

3.This loop never stops: n = 10 while n > 0: print(n) What single change makes it end?

4.Without looking back: what does a while loop repeat, when does it stop, and what makes a loop infinite?

A while loop is the honest workhorse of repetition: it runs while a condition holds and stops the instant it does not. It asks nothing about how many times — only whether to go again. When you do know the count in advance, there is a tidier tool. The next folio introduces the for loop, which runs the body once for each item in a sequence.

Note

Losing the thread of a loop mid-trace? The Atelier of Mind drills trace tables until reading a loop pass by pass feels automatic.

Practice — new ink and old, interleaved

1.Review from folio 5: (4 >= 4) and (2 < 1) evaluates to?

2.How many numbers does this loop print? n = 3 while n > 0: print(n) n = n - 1

3.Evaluate 10 % 3, the remainder of 10 divided by 3.

4.(4 < 2) or not (3 == 3) evaluates to?

5.Trace: what is x after this loop? x = 2 while x < 20: x = x * 2

6.Review from folio 6: x = 4. if x > 10: print('big') / elif x > 2: print('medium') / else: print('small'). What prints?

7.Without looking back: in an if / elif / else chain, which branch runs, and what does else do?

8.Say in your own words when a while loop stops running.

9.Evaluate 10 % 3 (the remainder).

The Call Slip — search everything Ctrl·K / ⌘K