Conditionals

The Wolfram Language provides various ways to set up conditionals, which specify that particular expressions should be evaluated only if certain conditions hold.

lhs:=rhs/;testuse the definition only if test evaluates to True
If[test,then,else]evaluate then if test is True, and else if it is False
Which[test1,value1,test2,]evaluate the in turn, giving the value associated with the first one that is True
Switch[expr,form1,value1,form2,]compare expr with each of the , giving the value associated with the first form it matches
Switch[expr,form1,value1,form2,,_,def]use def as a default value
Piecewise[{{value1,test1},},def]give the value corresponding to the first which yields True

Conditional constructs.

The test gives False, so the "else" expression is returned.
In[1]:=
Click for copyable input
Out[1]=
Only the "else" expression is evaluated in this case.

When you write programs in the Wolfram Language, you will often have a choice between making a single definition whose righthand side involves several branches controlled by If functions, or making several definitions, each controlled by an appropriate condition. By using several definitions, you can often produce programs that are both clearer, and easier to modify.

This defines a step function, with value for , and otherwise.
In[3]:=
Click for copyable input
This defines the positive part of the step function using a condition.
In[4]:=
Click for copyable input
Here is the negative part of the step function.
In[5]:=
Click for copyable input
This shows the complete definition using conditions.

The function If provides a way to choose between two alternatives. Often, however, there will be more than two alternatives. One way to handle this is to use a nested set of If functions. Usually, however, it is instead better to use functions like Which and Switch.

This defines a function with three regions. Using True as the third test makes this the default case.
In[7]:=
Click for copyable input
This uses the first case in the Which.
In[8]:=
Click for copyable input
Out[8]=
This uses the third case.
In[9]:=
Click for copyable input
Out[9]=
This defines a function that depends on the values of its argument modulo .
In[10]:=
Click for copyable input
Mod[7,3] is , so this uses the second case in the Switch.
In[11]:=
Click for copyable input
Out[11]=
matches neither nor , but does match .
In[12]:=
Click for copyable input
Out[12]=

An important point about symbolic systems such as the Wolfram Language is that the conditions you give may yield neither True nor False. Thus, for example, the condition does not yield True or False unless and have specific values, such as numerical ones.

In this case, the test gives neither True nor False, so both branches in the If remain unevaluated.
In[13]:=
Click for copyable input
Out[13]=
You can add a special fourth argument to If, which is used if the test does not yield True or False.
In[14]:=
Click for copyable input
Out[14]=
If[test,then,else,unknown]a form of If which includes the expression to use if test is neither True nor False
TrueQ[expr]give True if expr is True, and False otherwise
lhs===rhsorSameQ[lhs,rhs]give True if lhs and rhs are identical, and False otherwise
lhs=!=rhsorUnsameQ[lhs,rhs]give True if lhs and rhs are not identical, and False otherwise
MatchQ[expr,form]give True if the pattern form matches expr, and give False otherwise

Functions for dealing with symbolic conditions.

The Wolfram Language leaves this as a symbolic equation.
In[15]:=
Click for copyable input
Out[15]=
Unless expr is manifestly True, TrueQ[expr] effectively assumes that expr is False.
In[16]:=
Click for copyable input
Out[16]=
Unlike , tests whether two expressions are manifestly identical. In this case, they are not.
In[17]:=
Click for copyable input
Out[17]=

The main difference between and is that always returns True or False, whereas can leave its input in symbolic form, representing a symbolic equation, as discussed in "Equations". You should typically use when you want to test the structure of an expression, and if you want to test mathematical equality. The Wolfram Language pattern matcher effectively uses to determine when one literal expression matches another.

You can use to test the structure of expressions.
In[18]:=
Click for copyable input
Out[18]=
The operator gives a less useful result.
In[19]:=
Click for copyable input
Out[19]=

In setting up conditionals, you will often need to use combinations of tests, such as . An important point is that the result from this combination of tests will be False if any of the yield False. The Wolfram Language always evaluates the in turn, stopping if any of the yield False.

expr1&&expr2&&expr3evaluate until one of the is found to be False
expr1||expr2||expr3evaluate until one of the is found to be True

Evaluation of logical expressions.

This function involves a combination of two tests.
In[20]:=
Click for copyable input
Here both tests are evaluated.
In[21]:=
Click for copyable input
Out[21]=
Here the first test yields False, so the second test is not tried. The second test would involve , and would generate an error.
In[22]:=
Click for copyable input
Out[22]=

The way that the Wolfram Language evaluates logical expressions allows you to combine sequences of tests where later tests may make sense only if the earlier ones are satisfied. The behavior, which is analogous to that found in languages such as C, is convenient in constructing many kinds of Wolfram Language programs.