Mathematica 9 is now available
THIS IS DOCUMENTATION FOR AN OBSOLETE PRODUCT.
SEE THE DOCUMENTATION CENTER FOR THE LATEST INFORMATION.
Mathematica > Core Language > Procedural Programming > Conditionals >

Which

Which[test1, value1, test2, value2, ...]
evaluates each of the testi in turn, returning the value of the valuei corresponding to the first one that yields True.
  • If any of the testi evaluated by Which give neither True nor False, then a Which object containing these remaining elements is returned unevaluated.  »
  • You can make Which return a "default value" by taking the last testi to be True.
Which can be maintained in symbolic form:
Conditions are evaluated until one is found that is neither True nor False:
Use True for an else clause that always matches:
Define a piecewise function:
Expand it to use Piecewise:
Do symbolic operations:
Use Which rather than a nested if-then-elsif chain:
Use PiecewiseExpand to convert Which to Piecewise:
New in 1
Ask a question about this page  |  Suggest an improvement  |  Leave a message for the team