The switch statement

### 9.3.2 The switch statement

The switch statement can be used when you want the value of a block to depend on an integer. It takes one argument, an expression which evaluates to an integer. It should be followed by a sequence of case statements, which takes the form case followed by an integer and then a colon, which is followed by a code block to be executed if the expression equals the integer. At the end is an optional default: statement, which is followed by a code block to be executed if the expression doesn’t equal any of the given integers. For example, if you wanted to define a function of three variables which performed an operation on the first two variables depending on the third, you could enter

      oper(a,b,c) := {
      switch (c) {
        case 1: {a := a + b; break;}
        case 2: {a := a - b; break;}
        case 3: {a := a * b; break;}
        default: {a := a ^ b;}
        }
      return a;
      }

Then

oper(2,3,1)

will return 2+3=5, since the third argument is 1, and

oper(2,3,2)

will return 2−3=−1, since the third argument is 2.