Loops: Repeating Work Without Repeating Yourself
A loop lets a program do the same steps many times — that's where computers truly shine.
Remember how a computer's superpower is doing things fast, over and over, without getting tired? Loops are how we unleash that power.
The problem loops solve
Imagine you want to print "Hello" five times. You could write:
show "Hello"
show "Hello"
show "Hello"
show "Hello"
show "Hello"That works, but it's tedious. And what if you wanted it a thousand times? Or a million? Copying lines would be miserable. There's a better way.
A loop says 'repeat this'
A loop is an instruction that tells the computer to repeat some steps. Here it is in pseudo-code:
repeat 5 times:
show "Hello"That's it. Five words instead of five lines — and changing 5 to 1000 is just as easy. The computer happily does the repeating for you. This is one of the moments programming starts to feel like a superpower.
Looping over a list of things
Another extremely common kind of loop walks through a collection of items, one at a time:
for each fruit in ["apple", "banana", "cherry"]:
show "I like " + fruitThis would display:
I like apple
I like banana
I like cherryThe loop runs once for each item. Each time around, the variable fruit holds the next item in the list. The same instructions, applied to different data — no copying required.
Loops need a way to stop
A loop that never stops is called an infinite loop, and it's usually a bug — the program just spins forever. So a loop always needs a clear ending: a number of times to repeat, or a list to get to the end of, or a condition that eventually becomes false.
Tip: Whenever you catch yourself thinking "I'd have to copy and paste this same thing over and over," that's a loop waving at you. Loops are the programmer's answer to repetitive work.
The big four, together
You've now met the four core ideas that nearly every program is built from:
- Variables — store information.
- Sequence — run steps in order.
- Decisions (if) — choose between paths.
- Loops — repeat steps.
That's genuinely most of it. Everything else builds on these four. You're ready to run some real code.
Quick check
Q1 What is the main purpose of a loop?
Q2 Why is an 'infinite loop' usually a problem?
Want to save your progress? It's free.
Create a free account