Loops: Do It Again!
Byte the friendly robot stands on a bright play mat, clapping its hands again and again, while holding up a glowing card that says REPEAT 4 with a bold arrow looping back to the start.
- Say what a loop does in your own words.
- Explain what the two parts on a REPEAT card mean.
- Spot steps that repeat in a short list.
- Build a loop card to make one step happen many times.
- Pick the right REPEAT card for a simple job.
Key terms
- loop
- an instruction that tells the computer to repeat a step
- repeat
- to do the same step again and again
- count-controlled loop
- a loop that runs a step a set number of times
- count
- the number that says how many times to repeat
Why Loops Save Work
Imagine you wanted a robot to clap one hundred times. Writing the word clap one hundred times would take forever, and if you needed to change it later you would have to fix every line. A loop fixes this. You write the step you want just once and then tell the computer how many times to repeat it. The loop does the rest. This makes your program shorter, easier to read, and much faster to change when you want a different number.
The Two Parts of a REPEAT Card
A count-controlled loop always needs two things on its card. The first part is the step you want to repeat, like clap or jump. The second part is the count, which is how many times to do it. So REPEAT clap 4 times means do the clap step, then again, then again, then once more, and stop after four. If you forget the step, the loop has nothing to do. If you forget the count, the loop does not know when to stop.
Loops Are All Around You
Loops are not just for robots. You use loops every day without naming them. Brushing your teeth is the same brushing step over and over. Climbing stairs is the same step-up move repeated until you reach the top. Singing the chorus of a song again and again is a loop too. Noticing these everyday loops helps you spot the repeating step in a program, which is exactly the step you put on your REPEAT card.
Worked examples
Make a loop card so Byte claps three times.
- Find the step that repeats: it is clap.
- Count how many times you want it: three.
- Put both on the card: REPEAT clap 3 times, which runs clap, clap, clap and then stops.
Answer: REPEAT clap 3 times produces exactly three claps.
Read what REPEAT jump 5 times will do.
- The step on the card is jump.
- The count on the card is five.
- So the loop runs jump, jump, jump, jump, jump and then stops.
Answer: It makes Byte jump exactly five times.
Activity
Put the loop card together so Byte claps three times. Drag each card into the right spot.
Practice
Write a REPEAT card that makes Byte stomp exactly six times in a row.
Find a repeating step you do at home and say how many times you repeat it.
Common mistakes to avoid
- A loop does each step only one time.A loop repeats the step many times; doing a step once is a single command, not a loop.
- REPEAT clap 1 time will make several claps.A count of one means only one clap; you must set the count to the number of claps you want.
Check your understanding
What does a loop tell the computer to do?
Byte wants to clap 5 times. Which REPEAT card is correct?
Why is it better to use a loop than to write 'clap' many times?
Recap
A loop tells the computer to repeat the same step instead of writing it many times. A count-controlled loop needs two parts on its REPEAT card: the step to repeat and the count of how many times to do it. Loops make programs shorter, clearer, and easier to change.
Reflect
What is one repeating job at home you could turn into a loop, and what is its count?