[Next] [Up] [Previous] [Contents]
Next: Representing state machines using Up: Expressions Previous: Boolean operators

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 is 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 vi that contains the value x, and returns the corresponding ei. The vi can also be single values - these are treated as the set containing only the given value.



Ken McMillan
Sat Jun 6 21:41:59 PDT 1998