This is documentation for Mathematica 3, which was
based on an earlier version of the Wolfram Language.
View current documentation (Version 11.1)
 Documentation / Mathematica / Built-in Functions / Programming / Flow Control  /
Switch

  • Switch[ expr , , , , , ... ] evaluates expr, then compares it with each of the in turn, evaluating and returning the corresponding to the first match found.
  • Only the corresponding to the first that matches expr is evaluated. Each is evaluated only when the match is tried.
  • If the last is the pattern _, then the corresponding is always returned if this case is reached.
  • If none of the match expr, the Switch is returned unevaluated.
  • Switch has attribute HoldRest.
  • You can use Break, Return and Throw in Switch.
  • See the Mathematica book: Section 2.5.8.
  • See also: If, Condition, Which.

    Further Examples

    The output of this function depends on the type of the input.

    In[1]:=

    In[2]:=

    Out[2]=

    In[3]:=