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

Coding with Loops and Finding Bugs

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

Words to know

code
A list of steps, in order, that tells a robot or computer what to do.
loop
Code that repeats. "repeat 4" means do it 4 times.
bug
Something wrong in the code that stops it from working.
debug
To find a bug and fix it.

Saying it once ๐Ÿค–

In Grade 1 you gave robots steps like forward, turn left, forward. Code is a list of steps in order.

What if the robot must go forward 4 times? You could write forward, forward, forward, forward. Or you could use a loop:

repeat 4 [forward]

This means: do forward 4 times. Loops make code shorter, so it is easier to read and check.

StartGoalThe robot faces right at the start.
A 5 by 3 grid. The robot starts in the bottom-left square, facing right. The Goal is in the top-right square.
Worked example

Get to the Goal

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

  1. Long code: forward, forward, forward, forward, turn left, forward, forward. That is 7 steps.
  2. Look for repeats: forward comes 4 times in a row, then 2 times in a row.
  3. Short code: repeat 4 [forward], turn left, repeat 2 [forward]. That is only 3 lines.
  4. Test it: Trace with your finger: right 4, turn to face up, up 2. You are on the Goal!

repeat 4 [forward], turn left, repeat 2 [forward] takes the robot to the Goal. ๐ŸŽฏ

1. repeat 4 [forward]
2. turn left
3. repeat 2 [forward]

The short code, with two loops.

Bugs happen ๐Ÿ›

A bug is something wrong in the code. Every coder makes them, even grown-up experts! To debug:

  1. Go through the code one step at a time, with your finger or your body.
  2. Find the step where the robot goes the wrong way or stops in the wrong place.
  3. Fix that step, then test again.

This is trial and error, just like the paper bridge!

Worked example

Spot the bug

Jonah writes: repeat 3 [forward], turn left, repeat 2 [forward]. The robot ends up one square to the left of the Goal. What is the bug?

  1. Step 1: repeat 3 [forward]. The robot moves right 3 squares. It is in the 4th square, but it needed to reach the 5th. โœ—
  2. Step 2: turn left. It now faces up. โœ“
  3. Step 3: repeat 2 [forward]. It goes up 2, but it is one column too early.
  4. Fix: Change repeat 3 to repeat 4, then test again. Now it lands on the Goal. โœ“

The bug was repeat 3. The robot needed repeat 4 to go far enough.

Turns stay put

Turning never moves the robot to another square. It just points the robot a new way. Not sure which way is left? Stand up, face the same way as the robot, and turn your own body.

Hands-on activity

Human robot

  1. Make a grid on the floor with tape, 5 squares by 3 squares, or draw one on paper.
  2. Put a small toy on one square. That is the Goal.
  3. One player is the coder and writes loop code on a card. The other player acts as the robot.
  4. The robot does exactly what the code says, nothing more.
  5. Missed the Goal? Debug the code together and try again.
  6. Trade jobs.
True story

Almost 200 years ago, in 1843, a woman named Ada Lovelace wrote steps for a calculating machine that was never fully built. Many people call her the first computer programmer! ๐Ÿ’ป

Let's practise

1

What does repeat 5 [forward] mean?

2

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

3

What is a bug?

4

When the robot turns, does it move to another square?

5

How many forward steps are in: repeat 3 [forward], turn left, repeat 4 [forward]?

6

What does debug mean?

More practice

1

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

2

Code: repeat 2 [forward]. The robot must go 5 squares. Find the bug and say how to fix it.

3

The long code had 7 steps. The short code had 3 lines. How many lines shorter is the short code?

4

Why do coders like loops?

Check your understanding

Quiz

1.

What does a loop do in code? It ...

2.

Which is the same as forward, forward, forward, forward?

3.

The robot needs to go 4 squares, but the code says repeat 3 [forward]. This is ...

4.

How many forward steps are in: repeat 5 [forward], turn left, repeat 3 [forward]?

For parents and teachers

How to help at home: Play "human robot" with simple commands: forward, turn left, turn right and repeat. Write a route on paper and ask your child where a loop could replace steps that repeat. No device is needed; unplugged coding builds the same thinking.

Common mistakes: Mixing up left and right (a sticker on the right hand helps), thinking a turn also moves the robot, counting squares wrong, and getting upset about bugs. Remind your child that all coders debug, and finding the bug is like solving a puzzle.