The switch Statement


The switch statement has the following form.

switch expression
{
 constant_a
  statement

 constant_b
  statement
 ...
 default
  statement
}

The switch expression must be of integer type. No two cases can have the same value. The default case is executed when no case constants match the switch expression. The default case is optional and if not present, no action is taken when all matches fail. When a match is made, the coresponding statement is executed.

A switch statement is stored as a dictionary of instructions keyed off the integers. Thus it is feasible to have hundreds of cases without affecting performance.

The following example prints out hello.

generic sampleH
{
  sampleH()
  {
     switch false // this is a binary switch.
     {
         +a
         {
             s = "hello"
             s.println()
         }
         +b
         {
             s = "world"
             s.println()
         }
     }
   }
}