WolframLang: Which, Switch
Which[test1,value1, test2,value2 etc]
-
eval each of the test in turn, return the value corresponding to the first one that yields
True
. WhichTo 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]
SwitchTo 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"