Code & Robots, level 3
🔁 Loops
How do you tell a robot to do something again and again?
- How do you tell a robot to do something again and again? You could tell it "go right" three times in a row. But coders have a shortcut. It is called a loop. A loop tells the robot to repeat the same steps a certain number of times.
- Instead of writing right, right, right, you can write one block that says: repeat three times, go right. It does the same thing with fewer blocks. Loops make programs shorter and easier to read, and they make mistakes easier to spot and fix.
- A loop can hold more than one step. Repeat two times: go up, then go right. That makes the robot climb as if it were walking up stairs. Before you run a loop, count the steps you need, and then pick the right number.
Try it at home: Make a dance loop: repeat 3 times, clap, stomp, spin. Take turns writing a loop for each other to follow.
Questions and answers
Loops
- 🔁 A loop tells a robot to… repeat steps
- ➡️ Right, right, right, right is the same as… Repeat 4 times: →
- Why do coders use loops? To make programs shorter
- 🦘 Repeat 3 times: jump. How many jumps? 3
- 👏 Repeat 2 times: clap, stomp. How many claps? 2 (Each time round there is one clap, and it goes round twice.)
- 🔢 Before you run a loop, what should you count? The steps you need