Repetition: Loops
How computers repeat work without copying the same instruction over and over
- say what a loop is in simple words,
- explain why a loop is better than copying an instruction,
- read a simple loop diagram and count how many times a loop repeats.
1. What is a loop?
A loop is an instruction that says: "Do this again." Instead of telling the computer the same step over and over, we tell it once and say how many times to repeat it.
Imagine you want a computer to clap 10 times. You could write "clap" ten times in a row. That works, but it is a lot of writing! With a loop you simply say: "Repeat 10 times: clap." Short, clear, and easy to change.
The words that get repeated are called the body of the loop. The number tells the computer how many times to run the body.
2. Why not just copy the instruction?
You could copy the same instruction many times, but a loop is much better. Look at the difference:
| Copying by hand | Using a loop |
|---|---|
| clap clap clap clap clap (...and 5 more times) | Repeat 10 times: clap |
A loop is better because it is:
- Shorter — one line instead of ten.
- Safer — if you copy by hand you might miss one or add an extra. A loop counts perfectly.
- Easy to change — want 100 claps instead of 10? Just change the number from 10 to 100.