5.3.3. Boolean Values and Expressions

Boolean values and operations

There are just two values of type bool: true and false. They are used as the values of expressions that have yes-or-no answers.

The following table shows some operators that yield boolean results and some operations on boolean values.

x == y

 True if x and y are the same value.

x != y

 True if x and y are not the same value.

x > y

 True if x is greater than y.

x < y

 True if x is less than y.

x >= y

 True if x is greater than or equal to y.

x <= y

 True if x is less than or equal to y.

!x

 This is true if x is false, and false if x is true.

x && y

 This is true if both x and y are true, and false if either of them is false. If x is false, then y is not evaluated at all. For example, expression ``` 3 > 4 && max(z,w) == w ``` does not compute max(z,w) or ask whether its result is equal to w.

x || y

 This is true if either x is true or y is true or both are true. If x is true, then y is not evaluated.

Treating integers as boolean values (optional)

Java type boolean has only the two values true and false. C++ is different from Java in that type bool is actually equivalent to type int. Constant true is 1 and constant false is 0.

It is considered good practice, though, to write true and false in your program for boolean values rather than 1 and 0. The coding standards for this course require you to do that.

Whenever an integer value is tested to see whether it is true of false, 0 is considered to be false and all other integers are considered be true. Operators !, && and || always yield either 0 or 1. For example, expression 3 && −2 yields true.

Translating from English to C++.

Be careful when translating from English to C++. English allows you to take liberties that C++ does not. Common mathematical notation also allows you to write things that C++ does not allow.

x < y < z

 You cannot write combinations of comparisons this way. Write x < y && y < z.

x is 1 or 2

 Do not try to write this as x == 1 || 2. Each operand of || is treated as a boolean expression. Expression x == 1 || 2 is the same as (x == 1) || (2), which treats 2 as a boolean value (which is considered to be true). To ask if x is 1 or 2, say x ==1 || x == 2.

x is even

 In English, is sometimes means "is equal to", and sometimes means "has the property". For example, "Jumbo is an elephant" does not indicate that "Jumbo" and "an elephant" are the same thing, but that Jumbo has the property of being an elephant. In C++, some functions, called predicates, yield boolean values. Predicates are used to check for properties. For example, suppose that even is a predicate where even(x) yields true if x is even and false if x is odd. To use a predicate, treate it like any other function. If you want to ask if x is even, write even(x). Do not write x == even. If you have a function isPrime(x) that returns true if x is prime, then test isPrime(n) works to check whether n is prime. Do not write x == isPrime.

isPrime(n) !isPrime(n)

 To ask if n is prime, just write isPrime(n). Do not write isPrime(n) == true. To test whether n is not prime, write !isPrime(n), not isPrime(n) == false. The coding standards require you not to write == true or == false.

Boolean expressions yield values

Some students get the idea that a boolean expression can only occur in a test, such as in an if-statement. But that is not true. If x is 2, then expression x > 0 has value true. Instead of

```  if(x > 0)
{
return true;
}
else
{
return false;
}
```
why not just write
```  return x > 0;
```
which has the same effect. The coding standards require you to write the latter rather than the former.

Exercises

1. What is the value of expression 4 > 3? Answer

2. What is the value of expression 6 >= 9? Answer

3. What is the value of expression 3 > 4 || 6 >= 9? Answer

4. What is the value of expression 2 == 2 && 5 >= 3? Answer

5. What is the value of expression !(3 == 3)? Answer

6. Give an expression that is equivalent to !(x == y), but that does not use the negation operator, !. Answer

7. Give an expression that is equivalent to !(x > y), but that does not use the negation operator, !. Answer

8. Give an expression that is true if x, y and z are all equal to one another. Answer

9. Expression isPrime(n) == false is allowed in C++, but is not allowed by the coding standards. What is an equivalent expression that is allowed by the coding standards? Answer