Mathematica 9 is now available
Previous section-----Next section

1.5.6 Relational and Logical Operators

x Equal y equal (also input as x  y)
x NotEqual y unequal (also input as x  y)
x > y greater than
x GreaterEqual y greater than or equal to (also input as x  y)
x < y less than
x LessEqual y less than or equal to (also input as x  y)
x Equal y Equal z all equal
x NotEqual y NotEqual z all unequal (distinct)
x > y > z, etc. strictly decreasing, etc.

Relational operators.
This tests whether 10 is less than 7. The result is False.

In[1]:=  10 < 7

Out[1]=

Not all of these numbers are unequal, so this gives False.

In[2]:=  3 NotEqual 2 NotEqual 3

Out[2]=

You can mix < and LessEqual.

In[3]:=  3 < 5 LessEqual 6

Out[3]=

Since both of the quantities involved are numeric, Mathematica can determine that this is true.

In[4]:=  Pi^E < E^Pi

Out[4]=

Mathematica does not know whether this is true or false.

In[5]:=  x > y

Out[5]=

!p not (also input as  p)
p && q && ... and (also input as p  q  ... )
p || q || ... or (also input as p  q  ... )
Xor[p, q, ... ] exclusive or (also input as p  q  ... )
Nand[p, q, ... ] and Nor[p, q, ... ] nand and nor (also input as  and  )
If[p, then, else] give then if p is True, and else if p is False
LogicalExpand[expr] expand out logical expressions

Logical operations.
Both tests give True, so the result is True.

In[6]:=  7 > 4 && 2 NotEqual 3

Out[6]=

You should remember that the logical operations Equal, && and || are all double characters in Mathematica. If you have used a programming language such as C, you will be familiar with this notation.

Mathematica does not know whether this is true or false.

In[7]:=  p && q

Out[7]=

Mathematica leaves this expression unchanged.

In[8]:=  (p || q) && !(r || s)

Out[8]=

You can use LogicalExpand to expand out the terms.

In[9]:=  LogicalExpand[ % ]

Out[9]=



Any questions about topics on this page? Click here to get an individual response.Buy NowMore Information
THIS IS DOCUMENTATION FOR AN OBSOLETE PRODUCT.
SEE THE DOCUMENTATION CENTER FOR THE LATEST INFORMATION.