Branching
if
Classic. Executes a block if a test is true
.
if <test> [ block ]
>> if 10 > 4 [print "large"]
large
unless
Same as if not
. Executes block only if a test is false
.
unless <test> [ block (if test false
) ]
>> unless 10 > 4 [print "large"]
== none
>> unless 4 > 10 [print "large"]
large
either
A new name for the classic if-else. Executes the first block if the test is true
or executes the second block if the test is false
.
either <test> [true
block] [false
block]
>> either 10 > 4 [print "bigger"] [print "smaller"]
bigger
>> either 4 > 10 [print "bigger"] [print "smaller"]
smaller
switch
Executes the block correspondent to a given value.
twenty
>>
switch/default
If the program does not find a match, executes a default block.
none of them
>>
case
Makes a series of tests, executing the block corresponding to the first true
test.
this is it!
>>
case/all
Executes all the true
tests.
this is it!
also ok!
>>
catch & throw
Catch and throw may be used to create complex control structures. In its simplest form, catch
receives a return from one of many throws:
just right
>>
Boolean branching
all
Evaluates all expressions in a block. If one evaluation returns false
, it returns none
, otherwise returns the result of the last evaluation.
>> john: "boy"
== "boy"
>> alice: "girl"
== "girl"
>> all [john = "boy" alice = "girl" 10 + 3] ;all true, the last evaluation is returned.
== 13
>> all [john = "boy" alice = "boy" 10 + 3] ; alice = "boy" is false!
== none
any
Evaluates each expression in a block in and returns the first resulting value that is not false
. If all resulting values are false
it returns none
.
>> john: "boy"
== "boy"
>> alice: "girl"
== "girl"
>> any [john = "girl" alice = "girl" 10 + 3] ;alice = "girl" is not false: return it!
== true
>> any [john = "girl" 10 + 3 5 > 2] ; 10 + 3 is not false: return it!
== 13