Unit 3 · Programming 2 · Lesson 3.1

The while Loop

Until now, every line ran once. A loop lets one block of code run again and again — as long as a condition stays true. This is the single biggest jump in what your programs can do.

Big Idea 3 — Algorithms & ProgrammingWeek 1 · Iteration

Before this lesson — read on Runestone

Read the while half today; we’ll hit for in 3.3. Enrolled in our Runestone course? Open it from there so your progress counts.

1

Where We Are

People→ Bits→ Programs→ Internet→ Security→ Big Data→ Impact

Still in Programs, opening Unit 3 — the longest and most important unit of the year. Unit 2 taught the machine to decide; Unit 3 teaches it to repeat. Everything after this unit uses loops, lists, and procedures; nothing after it re-teaches them.

2

The Hook

“Keep stirring until the sauce thickens.” You didn’t count the stirs. You repeated one action and kept checking a condition — stopping the moment it changed. Humans loop on conditions all day.

A while loop is exactly that: “keep doing this while the condition is true.” The power — and the danger — is that the computer will happily repeat forever if you never let the condition become false. Today you learn to loop on purpose and stop on purpose.

3

The Idea

A while loop has three parts, and every correct loop has all three:

PartWhat it isIn the example below
the conditiona Boolean checked before each passn > 0
the bodythe indented lines that repeatprint(n)
the escape hatcha line that moves the condition toward falsen = n - 1
Forget the escape hatch and you get an infinite loop. If nothing inside the body ever makes the condition false, the loop runs forever and the program freezes. It is the most common loop bug there is — and every programmer, including your teacher, has shipped one. The fix is always the same: make sure something in the body changes the thing the condition checks.

Python — a countdown

n = 3
while n > 0:
    print(n)
    n = n - 1
print("liftoff")

What happens

# start n at 3
# check n > 0 BEFORE each pass
# body: prints 3, then 2, then 1
# escape hatch: n drops each time
# when n hits 0, condition is
# false → loop ends → "liftoff"

Hover or tap a line to light its twin. Output is 3, 2, 1, liftoff. The condition is checked before each pass, so when n reaches 0 the body is skipped entirely.

A while loop is a repeating if. An if asks its condition once and runs its block at most once. A while asks the same kind of condition over and over, running its block each time the answer is still true. If you understood 2.12, you’re most of the way here already.
4

Try It — Step the Loop

Set a starting value and step through the countdown one pass at a time. Watch the condition get checked before each body run, and the escape hatch shrink n toward zero.

start n =
output: (nothing yet)
5

Vocabulary

iteration Programs
Repeating a block of code. One pass through the loop body is one iteration.
loop Programs
A structure that repeats its body. A while loop repeats as long as a condition stays true.
condition-controlled loop Programs
A loop that repeats based on a Boolean condition (a while loop), not a fixed count.
infinite loop Programs
A loop whose condition never becomes false, so it never stops — usually a missing or broken escape hatch.
6

Check

Five questions. Trace each loop on paper — write the variable after every pass — then pick your answer.

0 of 5 answered
7

Impact Check

The loop that runs the world — and the one that hangs it

Almost every program you use is one big loop: a game redraws the screen while you’re playing; a server waits for requests while it’s running; your phone checks for messages while it’s on. “Do this repeatedly until something changes” is the shape of nearly all software.

It’s also how programs hang. When an app “stops responding,” a loop is often stuck — a condition that should have gone false never did. The same construct that gives software its power gives it its most common way to freeze. Respect the escape hatch.

8

Connections

Came from 2.12 — a while is an if that keeps asking. The Boolean condition skill transfers directly.
Next in 3.2 — AP Pseudocode writes this as REPEAT UNTIL, and its condition is the opposite of the while condition. That flip is a whole lesson.
Returns in 3.4 — put a running total inside the body and you have the accumulator, the pattern behind half of Unit 3.
Returns on the exam — “how many times does this loop run?” and infinite-loop spotting are guaranteed. Trace, don’t guess.
← 2.20 Test Day