Turtle LibreLogo
Tilbake til startsida
 

 

Loops

A loop is a program sequence or function repeated several times. The number of passes is determined either as a given value or until a certain conditions are met.

LibreLogo has several loop functions.

REPEAT x

The number x sets the number of times the function is repeated. If you omit x, the function will go forever, or in practice to the computer is turned off or you kill the program. An example:

REPEAT 8 [
  FORWARD 100 RIGHT 45
]

The command FORWARD 100 RIGHT 45 will be repeated 8 times.

FOR … IN

This function retrieves data one by one from a list of data. The loop ends when there are no more elements in the list.

FOR :x IN [10, 50, 80, 120, 180] [
  FORWARD :x
  LEFT 90
]

will add data from the list [10, 50, 80, 120, 180] one at the time to the variable :x and execute commands in turn. First command will then be FORWARD 10 LEFT 90. Next will be FORWARD 50 LEFT 90 and so forth until the loop is ended with FORWARD 180 LEFT 90.

Also text strings are in this case a list so it is possible to get data from a string.

FOR x IN "This is a text string" [
  LABEL x
  FORWARD 10
]

will print the text letter by letter on the screen.

To make a loop that runs from 0 to 20, enter FOR :n IN RANGE 20 [action]
This is the same as writing FOR n=0 to 19 in other programming languages.

REPCOUNT

Perhaps in the example above you prefer to use the value of the counter instead of values ​​from the list. You must then replace :n with REPCOUNT. REPCOUNT returns the value of the element counter and the function will repeat as many times as there are elements in the list.

REPCOUNT can be used in the loops REPEAT, FOR and WHILE.

WHILE condition

Like FOR, but loops until the condition is met.

WHILE REPCOUNT <= 10 [
  FORWARD 45
  RIGHT 36
]

The loop will be executed as long as repcount is less than or equal to 10. Repcount counts from 1, not from 0 as usual in other programming languages. Therefore the loop will be executed 10 times and render a polygon with 10 sides.

BREAK

This is not a procedure, but a command that terminates the loop. It is always used together with a condition (see below).

Example:

REPEAT [
  BACK 50 LEFT 45
  IF REPCOUNT = 20 [ BREAK ]
]

In this example, the procedure will be repeated until REPCOUNT equals 20. Thus actually the same as REPEAT 20.

Return from the procedure. Used together with a condition.

CONTINUE

This will skip the command that follows CONTINUE and jump to the line after the command. An example:

continue

REPEAT 8 [
  BACK 60 RIGHT 45
  IF REPCOUNT % 2 = 0 [ CONTINUE ]
  SQUARE 15
]

The term REPCOUNT % 2 = 0 is TRUE if REPCOUNT divided with 2 gives remainder 0. (Refer to the chapter on mathematics). If repcount is an even number the expression inside the brackets will be performed. In this case this is CONTINUE, which means that the procedure is performed from the start, without bothering about the rest of the commands in the procedure. The example will draw a polygon with 8 sides with a square in every second corner. See image at right.

STOP

As BREAK but returns from a procedure.

Example:

It is meaningless to try to render a polygon with less than 3 sides. So if the call asks for a polygon with less than three sides, the function below refuses to render a figure:
TO polygon :edges :size :color :line
IF :edges < 3 [ STOP ] ; return if fewer than 3 sides, else continue
  PENCOLOR :color
  PENWIDTH :line
  REPEAT :edges [
    FORWARD :size
    LEFT 360 / :edges
  ]
END

PAUSE

PAUSE 500 ; Wait for 0,5 second.

Is, not unexpectedly, used for making a pause in the program. The time is measured in milliseconds.

Conditions

Some commands can change function if certain conditions are fulfilled. In LibreLogo conditions are determined with the command IF. The test follows this pattern: IF :a < 10 [ perform if TRUE ] [ perform if FALSE ]

This means that if :a is less than 10, the first command will be executed, if :a is equal to or greater than 10, the second command will be executed. These blocks must be on the same line.

Here is an example of the use of terms:

REPEAT 10 [
  FORWARD 50
  IF REPCOUNT <= 5 [ LEFT 90 ] [ RIGHT 90 ]
]

This means that if the counter is less than or equal to 5, the turtle will be turned to the left, else to the right.

In conditional expressions you can also use the logical operators AND, OR and NOT.

REPEAT 10 [
  FORWARD 50
  IF REPCOUNT = 5 OR REPCOUNT = 7 [ LEFT 90 ] [ RIGHT 90 ]
]

The command with the condition can be read as follows: If repcount is equal to 5 or 7, perform LEFT 90, else perform RIGHT 90. Exchanging OR with AND, the condition is never met and RIGHT 90 is executed each time. This is because repcount in this case must be both equal to 5 and equal to 7 for that condition to be met.

Using the operator NOT requires a bit logical thinking.
IF NOT REPCOUNT = 5 AND NOT REPCOUNT = 7 [ LEFT 90 ] [ RIGHT 90 ].
This means that if repcount is 1, 2, 3, 4, 6, 8 or 9, the LEFT 90 will be performed. If repcount is 5 or 7, the RIGHT 90 will be performed.


© Context and design: Kolbjørn StuestølStuestøl homesite (in Norwegian)Modified June 29 2016