||(switch [exp1 exp2] ... [expn expn1] [defaultexp]) -> value of evaluated expression
||[exp1 exp2] ... [expn expn1]: A group of two expressions following each other. It the first expression evaluates to non-Nil, then the second expression is evaluated. Else the switch moves on to test the next group. There can be as many of these groups as neccessary.
[defaultexp]: an expression which will be evaluated if no group was successfully evaluated. This expression can be considered the "default" of the switch.
||Whatever the expression that gets evaluated returns.
||Allows branching of the code by more than 1 condition. For just 1 condition, 'if' is good enough. It just evaluates all the conditions (every even argument; odd arguments are corresponding functions) in left to right order, until the first one that evaluates to non-Nil. It then invokes the corresponding function, which is the argument that directly follows that condition. If all the conditions evaluate as Nil, then the last, optional function is invoked.
(setq num 3)
(eq num 1)
(plyMessage gPlayer "num = 1")
(eq num 2)
(plyMessage gPlayer "num = 2")
(eq num 3)
(plyMessage gPlayer "num = 3")
(eq num 4)
(plyMessage gPlayer "num = 4")
(plyMessage gPlayer "num not in (1,2,3,4)")
||Useful for various multiple choices in the code. In could be replaced by series of 'if's but then there would be even more parentheses than now. Also, it's interesting to notice that there probably isn't any real difference between 'expressions', 'functions' and 'optional function': these are legal switch function examples:
(switch 1 11 2 12 3 13 4 14 5 15) -> 11
(switch Nil 11 Nil 12 3 13 4 14 5) -> 13
(switch Nil 11 Nil 12 Nil 13 Nil 14 5) -> 5
So it just takes a list of arguments, evaluates the first one, if it's non-Nil, evaluates the second and returns whatever it is. If the first argument is Nil, skips the second and evaluates the third; if that one is non-Nil, evaluates the fourth and returns, etc. The 'optional function' might also be considered just another condition, which doesn't have a following argument, thus (switch Nil 11 Nil 12 3) evaluates as 3. It really is simple when you look at it this way.