next up previous contents
Next: Representing state machines using Up: Expressions Previous: Boolean operators   Contents

Conditional operators (``if'', ``case'' and ``switch'')

The simple conditional operator has the form

        x ? y : z
It yields y if x is 1 (true) and z if x is 0 (false). If x is not a boolean value, it yields undefined.

The complex conditionals are ``case'' and ``switch''. The expression

        case{
          c1 : e1;
          c2 : e2;
          ...
          cn : en;
          [default : ed;]
        }
is equivalent to

        c1 ? e1 : c2 ? e2 : ... cn ? en : ed
if there is a default case, and otherwise

        c1 ? e1 : c2 ? e2 : ... cn ? en : undefined
That is, if all the conditions c1...cn are false, and there is no default case, then the case expression is undefined.

The expression

        switch(x){
          v1 : e1;
          v2 : e2;
          ...
          vn : en;
          [default : ed;]
        }
is equivalent to

        (x in v1) ? e1 : (x in v2) ? e2 : ... (x in vn) ? en : ed
if there is a default case, and otherwise

        (x in v1) ? e1 : (x in v2) ? e2 : ... (x in vn) ? en : undefined

That is, the switch expression finds the first set v$i$ that contains the value x, and returns the corresponding e$i$. The v$i$ can also be single values - these are treated as the set containing only the given value.



2002-10-28