Comparisons and Boolean Values


Boolean values: true and false

A program often needs to ask questions and make decisions about what to do based on the answers. Shortly, we will look at the issue of deciding what to do. Right now, let's just look at the first step, asking questions.

We will only look at what you might call yes/no questions. But instead of yes and no, we will say that the answer to each such question is either true or false.

Values true and false are called boolean values.


Comparisons

A simple and natural way to ask a question is to perform a comparison. You can use < and > to ask whether numbers are less than or greater than one another. For example, 4 < 5 is true, but 4 < 3 is false.

There are other comparisons that you can use, but they look different from what you are used to. The following table shows mathematical operators and corresponding operators in Cinnameg.

Mathematical comparison     Cinnameg comparison
<   <
  <=
>   >
  >=
=   ==
  =/=

For example, expression 5 == 4 has value false. Be sure to notice that, to ask whether two values are equal, you use a double-equal sign.


Comparisons are expressions

In programming languages, comparisons are considered expressions that have values. You evaluate expression 4 < 5, getting result true, in the same general way that you evaluate expression 4 + 5, getting result 9. Special values true and false are considered genuine values.

You can even give a name to the result of a comparison. For example,

  Let c = 9 > 2.
  Let d = 9 < 2.
makes c = true and d = false.


And, or and not

You often find that you want to combine questions, asking whether two comparisons are both true, or whether at least one of them is true. You do that in Cinnameg using operators and and or. If p and q are two expressions that produce boolean values, then

For example, expression (3 <= 4 and 4 <= 5) has value true, but (3 < 4 and 4 == 5) has value false, since the second comparison is false.

You can switch true to false and false to true using not. Expression not e is true if e is false, and is false if e is true.

You can combine any two expressions that produce a boolean result using and and or. For example,

  Let a = 8 =/= 9.
  Let b = 2 < 4.
  Let c = a and b.
makes c = true, since both a and b are true.


Warning

In mathematics and in English, you are accustomed to using abbreviations, sometimes leaving things out and expecting the reader or listener to fill them in. But in a programming language, you must avoid doing that.

In mathematics, it is common to write x < y < z to mean that x is less than y, which in turn is less than z. But if you write

  Let q = x < y < z.
you get an error, since this abbreviation is not allowed. Spelling it out in more detail, what you want to know is whether x < y and y < z. So that is what you should write.
  Let q = x < y and y < z.

Another common mistake is to omit things. In English, you might say "If x and y are equal to 0 ...". It is tempting to ask whether x and y are both 0 by writing expression

  x and y == 0
But that does not mean what you want. Both operands of and are required to be boolean values. Since the left-hand operand of and is x, the compiler presumes that x is a boolean value. The correct way to ask whether x and y are both equal to 0 is to write
  x == 0 and y == 0


Precedence

We have now seen a few more operators. Here are their relative precedence values, from high to low.

not, - (negation) high precedence
*, /, `div`, `mod`  
+, - (subtraction)  
<, >, <=, >=, ==, =/=  
and  
or low precedence

So, for example, and's are done before or's. You can always add parentheses to ensure the operations are done in a particular order.


Short circuiting

The and and or operators have a property that we will find important later on. Imagine evaluating expression 3 > 4 and 5 == 5. The program starts by evaluating 3 > 4, which is false. But there is no point in finding out the value of expression 5 == 5, since the and is clearly going to be false no matter what. In any expression of the form A and B, if expression A has value false, then B is not computed. Similarly, in A or B, if A is true then B is not evaluated.

We will see examples where this short circuiting is important later.


Problems

  1. [solve] What is the value of expression 2 >= 3?

  2. [solve] What is the value of expression 3 == 4 or 2 <= 8?

  3. [solve] Suppose that you have three values called x, y and z. Write an expression that has value true if x, y and z are in ascending order (with x the smallest, y in the middle and z the largest.)

  4. [solve] Suppose that you have three values called x, y and z. Write an expression that has value true if x, y and z are in nondescending order (with x the smallest, y in the middle and z the largest.) In this problem, some of the values might be the same.

  5. [solve] Suppose that you have three values called x, y and z. Write an expression that has value true if x, y and z are not all equal to one another.


Summary

One of the most useful ways to ask a question is to compare two values to see whether they are equal or whether one is less than another. Be sure to use the correct names for the comparisons. They are: ==, =/=, <, <=, > and >=.

You can combine boolean values using operators and and or. Use not to get the opposite of a boolean value.


Review

Expressions in Cinnameg and Java look very similar to ordinary mathematical expressions. But remember a few differences.

  1. Use * to indicate multiplication.
  2. * and / have the same precedence.
  3. Never write a comma or space in a number
  4. Use parentheses to group things in an expression, but not square brackets or curly braces.


Review questions

  1. [solve] Convert mathematical expression 3(4+2-1) to programming language notation.

  2. [solve] How can you tell if a number is even?