Loops: Repeat Without Rewriting
Byte the cheerful blue robot stands on a sunny tile floor, clapping the same three steps over and over to reach a friendly gold star, grinning as the steps shrink into one short glowing card.
- State in their own words what a loop does and why programmers use one.
- Identify the repeated steps inside a list of instructions.
- Rewrite a long list of repeated steps as a short loop with a count.
- Predict how many total steps a loop runs from its repeat number.
Key terms
- Loop
- A short way to repeat steps.
- Repeat number
- How many times the loop runs the step.
- Repeated step
- A step that shows up again and again.
- Count
- To find how many times something happens.
Why a Loop Is Smarter
Writing clap, clap, clap over and over gets tiring fast. Imagine writing fifty claps by hand. A loop is a smarter trick. It says do these steps this many times. So instead of a long list, you write repeat four times clap. You get the same result with much less writing and far fewer mistakes.
Finding the Repeated Step
To make a loop, first look for steps that say the exact same thing over and over. That repeated step is what you wrap inside the loop. Count how many times the step shows up in the list. That count becomes the repeat number, so the computer knows how many times to do the step for you.
The Two Parts of a Loop
A loop has two parts. One part is the step to repeat, like clap. The other part is the repeat number, like four, which tells how many times to do it. The computer counts for you, so you never have to write the same line again and again on your own anymore.
Worked examples
Turn clap, clap, clap into a loop.
- Count the claps: there are three of them.
- Write the loop with that count: repeat three times clap.
Answer: REPEAT 3 TIMES: clap is the loop version.
How many claps from REPEAT 5 TIMES: clap?
- The repeat number is five.
- So the clap step runs exactly five times.
Answer: The computer claps 5 times.
Activity
Put these three cards in order to turn a long repeated list into one short loop.
Practice
Turn the list jump, jump, jump, jump into a short loop.
Tell how many times REPEAT 6 TIMES jump makes a jump.
Common mistakes to avoid
- A loop runs the step onceA loop runs the step many times, equal to the repeat number.
- The repeat number means extra timesThe repeat number is the total times, so five means five.
Check your understanding
What is the main job of a loop?
Which is the loop version of: clap, clap, clap, clap, clap?
How many times will the computer clap if you write: REPEAT 4 TIMES: clap?
Recap
A loop is a smarter way to repeat steps without rewriting them. Find the step that repeats, count how many times it shows up, and write REPEAT that number TIMES. The computer counts for you and runs the step exactly that many times.
Reflect
What long list of steps could you shrink into a loop?