# 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)

 In[1]:=
 In[2]:=
 Out[2]=