0

Short programs with loops

  1. repeat 4 times
    move up

    Fewer blocks

    The robot likes short programs. Four move_up() lines take it up 4 squares. One loop does the same job in just 2 lines.

  2. repeat 5 times
    move right

    One straight line, one loop

    The flag is 5 squares right, in a straight line. One loop does the whole line. That is repeat 5: with move_right() inside.

  3. repeat 3 times
    move down
    repeat 4 times
    move right

    A turn starts a new loop

    When the robot turns, start a new loop. This robot goes down 3, then right 4. That is two loops, one for each straight part.

  4. repeat 4 times
    move right
    repeat 3 times
    move up

    Find the gap

    Rocks block the way up. Find the gap first. Here the gap is on the right side. Go right 4, then up 3. Two loops get past the rocks.

  5. repeat 3 times
    move right
    repeat 2 times
    move up
    repeat 3 times
    move right

    Count the straight parts

    Count the straight parts of the path. This path has three straight parts. So it needs 3 loops.

For parents

Goal: Your child learns to make programs shorter by using one loop for each straight line of moves.

  • Ask: where does the robot turn? Each turn starts a new loop.
  • Ask your child to count the lines in two programs and say which one is shorter.
  • In the labs your child writes short programs with loops and fixes a loop with the wrong number. Fewer lines earn more stars.

Away from the screen: Make a path on the floor with tape that has one turn. Your child says the program, like "repeat 4: step forward, repeat 2: step left", then walks it.

Coding · Loops
Back to top