Grade by Grade
C. Algebra · Patterns, Variables, Coding and Modelling

Coding: Steps in Order and at the Same Time

C3.1About 40 minutes
Learning goals
  • I can write code with steps in order (sequential events).
  • I can write code for two characters that run at the same time (concurrent events).
  • I can run (execute) code step by step to see what happens.
  • I can write code to solve a math problem.

Words to know

code
Instructions written so a computer or robot can follow them.
sequential events
Steps that happen one after another, in order.
concurrent events
Things that happen at the same time.
execute (run)
To follow the code to see what happens.
outcome
What happens at the end when the code is run.

Remember from Grade 1

Code is a list of instructions. A robot follows the steps exactly, in order. Last year you wrote code to move Beep the robot on a grid, and code that did math, like Start with 10, Add 5.

This year Beep has a friend: Boop! When two robots move at once, we need a new idea: concurrent events.

Sequential events: one after another

Steps that happen in order, one after another, are sequential events. Here is code to move Beep (A) to the star. Each move is one square.

A
Beep (A) is in the bottom-left square. The star is 3 squares right and 2 squares up.
WHEN GO is pressed
➡ Move right 3
⬆ Move up 2
STOP

Sequential code: first move right 3, then move up 2.

Concurrent events: at the same time

In real life, many things happen at the same time. Snow falls while you build a snowman. Music plays while you dance.

In code, things that happen at the same time are concurrent events. When GO is pressed, both robots start their code together. On each beat, each robot does one step.

AB
Beep (A) starts on the left of the bottom row. Boop (B) starts on the right of the bottom row.
BeatBeep (A)Boop (B)
Press GOStartStart
1➡ Move right 1⬆ Move up 1
2➡ Move right 1⬆ Move up 1
3⬆ Move up 1⬅ Move left 1
Two codes that run at the same time. On beat 1, Beep moves right and Boop moves up, together.
Worked example

Run the concurrent code

Run the code in the table. Where is each robot at the end? Do they bump into each other?

  1. Put one finger on Beep (column 1, row 1) and one on Boop (column 6, row 1).
  2. Beat 1: Beep moves right to column 2. At the same time, Boop moves up to row 2.
  3. Beat 2: Beep moves right to column 3. Boop moves up to row 3.
  4. Beat 3: Beep moves up to row 2. Boop moves left to column 5.
  5. Beep is at column 3, row 2. Boop is at column 5, row 3. They were never on the same square at the same time.

Beep ends at column 3, row 2. Boop ends at column 5, row 3. They do not bump.

Run concurrent code beat by beat

Do beat 1 for both robots before you go on to beat 2. Use one finger for each robot, or ask a grown-up to move one robot while you move the other.

Just for fun

Why did the robot go back to school?

Its skills were getting a little rusty! 🤖

Coding to solve a math problem

Code can solve math problems too. Here, two piggy banks fill up at the same time.

DayBeep's bank (code: START at 0, each day ADD 5)Boop's bank (code: START at 0, each day ADD 10)
Start0¢0¢
15¢10¢
210¢20¢
315¢30¢
420¢40¢
Both codes run at the same time, one step each day.
Worked example

Who reaches 30¢ first?

Use the table. Who has 30¢ first? On which day?

  1. Look down Boop's column: 10, 20, 30 on day 3.
  2. Look down Beep's column: 5, 10, 15, 20 on day 4. Keep running Beep's code: day 5 is 25¢, day 6 is 30¢.
  3. Day 3 comes before day 6.

Boop has 30¢ first, on day 3. Beep gets there on day 6.

Worked example

Write code for a story problem

Write code to solve: There are 24 children on a bus. At the first stop, 6 get off. At the second stop, 10 get on. How many are on the bus now?

  1. Start with the number of children: START with 24.
  2. 6 get off: SUBTRACT 6. (24 − 6 = 18)
  3. 10 get on: ADD 10. (18 + 10 = 28)
  4. Show the outcome: SAY the number.

