Selection

Selection statments allow you to choose between one or more paths of script execution. In Max, the control statements return a result which can be used for further evaluation (like the ?: operator of C/C++)

if-do


if <evaluated-expression> do <expressions>

The if-do form is more useful in the listener where expressions are evaluated as they are typed. Everything after the do is performed if the evaluated-expression is true

a=0
x=10
if x > 3 do ( a= a+1)

As there is no possibilty of an else with the if-do form of selection, the code typed into the listener which is processed as soon as the statement is complete will immediately evaluate the statement.

if-then

if <evaluated-expression> then <do if true> [else <do if not true>]

if the evaluated expression is true, evaluate statements after then. else is optional, but if included, do the code after else when evaluated expression is false.

In the case where you are typing code into the listener, an if-then statement that does not include the else will not evaluate until a statement follows it.

For example if you type in:

if x > 3 then a=a+1

nothing will happen until you add another statement or an else clause.

case

Case is similar to a switch statment:

case [<evaluated-expression>] of (
<factor>:<expression>
[<factor>:<expression>]
[...]
[default:<expression>]
)

can be an a literal of some sort or it can be an expression also

NOTE: unlike C/C++ switch statments, once a match is found, the expression of the matching factor is evaluated and no other. There no break that is needed to get past the other cases like there is in C/C++

Also notice that the <evaluated-expression> is optional. It is possible to write something like:

case of(
<factor>:<expression>
[<factor>:<expression>]
[...]
[default:<expression>]
)

If this is done, the first factor expression that is true will be evaluated and the rest will be ignored

a=1
b=2
c=3
case of(
(a == 0):"here"
(b == 2): "there"
(c == 3):  "everywhere"
default: "nowhere"
)