Referencia › Conditionals and Flow — 50 comandos


=

También: ==

conditionals and flow

Entrada: expression = expression
Salida: 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

Entrada: expression > expression
Salida: 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

Entrada: expression < expression
Salida: 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

Entrada: expression >= expression
Salida: 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

Entrada: expression <= expression
Salida: 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

Entrada: expression != expression
Salida: 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

Entrada: comparison1 comparison2 (comparison1 comparison2 comparison3…)
Salida: 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

Entrada: word, phrase, number, boolean or list | list
Salida: 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

Entrada: name | list
Salida: none

Catch executes the provided list. If throw <name> 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

Entrada: milliseconds (number) | list
Salida: 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. If the first parameter is provided in the form of a single-item list, dosleep will instead attempt to average the given number of milliseconds taken to execute the contents of the second parameter such that if it takes longer than the provided number of milliseconds it will subtract that from the time allowed for the next iteration. See sleep, wait

dosleep 1000 [print "hello!]
dosleep [1000] [print |this attempts to average the execution speed of these instructions|]

dountil

conditionals and flow

Entrada: expression | instructions (list)
Salida: 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

Entrada: expression | instructions (list)
Salida: none

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

make "counter 0 dowhile :counter < 5 [make "counter :counter + 1 print :counter]
1
2
3
4
5

else

conditionals and flow

Entrada: list
Salida: 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

elsif

conditionals and flow

Entrada: boolean | list
Salida: none

If the previous if statement is false, check the given boolean and if true, execute the given list. See if, else

if 1 = 2 [do this]
elsif 2 = 2 [do that]

eval

conditionals and flow

Entrada: list
Salida: output

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

show eval [10 + 2]
12

every

conditionals and flow

Entrada: counter (number) | count (number) | instructions [list]
Salida: none

Performs the list of supplied instructions every time the supplied count divides equally into the supplied counter. Shorthand for IF REMAINDER COUNTER COUNT []. Has its own loopcount, which is reset by the reset primitive. See loopcount

every repcount 10 [print “moo]

for

conditionals and flow

Entrada: [container start end (optional step)] (list) | [instructions to run] (list)
Salida: 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, and can travel if go -> label -> return is used. See foreach, which iterates through a list of values.

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

foreach

conditionals and flow

Entrada: container (word) | word or [list of items] | [list of requests]
Salida: 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

conditionals and flow

Entrada: list of requests
Salida: 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

Entrada: word
Salida: 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

gofrom

conditionals and flow

Entrada: procedure (word) | label (word)
Salida: none

Executes the specified procedure starting from the specified label. See go, label

gofrom "myproc "label

if

conditionals and flow

Entrada: expression | list
Salida: 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. See unless, which is an inverse if

if 4 = 4 [forward 20]
show if 4 = 4 ["duck]

ifelse

conditionals and flow

Entrada: expression | list | list
Salida: 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 and iff, a more obvious advisor (functional) version of ifelse.

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

iff

conditionals and flow

Entrada: expression | true-result | false-result
Salida: result

The functional form of if – if the expression provided to iff is true, iff returns the first result, otherwise iff returns the second result.

show iff 1 = 2 "true "false
false

iffalse

conditionals and flow

Entrada: list
Salida: none

If the previous use of the test primitive rendered a false result, executes the provided list of instructions. See test, iftrue

iffalse [print "moo]

iftrue

conditionals and flow

Entrada: list
Salida: none

if the previous use of the test primitive rendered a true result, executes the provided list of instructions. See test, iffalse

iftrue [print "oink]

label

conditionals and flow

Entrada: name (word)
Salida: none

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

label "start
go "start

loopabove

conditionals and flow

Entrada: level (number)
Salida: number

Returns the loopcount of the specified level above the current loop. See loopcount, repabove

forever [repeat 5 [show loopabove 1]]

loopcount

conditionals and flow

Entrada: none
Salida: number

Returns the current repetition of a forever, while, until, dowhile, dountil, repeat, every or foreach loop. See repcount which only works for repeats

forever [show loopcount]

once

conditionals and flow

Entrada: boolean | instructions (list)
Salida: none

Performs the provided list of instruction if the boolean is true once and once only in the current scope. However, if execution leaves and returns to that scope, once will trigger (once) again.

once repcount = 10 [print "Boo!]

oncefalse

conditionals and flow

Entrada: boolean | instructions (list)
Salida: 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

Entrada: boolean | instructions (list)
Salida: 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

Entrada: list
Salida: 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

Entrada: expression | expression
Salida: 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

Entrada: list
Salida: 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

También: op

conditionals and flow

Entrada: input
Salida: 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

Entrada: none
Salida: 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

También: rpa

conditionals and flow

Entrada: number
Salida: 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

También: rct

conditionals and flow

Entrada: none
Salida: number

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

repeat 10 [print repcount]

repeat

También: rpt

conditionals and flow

Entrada: repetitions [list of requests to repeat]
Salida: none

Repite la lista de instrucciones entre corchetes el número de veces indicado. Véase repcount

repeat 4 [forward 10 right 90]

reptotal

conditionals and flow

Entrada: none
Salida: number

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

repeat 10 [show reptotal]

return

conditionals and flow

Entrada: none
Salida: none

Returns to the next command after the last executed 'go'. Does nothing if there are no prior 'go's on the 'stack' (the history of go 'jumps')

return

switch

conditionals and flow

Entrada: containername (word) or list
Salida: 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

Entrada: expression
Salida: none

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

test :a > 5

throw

conditionals and flow

Entrada: target (name)
Salida: 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

Entrada: list | list
Salida: 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!|]

unless

conditionals and flow

Entrada: expression | list
Salida: none

Performs the provided list of commands if the expression is false. This is the inverse behavior of if. See if

unless 3 = 4 [forward 20]
show unless 3 = 4 ["duck]

until

conditionals and flow

Entrada: comparison [list of requests]
Salida: none

Repeats the list of requests until the comparison is true.

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

when

conditionals and flow

Entrada: comparison [requests]
Salida: 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

Entrada: comparison [instructions]
Salida: none

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

while :x < 10 [make "x :x + 1 print :x]