Conditionals

The Wolfram Language 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 (RefLink[False, paclet:ref/False]), so the "else" expression y is returned:

If[7>8,x,y]

Only the "else" expression is evaluated in this case:

If[7>8,Print[x],Print[y]]

When you write programs in the Wolfram Language, you will often have a choice between making a single definition whose right-hand side involves several branches controlled by (RefLink[If, paclet:ref/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:

f[x_]:=If[x>0,1,-1]

This defines the positive part of the step function using a /; condition:

g[x_]:=1/;x>0

Here is the negative part of the step function:

g[x_]:=-1/;x<=0

This shows the complete definition using /; conditions:

?g

The function (RefLink[If, paclet:ref/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 (RefLink[If, paclet:ref/If]) functions. Usually, however, it is instead better to use functions like (RefLink[Which, paclet:ref/Which]) and (RefLink[Switch, paclet:ref/Switch]).

This defines a function with three regions. Using (RefLink[True, paclet:ref/True]) as the third test makes this the default case:

h[x_]:=Which[x<0,x^2,x>5,x^3,True,0]

This uses the first case in the (RefLink[Which, paclet:ref/Which]):

h[-5]

This uses the third case:

h[2]

This defines a function that depends on the values of its argument modulo 3:

r[x_]:=Switch[Mod[x,3],0,a,1,b,2,c]

(RefLink[Mod, paclet:ref/Mod])[7,3] is 1, so this uses the second case in the (RefLink[Switch, paclet:ref/Switch]):

r[7]

17 matches neither 0 nor 1, but does match _:

Switch[17,0,a,1,b,_,q]

An important point about symbolic systems such as the Wolfram Language is that the conditions you give may yield neither (RefLink[True, paclet:ref/True]) nor (RefLink[False, paclet:ref/False]). Thus, for example, the condition x==y does not yield (RefLink[True, paclet:ref/True]) or (RefLink[False, paclet:ref/False]) unless x and y have specific values, such as numerical ones.

In this case, the test gives neither (RefLink[True, paclet:ref/True]) nor (RefLink[False, paclet:ref/False]), so both branches in the (RefLink[If, paclet:ref/If]) remain unevaluated:

If[x==y,a,b]

You can add a special fourth argument to (RefLink[If, paclet:ref/If]), which is used if the test does not yield (RefLink[True, paclet:ref/True]) or (RefLink[False, paclet:ref/False]):

If[x==y,a,b,c]

Functions for dealing with symbolic conditions.

The Wolfram Language leaves this as a symbolic equation:

x==y

Unless expr is manifestly (RefLink[True, paclet:ref/True]), (RefLink[TrueQ, paclet:ref/TrueQ])[expr] effectively assumes that expr is (RefLink[False, paclet:ref/False]):

TrueQ[x==y]

Unlike ==, === tests whether two expressions are manifestly identical. In this case, they are not:

x===y

The main difference between lhs===rhs and lhs==rhs is that === always returns (RefLink[True, paclet:ref/True]) or (RefLink[False, paclet:ref/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:

Head[a+b+c]===Times

The == operator gives a less useful result:

Head[a+b+c]==Times

In setting up conditionals, you will often need to use combinations of tests, such as (Subscript[test, 1])&&(Subscript[test, 2])&&…. An important point is that the result from this combination of tests will be (RefLink[False, paclet:ref/False]) if any of the (Subscript[test, i]) yield (RefLink[False, paclet:ref/False]). The Wolfram Language always evaluates the (Subscript[test, i]) in turn, stopping if any of the (Subscript[test, i]) yield (RefLink[False, paclet:ref/False]).

Evaluation of logical expressions.

This function involves a combination of two tests:

t[x_]:=(x!=0&&1/x<3)

Here both tests are evaluated:

t[2]

Here the first test yields (RefLink[False, paclet:ref/False]), so the second test is not tried. The second test would involve 1/0, and would generate an error:

t[0]

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.