2.6.8 Conditionals
Mathematica provides various ways to set up conditionals, which specify that particular expressions should be evaluated only if certain conditions hold.
Conditional constructs.
The test gives False, so the "else" expression y is returned.
In[1]:= If[7 > 8, x, y]
Out[1]=
Only the "else" expression is evaluated in this case.
In[2]:= If[7 > 8, Print[x], Print[y]]
When you write programs in Mathematica, 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 1 for x > 0, and 1 otherwise.
In[3]:= f[x_] := If[x > 0, 1, 1]
This defines the positive part of the step function using a /; condition.
In[4]:= g[x_] := 1 /; x > 0
Here is the negative part of the step function.
In[5]:= g[x_] := 1 /; x <= 0
This shows the complete definition using /; conditions.
In[6]:= ?g
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]:= h[x_] := Which[x < 0, x^2, x > 5, x^3, True, 0]
This uses the first case in the Which.
In[8]:= h[5]
Out[8]=
This uses the third case.
In[9]:= h[2]
Out[9]=
This defines a function that depends on the values of its argument modulo 3.
In[10]:= r[x_] := Switch[Mod[x, 3], 0, a, 1, b, 2, c]
Mod[7, 3] is 1, so this uses the second case in the Switch.
In[11]:= r[7]
Out[11]=
17 matches neither 0 nor 1, but does match _.
In[12]:= Switch[17, 0, a, 1, b, _, q]
Out[12]=
An important point about symbolic systems such as Mathematica is that the conditions you give may yield neither True nor False. Thus, for example, the condition x == y does not yield True or False unless x and y 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]:= If[x == y, a, b]
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]:= If[x == y, a, b, c]
Out[14]=
Functions for dealing with symbolic conditions.
Mathematica leaves this as a symbolic equation.
In[15]:= x == y
Out[15]=
Unless expr is manifestly True, TrueQ[expr] effectively assumes that expr is False.
In[16]:= TrueQ[x == y]
Out[16]=
Unlike ==, === tests whether two expressions are manifestly identical. In this case, they are not.
In[17]:= x === y
Out[17]=
The main difference between lhs === rhs and lhs == rhs is that === always returns True or False, whereas == can leave its input in symbolic form, representing a symbolic equation, as discussed in Section 1.5.5. You should typically use === when you want to test the structure of an expression, and == if you want to test mathematical equality. The Mathematica pattern matcher effectively uses === to determine when one literal expression matches another.
You can use === to test the structure of expressions.
In[18]:= Head[a + b + c] === Times
Out[18]=
The == operator gives a less useful result.
In[19]:= Head[a + b + c] == Times
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. Mathematica always evaluates the in turn, stopping if any of the yield False.
Evaluation of logical expressions.
This function involves a combination of two tests.
In[20]:= t[x_] := (x != 0 && 1/x < 3)
Here both tests are evaluated.
In[21]:= t[2]
Out[21]=
Here the first test yields False, so the second test is not tried. The second test would involve 1/0, and would generate an error.
In[22]:= t[0]
Out[22]=
The way that Mathematica 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 Mathematica programs.
