conditionals and flow

= ==

conditionals and flow

expression = expression

boolean

Returns 1 (true) if expression1 equals (has the same result) as expression2. May be placed before or between the two expressions to be compared. If the expression is complex (is a chain of primitives, procedures or mathematical operations), it needs to be surrounded by brackets (). See equalp

if expression1 = expression2 [print [These are the same!]]
if (xpos + 4) = (ypos - 13) [...]
print = 1 2
0

>

conditionals and flow

expression > expression

boolean

Returns 1 (true) if expression1 is greater than expression2. Symbols may be placed before or between the two expressions to be compared. If the expression is complex (is a chain of primitives, procedures or mathematical operations), it needs to be surrounded by brackets ().

if :frogs > 0 [print "ribbit]

<

conditionals and flow

expression < expression

boolean

Returns 1 (true) if expression1 is less than expression2. Symbols may be placed before or between the two expressions to be compared. If the expression is complex (is a chain of primitives, procedures or mathematical operations), it needs to be surrounded by brackets ().

if :ships < 1 [gameover]

>=

conditionals and flow

expression >= expression

boolean

Returns 1 (true) if expression1 is greater than or equal to expression2. May be placed before or between the two expressions to be compared. If the expression is complex (is a chain of primitives, procedures or mathematical operations), it needs to be surrounded by brackets ().

<=

conditionals and flow

expression <= expression

boolean

Returns 1 (true) if expression1 is less than or equal to expression2. May be placed before or between the two expressions to be compared. If the expression is complex (is a chain of primitives, procedures or mathematical operations), it needs to be surrounded by brackets ().

!=

conditionals and flow

expression != expression

boolean

Returns 1 (true) if expression1 is not equal to expression2. May be placed before or between the two expressions to be compared. If the expression is complex (is a chain of primitives, procedures or mathematical operations), it needs to be surrounded by brackets ().

and

conditionals and flow

comparison1 comparison2 (comparison1 comparison2 comparison3…)

boolean

Returns 1 (true) if both the provided expressions are also true 0 (false) if not.

print and 5 = 4 3 = 2
0

if and 1 = 1 2 = 2 [print [Both are true!]]
Both are true!

if (and 1=1 2=2 3=3) [print [All are true!]]
All are true!

case

conditionals and flow

word, longword, number, boolean or list | list

none

Compares the input with the container specified by the previous switch primitive, and if true performs the contents of the list provided. See case, otherwise

case "yes [print |Welcome aboard!|]

catch

conditionals and flow

name | list

none

Catch executes the provided list. If throw CODE(0x8001c4650)nameCODE(0x8001c4608) is called while the provided list is run, control returns to the first statement after the catch. If you provide “error as the name, catch catches errors instead.

catch "duck [repeat 100 [fd 20 throw "duck]] pr "quack
catch "error [repeat 100 [fd 20

dosleep

conditionals and flow

milliseconds (number) | list

none

Performs the provided list of instructions and then sleeps if and until the provided number of milliseconds has passed since the list started execution

dosleep 1000 [print "hello!]

dountil

conditionals and flow

expression | instructions (list)

none

Repeats the provided list of instructions until the provided expression is true.

make "counter 0 dountil :counter = 5 [make "counter :counter + 1 print :counter]
1
2
3
4
5

dowhile

conditionals and flow

expression | instructions (list)

none

Repeats the provided list of instructions while the provided expression is true.

make "counter 0 dowhile :counter CODE(0x8001c4650) 5 [make "counter :counter + 1 print :counter]
1
2
3
4
5

else

conditionals and flow

list

none

Executes the provided list of requests if the previous if comparison was false. else is not necessary if the list of requests immediately follows the list of requests specified for the true condition, but else allows for the requests to be executed later in the program flow.

if :container = 5 [do this] [do that]

else is not needed here, since if can take an else request list

if :container = 5 [do this]
print |I am the walrus|
else [do that]

but else can come later

eval

conditionals and flow

list

output

evaluates the expression in the supplied list and returns its output.

show eval [10 + 2]
12

every

conditionals and flow

counter (number) | count (number) | instructions [list]

none

Performs the list of supplied instructions every time the supplied count divides equally into the supplied counter. Shorthand for IF REMAINDER COUNTER COUNT []

every repcount 10 [print “moo]

for

conditionals and flow

[container start end (optional step)] (list) | [instructions to run] (list)

none

Repeats the list of requests counting from the start value to the end value, incrementing the value of :container by one (or the optionally provided step value) each time. The value stored in container remains after the for loop has finished executing.

for [i 1 15] [print :i]
1
2
3
...

for [i 1 10 2] [print :i]
1
3
5
...

foreach

conditionals and flow

container (word) | word or [list of items] | [list of requests]

none

Repeats the list of requests once for each item in the list of items while also changing the value of the specified container to the list item currently being processed. Note that the container name begins with a ” not a : when using foreach. Also, the value of container remains after the foreach loop has finished executing.

foreach "animal [goose horse sheep] [print {[Old Macdonald had a] :animal [E I E I O]}]
foreach "char "duck [print :char]

forever

forever

conditionals and flow

list of requests

none

Repeats the list of requests until terminated by another thread or halt.

forever [print [I am slowly going crazy 1 2 3 4 5 6 switch]]

go

conditionals and flow

word

none

Jumps to the execution point marked by label with the specified word. Similar to the BASIC GOTO command. Should be avoided if at all possible. See label.

label "start
print |This will repeat forever!|
go "start

if

conditionals and flow

expression | list

none

