# Conditionals

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

lhs:=rhs/;test | use 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[test_{1},value_{1},test_{2},...] | evaluate the in turn, giving the value associated with the first one that is True |

Switch[expr,form_{1},value_{1},form_{2},...] | compare expr with each of the , giving the value associated with the first form it matches |

Switch[expr,form_{1},value_{1},form_{2},...,_,def] | use def as a default value |

Piecewise[{{value_{1},test_{1}},...},def] | give the value corresponding to the first which yields True |

In[1]:= |

Out[1]= |

When you write programs in *Mathematica*, you will often have a choice between making a single definition whose right-hand 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.

In[3]:= |

In[4]:= |

In[5]:= |

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.

In[7]:= |

In[8]:= |

Out[8]= |

In[9]:= |

Out[9]= |

In[10]:= |

In[11]:= |

Out[11]= |

In[12]:= |

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 does not yield True or False unless and have specific values, such as numerical ones.

In[13]:= |

Out[13]= |

In[14]:= |

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.

In[15]:= |

Out[15]= |

In[16]:= |

Out[16]= |

In[17]:= |

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 *Mathematica* pattern matcher effectively uses to determine when one literal expression matches another.

In[18]:= |

Out[18]= |

In[19]:= |

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.

expr_{1}&&expr_{2}&&expr_{3} | evaluate until one of the is found to be False |

expr_{1}||expr_{2}||expr_{3} | evaluate until one of the is found to be True |

Evaluation of logical expressions.

In[20]:= |

In[21]:= |

Out[21]= |

In[22]:= |

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.