Using Loops to Repeat Actions Without Retyping
Byte the robot stands in a sunny school gym holding a jump rope, counting out loud as a student jumps ten times in a row — Byte grins and points to a big loop arrow drawn on the wall behind them, showing how one instruction handles all ten jumps.
- Explain what a loop does in a computer program using an everyday example.
- Identify when using a loop would save a programmer time and effort.
- Compare writing the same step ten times versus writing it once inside a loop.
- Predict how many times a set of steps will run given a specific loop count.
- Demonstrate how to place a repeating action inside a loop structure.
Key terms
- Loop
- A tool that repeats steps many times.
- Count
- The number of times a loop repeats.
- Body
- The steps inside the loop that repeat.
- Repeat
- To do the same steps again.
Why Loops Help
Writing the same step over and over is tiring and slow. A loop lets you write a step once and tell the computer how many times to do it. So instead of writing blink ten times, you write blink once inside a loop. The loop saves you lots of time and keeps your program short and tidy.
The Two Parts of a Loop
A loop has two parts to remember. The first is the count, the line that says how many times to repeat, like repeat five times. The second is the body, the steps inside that get repeated, like blink. Get the count right and the body right, and your loop works perfectly every single time.
Counting the Total Actions
To find how many actions a loop does, look at how many steps are in the body and how many times the loop repeats. If the body has two steps and the loop repeats four times, the computer does eight actions in all. Multiply the steps inside by the repeat count to get the total.
Worked examples
Write a loop to print Go three times.
- Choose the count: repeat three times.
- Put the step inside the body: print Go.
Answer: repeat 3 times: print Go runs it three times.
How many actions does repeat 4 times: clap, stomp do?
- The body has two steps, clap and stomp.
- Two steps times four repeats equals eight.
Answer: The computer does 8 actions in all.
Activity
Drag the steps into the loop box to build a program that makes Byte wave hello exactly 3 times.
Practice
Write a loop that makes a robot wave hello five times.
Find how many actions repeat 3 times: jump does in total.
Common mistakes to avoid
- A loop runs the body onceA loop runs the body again and again for the whole count.
- The count is the total actionsMultiply the count by the steps inside to get total actions.
Check your understanding
A coder wants to print the word 'Go!' exactly 6 times. Which is the BEST way to do this using a loop?
Maya wrote this program: repeat 4 times: clap stomp How many total actions will the computer perform?
Which situation is the BEST reason to use a loop?
Recap
A loop tells the computer to repeat steps a set number of times so you do not retype them. A loop has a count that says how many times and a body that holds the steps that repeat each time.
Reflect
What repeated action in your day could a loop handle?