Repeating actions — for and while
Loops let you repeat a block of code without copying it. They are how programs process collections and repeat work until a condition changes.
- Write a for loop over a range or a list
- Write a while loop that terminates when a condition becomes false
- Use break and continue to adjust a loop mid-run
Suppose you want to print the numbers 1 to 100. You could write 100 print
statements, but that is brittle, repetitive, and breaks if you change your mind
to 1–1000. The right tool is a loop: a block of code paired with a rule for
how many times to run it.
Loops are one of the two fundamental ways programs do more work than the lines you write. (Functions, coming next, are the other.) Most real programs spend most of their time inside loops.
for loops — iterating over a sequence
A for loop walks through the items of a sequence one by one, running the body
for each item:
for name in ["Alice", "Bob", "Carol"]:
print("Hello,", name)To repeat a fixed number of times, use range:
for i in range(5): # i takes values 0, 1, 2, 3, 4
print(i)range(5) produces the integers 0, 1, 2, 3, 4 — it starts at 0 and stops
before 5. You can also give it a start and stop: range(1, 6) gives 1 through 5.
while loops — repeating until a condition changes
A while loop keeps running as long as a condition is True:
countdown = 5
while countdown > 0:
print(countdown)
countdown -= 1 # countdown = countdown - 1
print("Go!")The critical thing: something inside the loop must eventually make the condition
False. Here, countdown -= 1 decreases the counter each iteration. Without
that line, countdown would stay at 5 forever — an infinite loop.
Every time you write a while loop, identify the line that moves the loop
toward stopping. If there is no such line, you have an infinite loop. In the
browser runner, an infinite loop will time out — not freeze your machine — but
it is still a bug to avoid.
break and continue
Two keywords let you adjust a loop's behaviour mid-run:
breakexits the loop immediately, jumping past the rest of the body and past any remaining iterations.continueskips the rest of the current iteration and moves straight to the next one.
for n in range(10):
if n == 6:
break # stop the loop entirely when n reaches 6
if n % 2 == 0:
continue # skip even numbers
print(n) # prints 1, 3, 5break is useful when you have found what you were searching for. continue is
useful when you want to skip certain items without exiting the whole loop.
Prefer for loops when you know the sequence in advance — iterating over a list,
or a fixed number of steps. Reach for while when the stopping condition depends
on something that can only be checked as the loop runs, like "keep asking until
the user types a valid number."
Exercise
Write a function sum_to(n) that returns the sum of all integers from 1 to n inclusive. For example, sum_to(4) returns 10 because 1 + 2 + 3 + 4 = 10.
sum_to(4) → 10sum_to(10) → 55Where to go next
Next: functions — giving a name to a block of logic so you can reuse it anywhere in your program.