WolframLang: Which, Switch

By Xah Lee. Date: . Last updated: .
Which[test1,value1, test2,value2 etc]

eval each of the test in turn, return the value corresponding to the first one that yields True. Which

To have a catch-all test, add a last test to be just True.

Which[
3 > 9, "one",
3 > 4, "two",
True, "none of the above"
] === "none of the above"
Switch[expr, form1,value1, form2,value2,]

eval expr, then compares it with each of the form in turn, eval and return the value corresponding to the first match found. The comparison is done with Pattern Matching MatchQ[expr,form] Switch

To have a catch-all test, add a last test to be the pattern _.

Switch[ 4,
3, "three",
4, "four",
_, "none"
] === "four"
Switch[ {x, y},
3, "three",
{_,_} , "2thingsList",
_, "none"
] === "2thingsList"

WolframLang Boolean

WolframLang in Depth

Basics

Comment, Print

String

Arithmetic

List

Expression, Atom, Head

Boolean

Conditional

Variable

Loop

Data Structure

Function

Pattern Matching

Advanced

Shell Tasks

Misc