Code: START with 24 → SUBTRACT 6 → ADD 10 → SAY the number. The outcome is 28 children.

Your turn

1

Are these events sequential or concurrent? You put on your boots. Then you put on your coat.

2

Are these events sequential or concurrent? A dog barks while the doorbell rings.

3

Beep is at column 1, row 1. Write code to get Beep to column 3, row 4.

4

Run the code: START with 30 → ADD 20 → SUBTRACT 5 → SAY the number

5

Beep and Boop both start at 0. On each beat, Beep adds 2 and Boop adds 3. What does each robot have after 4 beats?

6

Write code to solve: Ali has 15 stickers. He gets 5 more. How many does he have?

Hands-on activity

Two robots, one floor

You need: a grid on the floor (use tiles, or make one with tape or sticky notes, about 5 by 5 squares), two people to be robots, and a caller.

  1. Write two codes on paper, one for each robot. Each code has 4 steps.
  2. The caller says GO! then Beat 1, beat 2, beat 3, beat 4. On each beat, both robots do their next step at the same time.
  3. Did the robots end where you planned? Did they ever try to stand on the same square?
  4. Now write codes so the two robots meet on squares right next to each other at the end.

What to look for: each robot does exactly one step per beat, and nobody skips ahead.

Keep trying

Coders almost never get code right the first time. If the robot does not end where you planned, that is okay! Run it again slowly and find the step to change.

More practice: warm-up

1

What does concurrent mean?

2

What does sequential mean?

3

Run the code: START with 10 → ADD 10 → ADD 10 → SAY the number

4

Beep is at column 2, row 1. The code says ⬆ Move up 2. Where is Beep now?

5

Sequential or concurrent? The lights flash while the music plays.

More practice: on your own

1

Run the code: START with 50 → SUBTRACT 10 → SUBTRACT 10 → ADD 5 → SAY the number

2

Beep starts at column 1, row 1 and Boop starts at column 5, row 1. On each beat, Beep moves right 1 and Boop moves left 1. After 2 beats, where is each robot?

3

Write code to solve: A baker made 40 muffins. She sold 15. Then she baked 20 more. How many does she have?

4

Two snowmen are being built at the same time. Snowman A gets 1 snowball each minute. Snowman B gets 2 snowballs each minute. Both start with 0. After 3 minutes, how many snowballs does each have?

5

Write code for Beep to go from column 1, row 1 to column 4, row 2, using 2 steps.

Challenge

1

Beep starts at 0 and adds 5 each beat. Boop starts at 20 and adds 0 each beat (Boop stays still). After how many beats do they have the same number?

2

Beep is at column 1, row 1. Boop is at column 4, row 1. On each beat Beep moves right 1 and Boop moves left 1. Will they land on the same square? Explain.

3

Beep starts at column 1, row 1. Boop starts at column 4, row 1. Write concurrent code so that after 3 beats they are side by side on row 3. Each robot does one move of 1 square on each beat, and they must never land on the same square.

Check your understanding

Quiz

1.

What are concurrent events?

2.

Run the code: START with 20 → ADD 10 → SUBTRACT 5

3.

Which is an example of sequential events?

4.

Beep is at column 1, row 1. Which code moves Beep to column 4, row 1?

5.

Beep and Boop start at 0. Each beat, Beep adds 10 and Boop adds 5. After 3 beats, what does Beep have?

6.

When two robots run concurrent code, how should you run it?

For parents and teachers

In Grade 2, coding adds concurrent events: two characters or actions that run at the same time. Children write and run code on a grid and use code to solve math problems.

Common mistakes

  • Running all of one robot's code first, then the other's. Model beat-by-beat with two fingers or two toys.
  • Counting the starting square as a move.
  • Mixing up left and right. A sticker on your child's right hand helps.

How to help

  • Make a floor grid with tape and act out code with toys or family members.
  • Free block-coding apps for young children (for example ScratchJr) let characters run scripts at the same time. These are optional.
  • Celebrate fixing mistakes: that is what real coders do.