MAXScript Language Reference > Controlling Program Flow >

# Case Expression

The case expression is used to select an expression to be evaluated from a set of labeled expressions based on a test value compared against the labels.

Only the first expression whose label matches the test expression is evaluated.

All labels must be comparable to the test expression.

Labeled expressions can be block-expressions, so you can use the case expression as a case statement, choosing between blocks of code to execute.

The syntax for <case_expr> is:

    case [ <expr> ]  of ( <cases> )

where <expr> is the test expression and <cases> is a sequence of labeled expressions:

    <factor>: <expr>
    default: <expr>

FOR EXAMPLE,
    new_obj = case copy_type.state of
    (
    2: copy $foo
    3: instance $foo
    default: reference $foo
    )

A special label, default , can be optionally used to tag the expression that will be evaluated if no other labels match the test expression.

If the default label is not used, and no label matches the test expression, the case expression returns a value of undefined .

The labels are <factors> - elements such as number literals, name literals, string literals, variables, or block-expressions.

When a case expression is evaluated, the test expression is evaluated once, then each label expression is evaluated in order until one is found that compares equally to the test expression value.

The expression it labels is then evaluated and this becomes the result of the case expression.

No further labels are evaluated or tested.

The test expression is optional.

If it is not supplied, the labels are expected to be Boolean values or expressions, and the first label evaluated as true determines the chosen case.

In this variant, it is common to use expressions as labels, so be sure to parenthesize them.

FOR EXAMPLE,
    case of
    (
    (a > b): print "a is big"
    (b < c): print "b is little"
    (c <= d*3): ...
    default: ...
    )