|
LoopsA 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. 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 [ The command FORWARD 100 RIGHT 45 will be repeated 8 times. 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] [ 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" [ To make a loop that runs from 0 to 20, enter FOR :n IN RANGE 20
[action] 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. Like FOR, but loops until the condition is met. WHILE REPCOUNT <= 10 [ 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. This is not a procedure, but a command that terminates the loop. It is always used together with a condition (see below). Example: REPEAT [ 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. This will skip the command that follows CONTINUE and jump to the line after the command. An example:
REPEAT 8 [ 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. 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: PAUSE 500 ; Wait for 0,5 second. Is, not unexpectedly, used for making a pause in the program. The time is measured in milliseconds. ConditionsSome 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 [ 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 [ 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. |
© Context and design: Kolbjørn Stuestøl | Stuestøl homesite (in Norwegian) | Modified June 29 2016 |