Experiment 5: Logical Expressions


It is sometimes useful to be able to combine relational expressions. For example, suppose we wish to find out if the value of x is within the range 1 to 100. Mathematically, you can write

   1 <= x <= 100
However, C++ is not Mathematics: if you try this and x is negative,
  1. The left relational subexpression
          1 <= x
    	
    is evaluated, producing false, which C++ represents by zero; and
  2. C++ uses that zero as the left-operand of the right relational subexpression, giving us
          0 <= 100
    	
    which evaluates to true -- which not what we intended!
Using express.cpp, try it and see for yourself! Simply writing mathematical expressions in C++ can thus give incorrect answers.

To do this sort of thing in C++, what is needed is to join the two relational expressions

   1 <= x
and
   x <= 100
using one of the logical operators. These operators include:
&& AND, produces true iff and only if both of its operands are true.
|| OR, produces true iff and only if either of its operands are true.

The AND and OR operators each require two operands. A third logical operator is the NOT operator (!), which is applied to just one relational operand (i.e., !(1 <= x)) and produces true if that expression is false, and produces false if that expression is true.

To solve the problem mentioned above, we can use the AND operator:

   1 <= x && x <= 100
For this problem, we could have instead used the OR operator:
   1 > x || x > 100
and still achieved the correct result. Try one of these in express.cpp, and verify that it gives you the correct result for both positive and negative values.

Now it is your turn: in the space below, write a logical expression using the AND operator that produces the value true if the value of char variable ch is uppercase (i.e., within the range 'A' to 'Z', and produces false otherwise. Test that it is correct using express.cpp.


Then rewrite your logical expression using the OR operator (and different relational operators) so that it produces the same results. (Hint: Use the NOT operator.) As before, test that it is correct using express.cpp


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. For example, suppose we have a problem that requires us to input a number x, and the nature of the problem is such that the number must be non-zero, or we will be unable to solve the problem correctly (perhaps the number will be used in a division). This requirement that the number must be non-zero is called a precondition. We can check that such a precondition is true using the assert() mechanism, by typing

  #include <cassert>      // above main
                          // ...
   cin >> x;              // within main
   assert(x != 0);
Once the user enters a value for x, the assert() mechanism will evaluate the boolean expression within the parentheses. If that 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 (i.e., was false). Try this in express.cpp, and in the space below, record the diagnostic message that is displayed when your precondition fails:



You should now know about the logical operators, and how they can be used to combine relational expressions into arbitrarily complex boolean expressions. You should also know about how preconditions can be expressed as boolean expressions, and how the assert() mechanism can be used to check preconditions.


Back to the Experiment List

Forward to the Next Experiment


Copyright 1998 by Joel C. Adams. All rights reserved.