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, aa, 3 > 4, bb, True, cc ] === cc
Switch[test, pattern1,value1, pattern2,value2 etc]
-
eval test, then compares it with each of the pattern in turn, eval and return the value corresponding to the first match found.
The comparison is done with
MatchQ[test, pattern]
[see WolframLang: Pattern Matching]To have a catch-all test, add a last test to be the pattern
_
.Switch[ 4, 3, aa, 4, bb, _, cc ] === bb
Switch[ {x, y}, 3, aa, {_,_} , bb, _, cc ] === bb