while Loops, break, continue
The for loop repeats over a collection. The while loop repeats over a condition — “keep going until…” — which makes it the tool for user input, games, and anything with an unknown number of repetitions.
The basic while
count = 3
while count > 0:
print(count)
count -= 1
print("Lift off! 🚀")
Output:
3
2
1
Lift off! 🚀
Read it: “while count is above zero, print it and decrease.” The loop checks the condition before each pass; the moment it’s False, execution jumps past the loop.
The infinite loop — and how to escape it
while True:
print("This never ends")
while True runs forever… unless something inside breaks out. That’s what break is for:
while True:
command = input("Type 'quit' to exit: ")
if command == "quit":
break # exits the loop RIGHT NOW
print(f"You typed: {command}")
print("Goodbye!")
This shape — while True: + a break condition — is the standard pattern for “keep asking until the user says stop.” The loop condition is deliberately always-true; the break decides when to leave.
The password gate (classic use)
attempts = 3
while attempts > 0:
password = input("Password: ")
if password == "open123":
print("Access granted")
break
attempts -= 1
print(f"Wrong! {attempts} attempt(s) left")
else:
print("Account locked")
Yes — while can take an else! It runs only when the loop finishes normally (condition became False), not when broken out. Rare but perfect here.
continue — skip this one, keep looping
break exits the loop entirely; continue skips just the current pass:
for number in range(1, 11):
if number % 2 == 0:
continue # skip evens
print(number) # odds only: 1 3 5 7 9
Real use — process only valid lines:
for line in lines:
if line == "": # skip empty lines
continue
process(line)
break vs continue — one screen
| break | continue | |
|---|---|---|
| Effect | Exits the whole loop | Skips to the next pass |
| Loop continues after? | No | Yes |
| Analogy | Leave the building | Skip this song |
The #1 while bug: forgetting to change the condition
count = 3
while count > 0:
print(count)
# count -= 1 is MISSING — infinite loop!
This program never ends — count stays 3 forever. If your terminal freezes in a print storm: Ctrl+C kills the program. Every programmer has done this; now you know the escape hatch before you need it.
The mental checklist for every while loop:
- Initialize — what does the condition check? (
count = 3) - Change — what moves it toward False? (
count -= 1) - Escape — is there a break for special cases?
Common Errors & Fixes
- Infinite loop / frozen terminal — condition never becomes False. Ctrl+C, then find the missing update line.
- Loop runs one extra time — you update the variable after using it; reorder.
while count = 3:— assignment in a condition; use==(or better, a real condition).
✅ Checkpoint
- for vs while — what decides which to use? (Known collection/count → for; unknown repetitions until a condition → while)
- What does break do inside nested loops? (Exits only the innermost loop)
- When does a while-else run? (Only when the loop ends normally — not via break)
- Your terminal is frozen printing — what do you press? (Ctrl+C)
Next: looping like a professional — enumerate, items, and the patterns that make loops clean.