next up previous contents
Next: Constructor loops Up: Conditionals Previous: Defaults   Contents

Complex conditionals - switch and case

The complex conditionals are ``case'' and ``switch''. A ``case'' statement has the form:

        case{
          <cond1> : <stmt1>
          <cond2> : <stmt2>
          ...
          <condn> : <stmtn>
          [default : <dftlstmt>]
        }

This statement is exactly equivalent to

        if (<cond1>) <stmt1>
        else if (<cond2>) <stmt2>
        ...
        else if (<condn>) <stmtn>
        [else <dfltstmt>]

Note this means that if all the conditions are false, and there is no default statement, then no assignments are made.

A ``switch'' statement has the form:

        switch(<expr>){
          <case1> : <stmt1>
          <case2> : <stmt2>
          ...
          <casen> : <stmtn>
          [default : <dftlstmt>]
        }
This is exactly equivalent to:

        case{
          <expr> in <case1> : <stmt1>
          <expr> in <case2> : <stmt2>
          ...
          <expr> in <casen> : <stmtn>
          [default : <dftlstmt>]
        }

Note that the set inclusion operator ``in'' is used instead of ``=''. This means that each case may be a set of values rather than a single value. For example, if we want a counter that waits for a signal ``start'', counts to seven, asserts a signal ``done'', and resets, we might write:

        default
          done := 0;
        in
          switch(count){
            0 : if start then next(count) := 1;
            1..6 : next(count) := count + 1;
            7 : {
              next(count) := 0;
              done := 1;
            }
          }



2002-10-28