>> Module 01 / Day 08 / Lecture

while Loops & break / continue

Day 8 of a 40-day journey
A for loop counts. A while loop waits for something to happen — and stops the moment it does.
Lecture
75 min · 09:00–10:15
Lab
60 min · Find-First-Match
Module
M1 · Python Foundations
DAY 08 / 40
>> Today's roadmap

The loop that waits

  1. The shape of a while loop — condition first
  2. The "loop until something happens" pattern
  3. break — leave early · continue — skip ahead
  4. Infinite loops — the trap, and how to never fall in
  5. for vs while — choosing the right tool
>> 01 / 05 · The shape

A while loop checks a condition, then repeats.

Read it as a sentence: "while this is still true, keep doing the body." Before every pass, Python re-checks the condition.

while_shape.py
count = 3
while count > 0:          # condition — checked before each pass
    print(count)
    count = count - 1     # move toward making the condition False
print("done")
# → 3, 2, 1, done
>> 02 / 05 · The core pattern

"Loop until something happens"

You don't always know how many passes you'll need. With while you don't have to — you loop until a goal is met.

first_multiple.py
# first number >= start divisible by 7
n = 50
while n % 7 != 0:     # keep going until we find one
    n = n + 1
print(n)             # → 56
Find-first-match.
We didn't count. We described the goal and let the loop run until it was reached.
>> 03 / 05 · Leaving early

break — jump out of the loop now

break stops the loop immediately, ignoring whatever the condition says. Perfect for "I found it — I'm done."

break_find.py
n = 50
while True:               # "loop forever" — break is the only exit
    if n % 7 == 0:
        break            # found a match — leave right away
    n = n + 1          # otherwise try the next candidate
print(n)               # → 56
>> 03 / 05 · Skipping ahead

continue — skip the rest of this pass

continue jumps straight back to the condition check, skipping the rest of the body — but the loop keeps going.

continue_demo.py
# print only the odd numbers 1..6
n = 0
while n < 6:
    n = n + 1
    if n % 2 == 0:
        continue        # even → skip the print
    print(n)
# → 1, 3, 5
break vs continue.
break = leave the loop entirely.
continue = skip to the next pass.
>> 04 / 05 · The trap

The infinite loop — and how to avoid it

never_ends.py
# BUG: count never changes
count = 3
while count > 0:
    print(count)
# condition stays True forever
# → 3, 3, 3, 3, ... ∞
fixed.py
# FIX: update the condition variable
count = 3
while count > 0:
    print(count)
    count = count - 1   # moves toward False
# → 3, 2, 1 ✓
>> 05 / 05 · Choosing

for vs while

for

Use when you know the count up front — "do this 10 times", "for each item in the list".

for i in range(10):
while

Use when you don't know the count — "until the user quits", "until we find a match".

while not found:
>> Putting it together

A countdown, start to finish

Condition, body, and the all-important update — every idea from today, in five lines.

countdown.py
def countdown(n):
    while n >= 1:        # condition
        print(n)         # body
        n = n - 1      # update — or it loops forever!
    print("Liftoff!")

countdown(3)            # → 3, 2, 1, Liftoff!
>> Common failures

Four mistakes you'll make today

  1. Forgetting to update the condition variable → infinite loop (the #1 bug).
  2. Off-by-one: using > 0 when you meant >= 1, or stopping one step too early/late.
  3. Updating in the wrong place — e.g. after a continue, so the update gets skipped and the loop hangs.
  4. Reaching for while when a simple for over a known range would be clearer.
>> 11:00 – 12:00 · Lab

Today's lab — Find-First-Match

PROBLEM 1
countdown(n)

Use a while loop to print n, n-1, … 1, then "Liftoff!". Remember the update.

PROBLEM 2
first_multiple(n, start)

Return the first integer >= start divisible by n, found with while + break.

>> End of Day 08

What you can now do

  • ✓ Write a while loop that runs until a condition is met.
  • ✓ Use break and continue to control a loop precisely.
  • ✓ Spot and avoid infinite loops by always updating the condition.
01 / 12
←/→ navigate   F fullscreen   Ctrl/⌘+P save as PDF