Previous Page Next Page Contents

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

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

Changes




Do you have questions or comments?


Copyright © SciFace Software GmbH & Co. KG 2000