Programming Essentials~25 min

Loops

Repetition lets a program apply the same steps to many items, or repeat until a condition changes — without rewriting those steps each time.

By the end of this lesson, you can

  • Trace a for loop over a sequence and a while loop controlled by a condition
  • Predict how many times a loop body runs, including common off-by-one mistakes
  • Explain why a while loop must eventually make its condition False, and what happens when it doesn't

Why it matters

Without repetition, a program that processes five numbers needs five near-identical copies of the same code — and one that processes five thousand numbers is impossible to write by hand at all. A loop repeats a block of steps so the same logic can apply to as many items as needed, without rewriting it.

Mental model

Python has two kinds of loop, and the difference is what controls how many times they run:

  • A for loop repeats once per item in a sequence — you know in advance (or Python can figure out) how many repetitions there will be.
  • A while loop repeats for as long as a condition stays True — the number of repetitions depends on what happens during the loop, and isn’t known in advance.
PythonA for loop: one repetition per number
total = 0
for number in [4, 8, 15]:
    total = total + number

print(total)
Output
27

range() is a common way to loop a fixed number of times rather than over an existing list:

PythonLooping three times with range
for i in range(3):
    print(i)
Output
0
1
2

range(3) produces 0, 1, 2 — three values, not including 3. This is one of the most common sources of off-by-one mistakes for beginners.

Trace it

Tracing a loop means writing down every variable’s value after each pass through the body:

Tracing total across the for loop above
Passnumbertotal
start(none yet)0
144
2812
31527

A while loop’s condition is checked before every pass, including the first — if it’s False immediately, the body never runs at all:

PythonCounting down with a while loop
countdown = 3
while countdown > 0:
    print(countdown)
    countdown = countdown - 1

print("Liftoff!")
Output
3
2
1
Liftoff!

Check your understanding

How many times does this loop body run? for i in range(5): print(i)
Or reveal the answer without checking

Answer:5
range(5) produces 0, 1, 2, 3, 4 — five values, not including 5. The loop body runs once per value, so five times total.

What causes a while loop to run forever?
Or reveal the answer without checking

Answer:The loop body never changes anything the condition depends on, so the condition never becomes False
A while loop stops exactly when its condition becomes False. If nothing in the body changes a variable the condition depends on, the condition's truth value never changes, and the loop never stops.

Practice: warm-up

Trace this loop by hand — write down total after each pass — before revealing the answer.

total = 100
for amount in [10, 25, 5]:
    total = total - amount

Stuck? Reveal one hint at a time.

  1. Hint 1

    There are three items in the list, so the body runs three times.

  2. Hint 2

    Each pass subtracts that pass's amount from whatever total currently is — use the running value, not the original 100, after the first pass.

Reveal the trace

Try the problem yourself before reading this. There is often more than one reasonable approach — treat this as one worked example, not the only correct answer.

start: total = 100
pass 1 (amount = 10): total = 90
pass 2 (amount = 25): total = 65
pass 3 (amount = 5):  total = 60

Practice: apply it

This loop is supposed to print 1 through 5, then stop — but it never stops:

count = 1
while count <= 5:
    print(count)
Why does this loop never stop?
Or reveal the answer without checking

Answer:Nothing inside the loop body ever changes count, so count <= 5 stays True forever
count starts at 1 and count <= 5 is True, but nothing in the body updates count — so it's still 1 on every pass, and the condition never becomes False.

Modification challenge: add the one missing line that makes this loop print 1 through 5 and then correctly stop.

Summary

  • A loop repeats a block of code so the same steps don’t need to be rewritten for every item.
  • A for loop repeats once per item in a sequence; a while loop repeats for as long as its condition stays True.
  • range(n) produces 0 up to, but not including, n — a common source of off-by-one mistakes.
  • A while loop’s body must eventually make its condition False, or it becomes an infinite loop.
  • Tracing a loop means writing down every relevant variable’s value after each pass through the body.