Lab 3: Experiment 5

Logical Expressions

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

For example, suppose we wish to find out if the value of i is within the range 1 to 100. Mathematically, you can write

1 <= i <= 100
However, while C++ is very mathematically based, mathematics can often be too ambiguous for C++. It's unclear which operator should be done first. Mathematically, we do both of the at the same time, but C++ can't do this. Unfortunately, we don't get an error for compiling this expression; C++ finds a different and (for us) very undesirable meaning for the expression.

Modify your program to evaluate and print the result of this expression. Compile and execute your program.

Question #3.5.1: Fill in this chart using your program:
i 1 <= i <= 100 should be
The "should be" column is the mathematical relationship. The 1<=i<=100 column is the result given by your C++ program. As hinted above, there should be some disagreement between the two columns.

In order to make this expression palatable for C++, we have to break it into two comparisons with two subexpressions. (Hmmmm... two operators, two subexpressions. Coincidence?) In English, we might ask if "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!

Let's turn the English into an 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're just need the ability to express the "and" relationship. C++ gives us the && operator:
1 is less than or equal to i and i is less than or equal to 100
1 <= i && i <= 100
To make it easier to read, you'll find many C++ programmers add some parentheses:

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

Try this instead in your program. Compile and execute it.

Question #3.5.2: Fill in this chart using your program:
i (1 <= i) && (i <= 100) should be
The "should be" column should be the same as before.

C++ gives three logical operators:
&& and, produces true if and only if both of its operands are true.
|| or, produces true if and only if either of its operands are true.
! not, produces true if and only its operand is false.

The AND and OR operators each require two operands. The NOT operator requires only one operand.

Boolean Expressions

Relational expressions and logical expressions both produce bool values as their result. For this reason, such expressions are collectively referred to as boolean expressions.

Boolean expressions have a variety of uses. One of these is the assert() mechanism, that allows you to halt the program if something bad happens, like a variable gets out of range. For example, suppose we have a problem that requires us to input a number i. Further suppose that this number must be non-zero, or the program will be unable to solve the problem correctly.

The requirement that the number be non-zero is known as a precondition; a precondition is an expression---a condition---that must be true in order for the code to succeed.. We can check a precondition using the assert() mechanism like so:

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

The parentheses in this statement are required. You can put any boolean expression between those parentheses. If the 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 assert after the input statement that reads in a value for i.

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

Question #3.5.4: Write down the exact message that your program gives you when the assert fails.


boolean expression, diagnostic message, precondition
Back to the Lab Exercise  |  Forward to the Next Experiment
© 2003 by Prentice Hall. All rights reserved.
Report all errors to Jeremy D. Frens.