## `case` -- switch statement

#### Introduction

`case-end_case` statement allows to switch between various branches in a program.

#### Call(s)

case x
of match1 do statements1
of match2 do statements2
...
<otherwise otherstatements>
end_case

`_case(x, match1, statements1, match2, statements2, ...
<, otherstatements>)`

#### Parameters

`x, match1, match2, ...` | - | arbitrary MuPAD objects
`statements1, ..., otherstatements` | - | arbitrary sequences of statements

#### Returns

the result of the last command executed inside the `case` statement. The void object of type `DOM_NULL` is returned if no matching branch was found and no `otherwise` branch exists. `NIL` is returned if a matching branch was encountered, but no command was executed inside this branch.

#### Related Functions

`break`, `if`, `return`

#### Details

- The `case` statement is a control structure that extends the functionality of the `if` statement. In a `case` statement, an object is compared with a number of given values and one or more statement sequences are executed.
- If the value of `x` equals one of the values `match1`, `match2` etc., the first matching branch and all its following branches (including `otherwise`) are executed, until the execution is terminated by a `break` or a `return` statement, or the `end_case`.
- If the value of `x` does not equal any of the values `match1, match2, ...`, only the `otherwise` branch is executed. If no `otherwise` branch exists, the `case` statement terminates and returns the void object of type `DOM_NULL`.
- The keyword `end_case` may be replaced by the keyword `end`.
- `_case` is a function of the system kernel.

#### Example 1

All statements after the first match are executed:

    >> x := 2:
       case x
         of 1 do print(1)
         of 2 do print(4)
         of 3 do print(9)
         otherwise print("otherwise")
       end_case:

                                         4

                                         9

                                    "otherwise"

`break` may be used to ensure that only one matching branch is executed:

    >> case x
         of 1 do print(1); 1; break
         of 2 do print(4); 4; break
         of 3 do print(9); 9; break
         otherwise print("otherwise")
       end_case:

                                         4

    >> delete x:

#### Example 2

The functionality of the `case` statement allows to share code that is to be used in several branches. The following function uses the statement `print(x, ïs a real number")` for the three branches that correspond to real MuPAD numbers:

    >> isReal := proc(x)
       begin
          case domtype(x)
            of DOM_INT do
            of DOM_RAT do
            of DOM_FLOAT do print(x, "is a real number"); break
            of DOM_COMPLEX do print(x, "is not a real number"); break
            otherwise print(x, "cannot decide");
          end_case
       end_proc:
       isReal(3), isReal(3/7), isReal(1.23), isReal(3 + I), isReal(z)

                               3, "is a real number"

                              3/7, "is a real number"

                             1.23, "is a real number"

                           3 + I, "is not a real number"

                                z, "cannot decide"

    >> delete isReal:

#### Example 3

The correspondence between the functional and the imperative form of the `case` statement is demonstrated:

    >> hold(_case(x, match1, (1; break), match2, (4; break),
                  print("otherwise")))

                              case x
                                of match1 do
                                  1;
                                  break
                                of match2 do
                                  4;
                                  break
                                 otherwise
                                  print("otherwise")
                              end_case

    >> hold(_case(x, match1, (1; break), match2, (4; break)))

                                  case x
                                    of match1 do
                                      1;
                                      break
                                    of match2 do
                                      4;
                                      break
                                  end_case

#### Background

- The functionality of the `case` statement corresponds to the `switch` statement of the C programming language.

#### Changes

- `end` can now be used as an alternative to `end_case`.

Do you have questions or comments?
Copyright © SciFace Software GmbH & Co. KG 2000