Sequences and Loops: Steps in Order, Again and Again
Kids guide a robot across a grid with arrow cards, spot patterns, and learn how loops repeat steps so code stays short.
CarnielSeriesCoding for Children2 of 4
On this page9 sections
Welcome back, coder! Last time you learned that code is a set of instructions for a computer, that an algorithm is a list of steps in order, and that a bug is a mistake, like putting socks on over shoes.
Today you will learn two big ideas that every coder uses: sequences and loops.
What Is a Sequence?
A sequence is a set of steps that happen one after another, in order. You already know one: brushing your teeth!
The order of a sequence matters. Try these two:
- Open the door, then walk outside.
- Walk outside, then open the door.
The first one works. The second one ends with a bump on the nose! Same steps, different order, very different result.
Computers always follow a sequence from the top to the bottom. So when you write code, you have to think: what needs to happen first?
The Grid Mission
Our robot friend is low on power. It needs to reach the battery. But watch out for the rocks!
The robot understands only two kinds of cards:
- Right arrow: move one square to the right.
- Down arrow: move one square down.
Here is a sequence that gets the robot to the battery:
- Right
- Down
- Right
- Down
- Right
- Down
Put your finger on the robot and follow each card. Did you miss every rock? Great job!
Now try swapping cards 1 and 2, so the robot goes down first. Where does it end up? (Hint: ouch, a rock!) That is a bug. The fix is to put the cards back in the right order.
Spot the Pattern
Look at those six cards again. Do you notice anything?
Right, down. Right, down. Right, down. The same two cards keep coming back! When something repeats like that, we call it a pattern.
Patterns are everywhere. Here is one with shapes:
Circle, square. Circle, square. Circle, square. Instead of drawing all six shapes, we could just say: "circle, square, repeat 3 times." Shorter, right?
Loops: Do It Again!
Coders are a little lazy, in a good way. If they need to do the same steps again and again, they don't write them out over and over. They use a loop.
A loop tells the computer to repeat some steps a number of times.
Let's use a loop on the grid mission. Instead of six cards, we need just three things:
- A repeat 3 times block
- A right arrow inside it
- A down arrow inside it
The robot reads the loop like this: "Do right, then down. Now do it again. And one more time." It walks the exact same path as before, but your code is much shorter.
Now imagine the battery was 100 squares away. Would you rather lay out 200 cards, or use one loop that says repeat 100 times? That's why coders love loops.
Try It: Paper Grid Game
Grab some paper, a pencil and a small toy to be your robot.
- Draw a grid of 5 squares by 5 squares.
- Put your toy robot in the top left square.
- Draw a star (the battery) somewhere on the grid.
- Draw a few rocks to make it tricky.
- Make arrow cards on scraps of paper: right, left, up and down.
- Lay out a sequence of cards that gets the robot to the star. Then test it by moving the toy one card at a time.
Bonus challenge: Can you find a pattern in your cards? If so, write a "repeat" card and see how many cards you can save.
Play with a friend or a grown-up. One person sets up the grid, the other writes the code. Then swap!
New Words
- Sequence: steps that happen one after another, in order.
- Pattern: something that repeats.
- Loop: code that repeats some steps a number of times.
Quick Quiz
- Why does the order of a sequence matter?
- What is the pattern in this list: clap, stomp, clap, stomp, clap, stomp?
- How would you write "clap, stomp, clap, stomp, clap, stomp" using a loop?
- Why do coders like loops?
Answers: 1. The computer follows the steps exactly in order, so the wrong order gives the wrong result. 2. Clap, stomp. 3. Repeat 3 times: clap, stomp. 4. They save time and make code shorter.
What's Next?
You have been coding with paper cards. Next time, you will write your first real program on a computer! In Your First Scratch Program, you will snap blocks together to make a character move, talk and bounce, and you will use a loop to keep it going.
For Grown-Ups
Sequences and loops are two of the three building blocks of every program (the third, making choices with "if," comes later in the series). The paper grid game scales nicely: make the grid bigger, add "left" and "up" cards, or add a rule that the robot must collect two stars. If your child gets stuck, ask them to say each step out loud while moving the toy. Talking through the code is exactly what professional programmers do when they hunt for bugs.