Lab 3: Experiment 5

Logical Expressions

In Experiment #4 we saw how to write simple relational expressions; but it is often useful to be able to combine relational expressions into more complex expressions.

For example, suppose we need to check if the value of i is positive and less than 100. Thinking mathematically, we might write

     0 < i < 100
However, although C++ is mathematically based and this expression is mathematically correct, it (along with many others) would be misinterpreted in C++.

To demonstrate this, modify your program to evaluate and output the value of this expression (0 < i < 100); be sure to enclose this expression in parentheses or you will most likely get a compile error. (The reason for this will become clear later.) Also, if you removed the << boolalpha after cout, put it back so that logical expressions will be output as true or false instead of 1 and 0 (or some other numeric value). Then compile and execute your program.

Question #3.5.1: Fill in this table using the results from your program. The second column is the result given by your C++ program. The third column is the mathematical relationship.

Computer's value for
1 < i < 100
What (mathematically)
it's value should be
a. -5    
b. 50    
c. 5000    

Unfortunately, we don't get an error when compiling this expression because C++ does find a meaning for it; but as the results in the preceding table should indicate, it may be different from what it should be.

To see why this is, proceeding mathematically, we think of the expression as checking whether i is between 1 and 100; that is, we think of the two < operations as being performed simultaneously. But C++ can only perform one operation at a time; that is, it evaluates it either as

(0 < i) < 100
0 < (i < 100)

In order to make this expression work in C++, we have to break it into two comparisons with two subexpressions. In English, we could rephrase it:

"1 is less than or equal to i and i is less than or equal to 100"

Verbally, we've broken it into two separate expressions.

Now let's try turning this version into a C++ expression:

1 is less than or equal to i and i is less than or equal to 100
1 <= i ???? i <= 100

We're close; we only need the ability to express the "and" relationship in C++. It gives us the && operator for this purpose:

(1 < i) && (i < 100)

The parentheses are not necessary in this boolean expression, but many C++ programmers add them to make it easier to read.

Try this as a replacement for 1 < i < 100 in your program. Compile and execute it.

Question #3.5.2: Fill in this table using the results from your program:

Computer's value for
(1 < i) && (i < 100)
What (mathematically)
it's value should be

C++ gives three logical operators that can be applied to boolean expressions:

&&   and — produces true if and only if both of its operands are true.
||   or — produces true if and only if either or both of its operands are true.
!   not — produces true if and only its operand is false.

Note that the NOT operator consists of only one symbol, but the AND and OR operators each consist of two symbols. (Caution: & and | are bitwise operators and are not the same as && and ||.)

Using Boolean Expressions

Boolean expressions have a variety of uses. One of these is with the assert() mechanism that allows you to halt a program if something "bad" happens such as a variable getting out of range. It is defined in the cassert library, so one must remember to add

#include <cassert>

to the list of other #includes in the program.

To illustrate how it is used, suppose we have a problem that requires inputting a number i as in the program for this lab exercise and that this number must be nonzero or the program will be unable to solve the problem correctly. Such a requirement is known as a precondition; it is an expression —a condition — that must be true in order for the program to continue.

We can check a precondition using the assert() mechanism as follows:

assert(i != 0);
This statement should be placed after the statement that inputs (or computes) a value for i.

The parentheses in this statement are required and any boolean expression can be used between them. If this boolean expression is true, execution will continue as usual; but if the boolean expression is false, the program will terminate and a diagnostic message will be displayed, listing the precondition that failed.

Add this assertion after the input statement that inputs a value for i. Also, add #include <cassert> to your list of #includes.

Question #3.5.3: What value do you need to enter to make the assertion fail?

Question #3.5.4: Write down (or copy and paste) the exact message that your program gives you when the assertion fails.

Back to the Lab Exercise  |  Forward to the Next Experiment

Report errors to Larry Nyhoff (