If

If[condition,t,f]

gives t if condition evaluates to True, and f if it evaluates to False.

If[condition,t,f,u]

gives u if condition evaluates to neither True nor False.

Details

  • If evaluates only the argument determined by the value of the condition.
  • If[condition,t,f] is left unevaluated if condition evaluates to neither True nor False.
  • If[condition,t] gives Null if condition evaluates to False.

Background & Context

  • If is a procedural programming structure whose evaluation branch is determined by the truth value of a specified condition. If typically takes a condition plus two additional arguments: If[cond,t,f]. Here, t and f are evaluated if the condition is True or False, respectively. If may also take three additional arguments: If[cond,t,f,u]. Here, u gives the evaluation to take place if the specified condition is neither explicitly True nor explicitly False. Finally, If may take just one additional argument: If[cond,t]. In this case, the value for f is taken to be Null.
  • For efficiency, If evaluates only the relevant arguments. For example, if the condition in If[cond,t,f,u] is True, only t (and neither f nor u) will be evaluated.
  • ConditionalExpression is a related symbolic construct that represents an expression only when the given condition is True. Other more flexible programming structures that generalize If include Which and Switch. Mathematical functions that evaluate depending on the values of their arguments include Boole and Piecewise. Condition is a pattern that matches only if the evaluation of a test results in True. TrueQ is a specific case of If that yields True if an expression is explicitly True, and False otherwise.

Examples

open allclose all

Basic Examples  (1)

Scope  (4)

Evaluation  (3)

If the condition is neither True nor False, If remains unevaluated:

The form with an explicit case for an undetermined condition evaluates in any case:

Use TrueQ to force the condition to always return a Boolean value:

Symbolic Transformations  (1)

Define a piecewise function with both If and Round as piecewise elements:

Use PiecewiseExpand to get it into piecewise normal form:

Perform several different symbolic operations:

Generalizations & Extensions  (1)

If can be used as a statement:

It can also be used as an expression returning a value:

Properties & Relations  (4)

Only the branch actually taken is evaluated:

Define a function by cases:

Alternatively, use several conditional definitions:

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

Use PiecewiseExpand to convert If to Piecewise:

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

Text

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

CMS

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

APA

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

BibTeX

@misc{reference.wolfram_2024_if, author="Wolfram Research", title="{If}", year="1988", howpublished="\url{https://reference.wolfram.com/language/ref/If.html}", note=[Accessed: 24-November-2024 ]}

BibLaTeX

@online{reference.wolfram_2024_if, organization={Wolfram Research}, title={If}, year={1988}, url={https://reference.wolfram.com/language/ref/If.html}, note=[Accessed: 24-November-2024 ]}