Spis poleceń › Conditionals and Flow — poleceń: 50
=
Także: ==
Wejście: expression = expression
Wyjście: 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
>
Wejście: expression > expression
Wyjście: 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]
<
Wejście: expression < expression
Wyjście: 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]
>=
Wejście: expression >= expression
Wyjście: 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 ().
<=
Wejście: expression <= expression
Wyjście: 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 ().
!=
Wejście: expression != expression
Wyjście: 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
Wejście: comparison1 comparison2 (comparison1 comparison2 comparison3…)
Wyjście: 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
Wejście: word, phrase, number, boolean or list | list
Wyjście: 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
Wejście: name | list
Wyjście: 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
Wejście: milliseconds (number) | list
Wyjście: 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
Wejście: expression | instructions (list)
Wyjście: 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
Wejście: expression | instructions (list)
Wyjście: 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
Wejście: list
Wyjście: 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
Wejście: boolean | list
Wyjście: 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
Wejście: list
Wyjście: output
evaluates the expression in the supplied list and returns its output.
show eval [10 + 2]
12
every
Wejście: counter (number) | count (number) | instructions [list]
Wyjście: 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
Wejście: [container start end (optional step)] (list) | [instructions to run] (list)
Wyjście: 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
Wejście: container (word) | word or [list of items] | [list of requests]
Wyjście: 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
Wejście: list of requests
Wyjście: 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
Wejście: word
Wyjście: 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
Wejście: procedure (word) | label (word)
Wyjście: none
Executes the specified procedure starting from the specified label. See go, label
gofrom "myproc "label
if
Wejście: expression | list
Wyjście: 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
Wejście: expression | list | list
Wyjście: 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
Wejście: expression | true-result | false-result
Wyjście: 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
Wejście: list
Wyjście: 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
Wejście: list
Wyjście: 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
Wejście: name (word)
Wyjście: 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
Wejście: level (number)
Wyjście: number
Returns the loopcount of the specified level above the current loop. See loopcount, repabove
forever [repeat 5 [show loopabove 1]]
loopcount
Wejście: none
Wyjście: 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
Wejście: boolean | instructions (list)
Wyjście: 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
Wejście: boolean | instructions (list)
Wyjście: 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
Wejście: boolean | instructions (list)
Wyjście: 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
Wejście: list
Wyjście: 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
Wejście: expression | expression
Wyjście: 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
Wejście: list
Wyjście: 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
Także: op
Wejście: input
Wyjście: 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
Wejście: none
Wyjście: 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
Także: rpa
Wejście: number
Wyjście: 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
Także: rct
Wejście: none
Wyjście: number
Returns the current iteration of a repeat loop. See repabove, loopcount
repeat 10 [print repcount]
repeat
Także: rpt
Wejście: repetitions [list of requests to repeat]
Wyjście: none
Powtarza listę instrukcji w nawiasach kwadratowych podaną liczbę razy. Zobacz repcount
repeat 4 [forward 10 right 90]
reptotal
Wejście: none
Wyjście: number
Returns the target number of loops inside a repeat. See repeat, repcount
repeat 10 [show reptotal]
return
Wejście: none
Wyjście: 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
Wejście: containername (word) or list
Wyjście: 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
Wejście: expression
Wyjście: none
Evaluates the provided expression and then holds a boolean result for use with iftrue and iffalse.
test :a > 5
throw
Wejście: target (name)
Wyjście: 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
Wejście: list | list
Wyjście: 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
Wejście: expression | list
Wyjście: 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
Wejście: comparison [list of requests]
Wyjście: none
Repeats the list of requests until the comparison is true.
make "count 1 until :count = 20 [inc "count]
when
Wejście: comparison [requests]
Wyjście: 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
Wejście: comparison [instructions]
Wyjście: none
While the provided comparison is true, the supplied instructions will be repeatedly executed.
while :x < 10 [make "x :x + 1 print :x]