Grade by GradeEssential student suppliesSign in
Applied Design, Skills, and Technologies ยท Design and Make: Grade 2 Design Detectives

Coding with Repeat and Fixing Bugs

BC ADST 2: explore technologiesAbout 35 minutes
Learning goals
  • I can write code for a robot using forward and turn.
  • I can use a repeat (loop) to make code shorter.
  • I can find and fix a bug in code.

Words to know

code
Steps, in order, that tell a robot or computer what to do.
loop
A part of code that repeats. We say "repeat 3" to do something 3 times.
bug
A mistake in code.
debug
To find and fix a bug.

Code, then shorter code ๐Ÿค–

In Grade 1 you gave robots directions like forward, forward, turn right. Code is just steps in order.

Sometimes the same step comes again and again. Instead of writing forward, forward, forward, coders use a loop:

repeat 3 [forward]

It means: do forward 3 times. Shorter code is easier to read and to check!

GoalStartRobot faces upat the start.
The robot starts at the bottom left, facing up. It must reach the Goal.
Worked example

Code the path

The robot faces up. It must go up 3 squares, turn right, then go 2 squares to the Goal. Write the long code, then the short code with loops.

  1. Long code: forward, forward, forward, turn right, forward, forward. That's 6 steps.
  2. Spot the repeats: forward comes 3 times, then 2 times.
  3. Short code: repeat 3 [forward], turn right, repeat 2 [forward]. That's only 3 lines.
  4. Test it: Move your finger on the grid. Up 3, turn right, over 2. You land on the Goal!

repeat 3 [forward], turn right, repeat 2 [forward] gets the robot to the Goal. ๐ŸŽฏ

1. repeat 3 [forward]
2. turn right
3. repeat 2 [forward]

The short code with loops.

Bugs and debugging ๐Ÿž

A bug is a mistake in code. Bugs are normal! All coders get them. To debug:

  1. Run the code one step at a time with your finger.
  2. Find the step where the robot goes the wrong way.
  3. Fix that step and test again.

That's trial and error, just like the foil boat!

Worked example

Find the bug

Zoe writes: repeat 3 [forward], turn left, repeat 2 [forward]. The robot does not reach the Goal. What is the bug?

  1. Step 1: repeat 3 [forward]. The robot goes up 3 to the top row. โœ“
  2. Step 2: turn left. Now it faces left, toward the edge of the grid. โœ— The Goal is to the right!
  3. Fix: Change turn left to turn right.
  4. Test again: Up 3, turn right, over 2. It reaches the Goal. โœ“

The bug was turn left. It should be turn right.

Turning

A turn does not move the robot to a new square. It only changes the way the robot faces. Pretend you are the robot: stand up and turn your own body to check!

Hands-on activity

Be the robot

  1. Make a 4 by 4 grid on the floor with tape, or draw one on paper.
  2. Put a toy on one square as the Goal.
  3. One person writes code with loops on a card. The other person is the robot.
  4. The robot follows the code exactly.
  5. If you miss the Goal, debug together and try again.
  6. Switch jobs.
True story

Long ago, in 1947, people working on a giant computer found a real moth stuck inside it! They taped it in their notebook. People had already called mistakes "bugs," but now they had a real one. ๐Ÿฆ‹

Let's practise

1

What does repeat 4 [forward] mean?

2

Write this with a loop: forward, forward, forward, forward, forward.

3

What is a bug?

4

Does a turn move the robot to a new square?

5

How many forward steps are in: repeat 2 [forward], turn right, repeat 3 [forward]?

6

What does debug mean?

More practice

1

Write the long code for: repeat 2 [forward], turn left, forward.

2

Code: repeat 3 [forward]. The robot needs to go 4 squares. What is the bug and the fix?

3

The long code has 6 steps. The short code has 3 lines. How many fewer lines is that?

4

Why do coders use loops?

Check your understanding

Quiz

1.

A loop in code ...

2.

Which is the same as forward, forward, forward?

3.

A robot faces up and must go to a square on its right. It turns left. This is ...

4.

How many forward steps are in: repeat 4 [forward], turn right, repeat 2 [forward]?

For parents and teachers

How to help at home: Play "robot" with simple commands: forward, turn left, turn right, and repeat. Write code on sticky notes and have your child spot where a loop could replace repeated steps. You don't need a device; unplugged coding builds the same thinking.

Common mistakes: Mixing up left and right (put a sticker on the right hand), thinking a turn also moves the robot forward, and getting upset by bugs. Remind your child that every coder debugs, and that finding the bug is part of the fun.