Loops: Making the Computer Repeat
A middle-school coding lesson on loops: learn what loops are, how for and while loops work, why they save time, and see real Scratch and Python examples. With quiz.
Key takeaways
- A loop repeats a set of instructions so you don't have to write them over and over
- A 'for' loop repeats a set number of times; a 'while' loop repeats as long as a condition is true
- A loop that never stops is called an infinite loop, and it's usually a bug
- Loops save time, reduce mistakes, and keep code short and clear
The problem loops solve
Imagine you want a program to print "Hello" five times. You could write the same line five times:
print("Hello")
print("Hello")
print("Hello")
print("Hello")
print("Hello")
That works, but what if you needed it 100 times? Or 1,000? Copying lines is slow and easy to get wrong.
A loop solves this. A loop tells the computer: "repeat these instructions." You write the steps once, and the loop runs them as many times as you want.
The 'for' loop: repeat a set number of times
A for loop repeats a known number of times. In Python, it looks like this:
for i in range(5):
print("Hello")
range(5) produces the numbers 0, 1, 2, 3, 4 — that's five numbers — so the indented line runs 5 times. The output is "Hello" printed five times.
The variable i holds the current number each time around. We can use it:
for i in range(3):
print("Count:", i)
This prints:
Count: 0
Count: 1
Count: 2
Notice it starts at 0, not 1. That's normal in many programming languages.
Loops in Scratch
If you've tried Getting Started with Scratch, you've already met a loop! The repeat block is a for loop:
repeat 5
move 10 steps
wait 0.2 seconds
Everything inside the repeat block runs 5 times. Scratch also has a forever block, which repeats endlessly — handy for things like "always check if the sprite touches the edge."
The 'while' loop: repeat while something is true
Sometimes you don't know how many times to repeat. You just want to keep going while a condition is true. That's a while loop:
count = 1
while count <= 5:
print(count)
count = count + 1
This prints 1, 2, 3, 4, 5. Here's how it works each time around:
- Check the condition: is
countless than or equal to 5? - If yes, run the body and add 1 to
count. - Go back to step 1.
When count becomes 6, the condition is false, so the loop stops.
Watch out for infinite loops
The while loop above works because count keeps growing until the condition becomes false. But look what happens if we forget to add 1:
count = 1
while count <= 5:
print(count)
# oops, we never change count!
count stays 1 forever, so the condition is always true. This is an infinite loop — it never stops. Infinite loops are a common bug. Always make sure something inside the loop will eventually make the condition false.
Why loops matter
Loops are one of the most powerful ideas in coding. They let a computer do huge amounts of repeated work in a tiny amount of code. The same idea powers everything from animations to the math behind artificial intelligence, where computers loop over millions of examples to learn.
Try it yourself
- Write a for loop that prints the numbers 1 to 10.
- Write a while loop that counts down from 5 to 1.
- In Scratch, use a repeat block to draw a square (hint: repeat 4 times, move and turn 90 degrees).
Ready to write real loops in code? Head to Your First Python Program.
Quick quiz
Test yourself and earn XP
What is a loop in coding?
A loop repeats a block of instructions, so you write the steps once instead of many times.
Which loop runs a set number of times?
A 'for' loop runs a known number of times, like 'repeat 5 times'.
How many times does this run? ``` for i in range(3): print(i) ```
range(3) gives 0, 1, 2, so the loop body runs 3 times.
A loop that never stops is called a...
An infinite loop never ends because its stop condition is never met. It's usually a bug.
Why are loops useful?
Loops let you repeat work without copying the same lines, keeping code short and easy to fix.
FAQ
A for loop repeats a known number of times. A while loop keeps repeating as long as a condition stays true, which may be an unknown number of times.
An infinite loop is a loop whose stop condition is never met, so it runs forever. It's almost always a mistake in your code.
Keep exploring
More in Coding