A for loop is used when you know how many times you want to repeat an action. It’s perfect for counting, working through a list, or repeating a task a fixed number of times.
Real-life example: You check your messages every 5 minutes. That’s a loop — you repeat the same action at regular intervals.
Learning Objectives
By the end of this page, you will:
Understand when to use a for loop
Use range() to control the number of repeats
Use break to exit a loop early
Use continue to skip a step
Write and test your own for loops
for i in range(5):
print(i)
What it does:
Runs 5 times
Starts at 0
Prints: 0, 1, 2, 3, 4
Change the number in range() to 10. What prints?
for i in range(10):
if i == 4:
break
print(i)
Output: 0, 1, 2, 3
for i in range(6):
if i == 3:
continue
print(i)
Output: 0, 1, 2, 4, 5
Print numbers from 1 to 20
Use break to stop at 7
Use continue to skip 3
Write each one by hand, then test and correct