Anvil | Smiths Smiths | Register Register | Login Login |
Search:
Show links Show tools Show tree | Previous document Next document | njet.org > Anvil > Documentation > Language reference > Statements > Compound statements > Switch

Switch

The switch statement transfers control to one fo several statements depending on the the value of a candidate expression. There are three flavors of switch:

  • All expressions in ``case´´ statements evaluate to integer constants at compile time. This is fastest of all switches.
  • All expressions in ``case´´ statements evaluate to constants at compile time. One hashtable lookup is required when evaluating statement.
  • One or more of expressions in ``case´´ statements does not evaluate to constant at compile time. This type is slowest and it is implemented with consequtive combination of if's and equality checks. Also it causes re-evaluation of non-constant case expressions, thought the switch expression is only evaluated once.

No two of the case constant expressions may have the same value, although non-constant cases may yield to same values.

If these is a matching case for value yielded by the expression then the statement assosicated with that case are evaluted. Evaluation of statements in switch "falls through cases" provided that no flow control statements are encountered.

If none of the cases could be matched and if there is a ``default´´ then the default statements are evaluted, otherwise no further actions are taken.

Default statement may only appear once, in any position inside the body of switch statement.

A break statement will terminate execution of switch. Continue statement cannot be used inside switch, although special continue .. case and continue .. default are used to transfer the execution to specified case statement.

  switch :=  
    "switch" "(" expr-startcandidate ")"  
    "{" 
      ( "case" exprvalue ( "," exprvalue )* ":"  statement * 
      | "default" ":" statement * 
      ) * 
    "}" 
Contributes notes:
Add a note
What's new | Anvil