Branching


native! if  Red-by-example    MyCode4fun

Executes a block if a test is true.

if <test> [ block ]

>> if 10 > 4 [print "large"]
large

Remember from the Datatypes chapter that everything that is not false , off or no is considered true:

>> if "house" [print "It's true!"]
It's true!

>> if 0 [print "It's true!"]
It's true!

>> if [] [print "It's true!"]
It's true!

>> if [false] [print "It's true!"]  ;bizarre!
It's true!

native! unless  Red-by-example    MyCode4fun

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

native! either  Red-by-example    MyCode4fun

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

native! switch  Red-by-example    MyCode4fun

Executes the block correspondent to a given value.

Red[]


switch 20 [

       10 [print "ten"]

       20 [print "twenty"]

       30 [print "thirty"]

]


twenty


/default

If the program does not find a match, executes a default block.

Red[]


switch/default 15 [

       10 [print "ten"]

       20 [print "twenty"]

       30 [print "thirty"]

][ print "none of them"]        ;default block


none of them

native! case  Red-by-example    MyCode4fun

Makes a series of tests, executing the block corresponding to the first true test.

Red[]


case [

       10 > 20 [print "not ok!"]

       20 > 10 [print "this is it!"]

       30 > 10 [print "also ok!"]

]


this is it!

/all

Executes all the true tests.

Red[]


case/all [

       10 > 20 [print "not ok!"]

       20 > 10 [print "this is it!"]

       30 > 10 [print "also ok!"]

]


this is it!
also ok!

native! catch & throw   Red-by-example  

Catch and throw may be used to create complex control structures. In its simplest form, catch receives a return from one of many throws:

Red[]


a: 10

print catch [

       if a < 10 [throw "too small"]

       if a = 10 [throw "just right"]

       if a > 10 [throw "too big"]

]


just right

catch/name

catches a named throw. Really deserves an example, hope to make one soon...

throw/name

throws a named catch.



Boolean branching

native! all  Red-by-example    MyCode4fun

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

>> if all [john = "boy" alice = "girl"] [print "It' all true"]
It' all true



native! any  Red-by-example    MyCode4fun

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


>> if any [john = "girl" alice = "girl"] [print "Something is true here"]

Something is true here



< Previous topic                                                                                          Next topic >