Dr Logo for the Tatung Einstein, showing a rosette drawn by the turtle
← Back to Courses
module
5

Doing it more than once

Introduction

The square in S4 took nine lines, and six of them were the same two instructions over and over. Repetition like that is worth getting rid of - not to save typing, but because a shape written as "do this four times" says what the shape is in a way that a list of moves does not.

Saying it once

repeat takes two things: a number, and a list of instructions in square brackets.

repeat 4 [fd 50 rt 90]

Four times, do fd 50 and then rt 90. That is the whole square.

Remember the brackets are on the arrow keys - left arrow for [, right arrow for ].

The instructions inside the brackets are an ordinary Logo line; there is nothing special about being inside a repeat. You can put as many instructions in there as you like, separated by spaces.

Why the angle is what it is

The square works because of a piece of arithmetic worth having in your head: the turtle turns 90 degrees four times, which is 360 degrees in total - one whole turn. It ends up facing exactly the way it started.

That is not a coincidence, and it is the rule for any shape of this kind. If you want a closed shape with a given number of sides, the turn at each corner has to be 360 divided by the number of sides. Five sides means turning 72 degrees. Ten sides means 36.

Get that wrong and the shape does not close, which you can see for yourself in the edits below.

The code

Starting from

A fresh boot, at the ? prompt.

What you should see

The same square as S4 - fifty steps on a side, up and to the right of where the turtle started. The turtle finishes back at the corner it began from, and this time it is pointing up again, because four turns of 90 have brought it all the way round.

The square drawn by repeat, with the turtle back at its starting corner

Three lines instead of nine, and the same picture pixel for pixel.

Change one thing

  • Change the 4 to a 3. You might expect a triangle. Run it and see what you actually get, then work out why - the angle is the clue.
  • Change the 90 to 60, leaving the 4 alone. Does the shape close? Use the rule above to work out what count 60 would need to go with.
  • Change the 50 to 100. The square gets bigger, but not four times bigger in the way you might expect. What has happened to the top of it, and why? S8 is about that.

When it goes wrong

Symptom Cause
Pressing [ does nothing There is no bracket key. Left arrow gives [, right arrow gives ].
repeat draws nothing and the line seems to be ignored Check there is a space between every instruction inside the brackets. Dr Logo needs them to tell the words apart.
The shape does not join up The turns do not add up to 360 degrees. Number of sides times the angle should make a whole turn.
Part of the shape is missing off the top or the side It has run off the screen. There are only about 95 steps above the middle and 128 to either side.

Summary

repeat takes a number and a list of instructions in square brackets, and runs the list that many times. A closed shape with n sides turns 360 divided by n at each corner. Three lines now do what nine did.

Next

S6, lifting the pen - moving the turtle without leaving a line behind it, so a drawing can have gaps in it.

Get the Newsletter

New guides, disk images and community finds, roughly once a quarter. No spam, we promise, this isn't Tatung's marketing department.
Your subscription could not be saved. Please try again.
Your subscription has been successful.

Newsletter

Subscribe to our newsletter and stay updated.