if can act both as an advisor (function) or a performer (command). If acting as a thinker and the provided expression resolves true, if returns the provided list. If acting as a doer and the provided expression resolves true, if executes the instructions in the provided list.

if 4 = 4 [forward 20]

show if 4 = 4 ["duck]

ifelse

conditionals and flow

expression | list | list

none

ifelse can act as both an advisor or a performer. If the provided expression resolves true, ifelse (advisor) returns the first list, otherwise it returns the second list. If ifelse is acting as a performer, it executes either the first list or the second. See if, else

ifelse :container = 5 [do this] [do that]

show ifelse :container = 5 ["this] ["that]

label

conditionals and flow

name (word)

none

Creates a ‘label’ with the specified name, which can be ‘jumped’ back to during execution using go. See go

label "start
go "start

loopcount

conditionals and flow

none

number

Returns the current repetition of a forever, while, until, dowhile, dountil or foreach loop. See repcount

forever [show loopcount]

oncefalse

conditionals and flow

boolean | instructions (list)

none

Preforms the provided list of instructions if the boolean is false once, and not again until the boolean becomes true and then false once more (for example, when the turtle crosses a boundary). To ‘flip’, oncefalse must see the boolean become true (be called when the boolean is true). See oncetrue

oncefalse :boolean [instructions]

oncetrue

conditionals and flow

boolean | instructions (list)

none

Preforms the provided list of instructions if the boolean is true once, and not again until the boolean becomes false and then true once more (for example, when the turtle crosses a boundary). To ‘flip’, oncetrue must see the boolean become false (be called when the boolean is false). See oncefalse

oncetrue :boolean [instructions]

onerror

conditionals and flow

list

none

Causes the calling turtle to execute the supplied list of instructions when an error is encountered rather than stop execution. It then continues execution from the point the error was encountered. onerror is ‘scoped’ to the procedure it is called from.

onerror [print "Caught!] show 1 / 0
Caught!

or

conditionals and flow

expression | expression

boolean

Returns true or false based on whether or not one or more of the supplied expressions are true. or takes two expressions by default, but can take more than two if the entire or statement is wrapped in brackets, eg: (or expr1 expr2 expr3 …) See and

show or 1 = 1 2 = 3
TRUE

otherwise

conditionals and flow

list

none

If no case statement has been satisfied since the last switch, execute the provided list. See switch, case

otherwise [pr |Eat a sandwich.|]

output op

conditionals and flow

input

output

Used by procedures to return output to a calling procedure or function. The input and output are effectively the same.

to duck
output "quack
end

print duck
quack

pause

conditionals and flow

none

none

Causes execution in the calling worker to pause and return to the prompt. Execution can be continued with co. Pause only works with the main (archon) worker. NOTE: pause DOES NOT WORK in interactive mode (only in a procedure!) See co

pause

repabove rpa

conditionals and flow

number

number

Returns the current iteration of a repeat loop at the specified level above the current repeat loop. For example, repabove 1 returns the repcount value of the repeat loop directly (one level) above the current repeat loop. If the repeat ‘scope’ specified by repabove does not exist, repabove returns 0. See repcount

show repabove 1

repcount rct

conditionals and flow

none

number

Returns the current iteration of a repeat loop. See repabove, loopcount

repeat 10 [print repcount]

repeat rpt

conditionals and flow

repetitions [list of requests to repeat]

none

Repeats the list of requests specified in square brackets the declared number of repetitions. See repcount

repeat 4 [forward 10 right 90]

reptotal

conditionals and flow

none

number

Returns the target number of loops inside a repeat. See repeat, repcount

repeat 10 [show reptotal]

switch

conditionals and flow

containername (word) or list

none

If the name of a container is given, switch sets up the specified container to be tested using subsequent case primitives. Note: the container name for switch is ‘absolute’, that is, prefaced with a double quote “. If you specify a container using a colon : the container used by switch will be the container named by the container specified with a colon! However, if a list is given, subsequent case statements will test against the first item in the list. Hence, you can use a ‘softlist’ to test against the value stored in a container or returned by a calculator.

switch "var
switch [testme donttestme]
switch {:var}

test

conditionals and flow

expression

none

Evaluates the provided expression and then holds a boolean result for use with iftrue and iffalse.

test :a CODE(0x8001c4608) 5

throw

conditionals and flow

target (name)

none

Diverts execution to the first statement after the target catch statement. Chiefly useful for returning to the procedure / level the catch was called from when several levels deep into subprocedures or other procedures, as label / go only work inside the same procedure. Providing “toplevel as a target effectively halts execution, as toplevel is the interactive prompt. See also catch, label, go

TO test
catch "cow [duck]
pr "moo
END

TO duck
pr "quack
frog
END

TO frog
pr "ribbit
throw "cow
END

try

conditionals and flow

list | list

none

Attempts to execute the instructions given in the first list, and if an error is encountered then executes the instructions in the second list. A one-off form of onerror. See onerror, throw, catch

try [repeat 10 [show 10 / (10 - repcount)]] [print |Divide by zero, silly!|]

until

conditionals and flow

comparison [list of requests]

none

Repeats the list of requests until the comparison is true.

make "count 1 until :count = 20 [inc "count]

when

conditionals and flow

comparison [requests]

none

Waits until the comparison is true, then executes the provided requests. This blocks execution; it is not a trigger. To use it as such, use newworker eg newworker [when minutes = 0 [toot 1000 60]]

when minutes = 0 [toot 1000 60]

while

conditionals and flow

comparison [instructions]

none

While the provided comparison is true, the supplied instructions will be repeatedly executed.

while 😡 CODE(0x8001c4650) 10 [make "x 😡 + 1 print :x]