0

Count with a loop

  1. steps = 0

    Start at 0

    The robot wants to count its steps. First it makes a box with 0 inside. Nothing is counted yet. A box used this way is a counter.

  2. set steps to 0
    repeat 3 times
    add 1 to steps
    say steps

    Add 1 each turn

    Put steps = steps + 1 inside a loop. Each turn adds 1. The loop runs 3 times, so steps ends at 3. say(steps) runs once, after the loop.

    1. 10
      start
    2. 21
      turn 1
    3. 32
      turn 2
    4. 43
      turn 3

    Watch the box

    Write the number after each turn. It goes 0, then 1, 2 and 3. The last number is what the robot says.

  3. set steps to 0
    repeat 4 times
    move right
    add 1 to steps
    say steps

    The robot counts as it walks

    Now the loop has two lines inside. Each turn, the robot moves right and adds 1. It walks 4 squares, so it says 4.

  4. say after the loop
    says 3 once
    say inside the loop
    says 1, 2, 3

    Where say goes

    Where you put say matters. After the loop, it says one number at the end. Inside the loop, it says a number every turn.

For parents

Goal: Your child learns to use a loop to add to a variable, and to tell the number it ends on.

  • Have your child write the number in the box on paper after each turn of the loop.
  • Ask: is say inside the loop or after it? How many numbers will it say?
  • The labs ask your child to predict a count, fix a broken counter, then build one. Ask where each block lives: before, inside or after the loop.

Away from the screen: Act out claps = 0, then repeat 4: clap and add 1. Your child claps and counts out loud, then says the final number.

Coding · Variables and a First Program
Back to top