Switch
✖
Switch
✖
Switch[expr,form1,value1,form2,value2,…]
evaluates expr, then compares it with each of the formi in turn, evaluating and returning the valuei corresponding to the first match found.
Details

- Only the valuei corresponding to the first formi that matches expr is evaluated. Each formi is evaluated only when the match is tried.
- If the last formi is the pattern _, then the corresponding valuei is always returned if this case is reached.
- If none of the formi match expr, the Switch is returned unevaluated.
- Switch has attribute HoldRest.
- You can use Break, Return, and Throw in Switch.
Examples
Basic Examples (2)Summary of the most common use cases
Process Boolean value to 1 or 0, giving a message and defaulting to 0 when not Boolean:
In[1]:=1

✖
https://wolfram.com/xid/0mq47q-z2yvn
In[2]:=2

✖
https://wolfram.com/xid/0mq47q-lo4xr2
In[3]:=3

✖
https://wolfram.com/xid/0mq47q-b4268r

Out[3]=3

Give a suggested symbolic transformation for an expression:
In[1]:=1

✖
https://wolfram.com/xid/0mq47q-l5vqgl
In[2]:=2

✖
https://wolfram.com/xid/0mq47q-fw7egc
Out[2]=2

In[3]:=3

✖
https://wolfram.com/xid/0mq47q-yxnd2
Out[3]=3

Wolfram Research (1988), Switch, Wolfram Language function, https://reference.wolfram.com/language/ref/Switch.html.
✖
Wolfram Research (1988), Switch, Wolfram Language function, https://reference.wolfram.com/language/ref/Switch.html.
Text
Wolfram Research (1988), Switch, Wolfram Language function, https://reference.wolfram.com/language/ref/Switch.html.
✖
Wolfram Research (1988), Switch, Wolfram Language function, https://reference.wolfram.com/language/ref/Switch.html.
CMS
Wolfram Language. 1988. "Switch." Wolfram Language & System Documentation Center. Wolfram Research. https://reference.wolfram.com/language/ref/Switch.html.
✖
Wolfram Language. 1988. "Switch." Wolfram Language & System Documentation Center. Wolfram Research. https://reference.wolfram.com/language/ref/Switch.html.
APA
Wolfram Language. (1988). Switch. Wolfram Language & System Documentation Center. Retrieved from https://reference.wolfram.com/language/ref/Switch.html
✖
Wolfram Language. (1988). Switch. Wolfram Language & System Documentation Center. Retrieved from https://reference.wolfram.com/language/ref/Switch.html
BibTeX
✖
@misc{reference.wolfram_2025_switch, author="Wolfram Research", title="{Switch}", year="1988", howpublished="\url{https://reference.wolfram.com/language/ref/Switch.html}", note=[Accessed: 26-March-2025
]}
BibLaTeX
✖
@online{reference.wolfram_2025_switch, organization={Wolfram Research}, title={Switch}, year={1988}, url={https://reference.wolfram.com/language/ref/Switch.html}, note=[Accessed: 26-March-2025
]}