Booleans

Math 250 - Mathematical Computing
Christopher Hanusa

7.1 Overview

In this tutorial, we learn about Boolean values, comparison operators, built-in commands that produce Boolean values, and the If statement.

7.2 Boolean values

Determining whether an expression is logically true or logically false is one of the things programming languages do well. In every programming language, there are special words for the logical concepts of true and false. As you might expect with anything pre-defined in Mathematica, they start with a capital letter: True and False. These are not the same as the strings “True” and “False”.

{True,False}

The exclamation mark (when applied to a logical expression) changes a True to a False and vice versa.  I read it as “NOT”.

{!True,!False}

When you have two or more expressions and you want to verify that all are true, use And (or &&). If you want to verify that at least one is true, use Or (or ||).

True && True && True && True

True && True && True && True

False ||False||False||False

False ||False||False||False

7.3 Comparison operators

One type of operation that will give an answer of True or False is when you compare two quantities.  You can ask if a first quantity is less than (<), less than or equal to (<=), equal to (==), greater than or equal to (>=) or greater than (>) a second quantity.  These are called comparison operators

Important: We are using a double equals == instead of a single equals = because = is exclusively used to define variables.  I read == as a question, kind of like (Overscript[=, ?]) .

Compare the following. The first defines x to be 4 and the second asks the question whether x is equal to 4.

x=4

x==4

Example 7.3.1. Which is larger, (2.2^3.3) or (3.3^2.2) ?

7.4 Q Commands

There are a number of commands in Mathematica that ask and answer a True or False question --- they all end in the letter Q.  For example, EvenQ determines if the number is even or not.  Evaluate the next cell.

EvenQ[3]
EvenQ[4]
EvenQ[Sqrt[2]]

The command PrimeQ determines if a number is prime.  Evaluate the next cell.

PrimeQ[1]
PrimeQ[55]
PrimeQ[97]
PrimeQ[1234577]

The Testing Expressions Guide in the Documentation Center gives a list of many “Q” functions; you can find more by searching for “Q” in the Documentation Center and scrolling through the results. This will give you a good feel for all the sorts of True/False questions you can have Mathematica answer.

Example 7.4.1.  Is (2^31)-1 prime?

Example 7.4.2.  Is (e^(iπ)) an integer?

Example 7.4.3.  Is the number 91 one of the first 100 prime numbers?

7.5 If Statements

The natural habitat of comparison operators and Q commands is in an If statement.  An If statement evaluates in different ways depending on whether a the condition you specify is True.  The syntax for If is:

?If

That is, when you type If[condition, expression1, expression2], Mathematica will first check the condition.  If the condition evaluates to True, then it will do expression1; whereas, if it evaluates to False, it will do expression2.

z=10;
If[z^2<50,Print["z is small"],Print["z is large"]]

Example 7.5.1. Use an If statement to define a function abs that gives the absolute value of an input.

In Mathematica, use the Which command instead of nested If statements.

Example 7.5.2. Replace the following nested If statement by a Which command.

If[grade≥90,"A",If[grade≥80,"B",If[grade≥70,"C",If[grade≥60,"D","F"]]]]