Level 1Unit B · Session 7
7

Unit B · Telling a computer what to do

Repeat and forever

What we are making: a sprite that draws a square, and one that never stops moving — both without writing the same block twenty times.

The idea

Suppose you want the cat to move ten times. You could drag ten move blocks. It would work. It would also be slow to build, impossible to read, and a nightmare to change.

Instead you say it once and wrap it in a loop.

The long way — works, but do not
move 50 steps
move 50 steps
move 50 steps
move 50 steps
The same thing, said once
repeat 4
move 50 steps

Both do exactly the same. The second one is easier to read, faster to build, and if you want forty instead of four you change one number.

New words
LoopA block that repeats what is inside it.
RepeatLoops a set number of times, then moves on.
ForeverLoops until you press stop. Never moves on.
Inside the loopThe blocks held by the C-shape. Those are the ones that repeat.

Do it — draw a square

This is the classic, and it is worth doing properly because it shows loops doing real work.

A square, in seven blocks
when green flag clicked
go to x: 0 y: 0
pen down
repeat 4
move 100 steps
turn 90 degrees
pen up
📷
images/l1-s7-script-square.png
Screenshot: the square script, with the two blocks clearly INSIDE the repeat C-shape
  1. Build it. The two blocks after repeat 4 must be inside the C-shape, not under it.
  2. Run it. You should get a square.
  3. Change repeat 4 to repeat 3 and turn 90 to turn 120. Run it. A triangle.
  4. Try repeat 5 with turn 72. Then repeat 6 with turn 60.
  5. Look at the pattern. What do the two numbers always multiply to?
💡 Tip
The two numbers always multiply to 360. That is the whole of turtle geometry in one sentence, and you just worked it out yourself rather than being told.
📷
images/l1-s7-stage-shapes.png
Screenshot: the stage showing a drawn square, and beside it a triangle and a pentagon
⚠ Careful
If the blocks are under the loop rather than inside it, the shape will not draw. Look carefully at where the C-shape wraps around them.

Do it — forever

Forever is a loop with no number. It never ends, and nothing after it ever runs.

A sprite that never stops
when green flag clicked
forever
move 5 steps
if on edge, bounce
📷
images/l1-s7-script-forever.png
Screenshot: the forever script — note the loop has no number in it
  1. Build it and run it. The cat bounces around forever.
  2. Press the red stop button. That is the only way to stop a forever loop.
  3. Add turn 3 degrees inside the loop. Run it. What shape does it travel in now?
  4. Try putting a say block after the forever loop. Run it. It never speaks. Work out why.
🔧 If it goes wrong
Nothing after a forever block ever runs. If you find blocks that seem to be ignored, check whether they are stranded below a forever loop — it catches everybody once.

Try it

  1. Draw a shape with 8 sides. Work out the turn yourself.
  2. Make a sprite that moves in a growing spiral. Hint: put a change block inside the loop.
  3. Put a loop inside another loop. Draw four squares in a row.

What you learned

  • A loop says something once and runs it many times.
  • Repeat has a number. Forever does not.
  • Blocks must be inside the C-shape to repeat.
  • Nothing after a forever loop ever runs.
  • Sides multiplied by turn always makes 360.
Challenge optional — only if you finish early
  • Make a sprite that walks to the right, then walks back, forever, without using if on edge bounce.
6. Your first blocks