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 >

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.
  • 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.
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:
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:
If can be used as a statement:
It can also be used as an expression returning a value:
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-elsif chain:
Use PiecewiseExpand to convert If to Piecewise:
New in 1
Ask a question about this page  |  Suggest an improvement  |  Leave a message for the team