case, of, otherwise, end_case, _case

Switch statement

MuPAD® notebooks will be removed in a future release. Use MATLAB® live scripts instead.

MATLAB live scripts support most MuPAD functionality, though there are some differences. For more information, see Convert MuPAD Notebooks to MATLAB Live Scripts.

For switch-case in MATLAB®, see switch.

Syntax

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

case x
  of match1 do
    statements1
  of match2 do
    statements2
  ...
end_case

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

Description

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

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.

Examples

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:

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:

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, "is 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)

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

Parameters

x, match1, match2, …

Arbitrary MuPAD objects

statements1, otherstatements, …

Arbitrary sequences of statements

Return Values

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.

Algorithms

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

See Also

MuPAD Functions