WOLFRAM

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.

Details

  • Which has attribute HoldAll.
  • 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.
  • If all the testi evaluate to False, Which returns Null.

Examples

open allclose all

Basic Examples  (1)Summary of the most common use cases

Out[1]=1

Scope  (4)Survey of the scope of standard use cases

Programming Behavior  (3)

Which can be maintained in symbolic form:

Out[1]=1
Out[2]=2

Conditions are evaluated until one is found that is neither True nor False:

Out[1]=1

Use True for an else clause that always matches:

Out[2]=2

Symbolic Transformations  (1)

Define a piecewise function:

Out[1]=1

Expand it to use Piecewise:

Out[2]=2

Do symbolic operations:

Out[3]=3
Out[4]=4
Out[5]=5
Out[6]=6

Properties & Relations  (2)Properties of the function, and connections to other functions

Use Which rather than a nested if-then-elseif chain:

Out[2]=2
Out[4]=4

Use PiecewiseExpand to convert Which to Piecewise:

Out[1]=1
Out[2]=2
Out[3]=3
Wolfram Research (1988), Which, Wolfram Language function, https://reference.wolfram.com/language/ref/Which.html.
Wolfram Research (1988), Which, Wolfram Language function, https://reference.wolfram.com/language/ref/Which.html.

Text

Wolfram Research (1988), Which, Wolfram Language function, https://reference.wolfram.com/language/ref/Which.html.

Wolfram Research (1988), Which, Wolfram Language function, https://reference.wolfram.com/language/ref/Which.html.

CMS

Wolfram Language. 1988. "Which." Wolfram Language & System Documentation Center. Wolfram Research. https://reference.wolfram.com/language/ref/Which.html.

Wolfram Language. 1988. "Which." Wolfram Language & System Documentation Center. Wolfram Research. https://reference.wolfram.com/language/ref/Which.html.

APA

Wolfram Language. (1988). Which. Wolfram Language & System Documentation Center. Retrieved from https://reference.wolfram.com/language/ref/Which.html

Wolfram Language. (1988). Which. Wolfram Language & System Documentation Center. Retrieved from https://reference.wolfram.com/language/ref/Which.html

BibTeX

@misc{reference.wolfram_2025_which, author="Wolfram Research", title="{Which}", year="1988", howpublished="\url{https://reference.wolfram.com/language/ref/Which.html}", note=[Accessed: 05-June-2025 ]}

@misc{reference.wolfram_2025_which, author="Wolfram Research", title="{Which}", year="1988", howpublished="\url{https://reference.wolfram.com/language/ref/Which.html}", note=[Accessed: 05-June-2025 ]}

BibLaTeX

@online{reference.wolfram_2025_which, organization={Wolfram Research}, title={Which}, year={1988}, url={https://reference.wolfram.com/language/ref/Which.html}, note=[Accessed: 05-June-2025 ]}

@online{reference.wolfram_2025_which, organization={Wolfram Research}, title={Which}, year={1988}, url={https://reference.wolfram.com/language/ref/Which.html}, note=[Accessed: 05-June-2025 ]}