CS120
Lab #3

Due at the end of lab.

This lab uses logical comparisons and Boolean operators. These operators are used to create the conditions that govern whether a loop will be repeated, whether the code within an if statement will be executed, and which branch of an if-else statement will be executed.

Comparison operators compare two values and evaluate to true (actually the value 1) if the comparison is true; and evaluate to false (actually the value 0) if the comparison is false. For example, the expression 7 < 8 has the value 1, whereas the expression 8 < 7 has the value 0.

The common comparison operators are:
<   (less than) Example: A < B is true if A is less than B and false otherwise
>   (greater than) Example: A > B is true if A is greater than B and false otherwise
<=   (less than or equal to) Example: A >= B is true if A is less than or equal to B and false otherwise
>=   (greater than or equal to) Example: A >= B is true if A is equal to or greater than B and false otherwise
==   (equal) (note the double equal signs) Example: A == B is true if A is equal to B and false otherwise
!=   (not equal) Example: A != B is true if A is not equal to B and false otherwise

Boolean operators (named after the mathematician and logician George Boole) make it possible to create more complex logical tests. If you want to determine if two things, A and B, are true, or if either of two things, A or B, are true, then you need to use Boolean operators. For example, the expression:
(x < y) && (x < z)
is true if the value held by variable x is less than the value held by variable y and (the symbols && mean and) the value held by variable x is less than the value held by variable z.

The common Boolean operators are:
||     (OR) example: A || B is true if A is true or B is true (or both are true) and false otherwise. (The vertical line is usually above the \ key on the keyboard.)
&&   (AND) example: A && B is true only if A is true and B is true, otherwise its false.
!     (NOT) example: !A is true if A is false and is false otherwise

This lab consists of three parts. In part one you will hypothesize whether each of the Boolean statements given below is true or false. Write your hypotheses on a piece of a paper to turn in. You will not be graded on whether you get these right or wrong, but you do need to turn them in.

In part two you should write a program to determine whether the Boolean statements given below are true or false. Remember that C++ (and C) use a 1 for true and a 0 for false. Thus, to determine the validity of a statement you only need to embed it in a cout statement. For example, the command:
cout << (7 < 9) || (4 < 0) << endl;
will print 1 if the statement is true and 0 if the statement is false.

Make sure you can print and turn in the results from your program as well as the program itself. (Remember to use the script command to create a printable output file. The command script lab2output will create a file called lab2output that you can print.

For part three use the results of the program to mark on your original sheet of hypotheses which hypotheses were correct and which ones were incorrect.

Boolean expressions to test:

  1. 7 < 8
  2. 10 > 10
  3. (7 < 10) || (7 > 10)
  4. (7 < 10) && (7 > 10)
  5. -5 >= 5
  6. (7 > 10) || (-5 == 5)
  7. !(6 == 7)
  8. (10 != 11) && (7 > 8)
  9. (7 == 6) || (7 == 7) || (7 == 8)
  10. (6 < (8 && 9)) [This one is tricky.]
  11. (1 == (6 < 8)) [Also tricky.]
  12. (0 == (0 || 1)) [Still more trickiness.]
  13. 6 < (5 + 2)
  14. 6 + (5 == 5) [Not entirely a Boolean statement.]
  15. (6 < 7) + (7 >= 5) [Also, not entirely Boolean.]

Turn in: Your page of hypotheses regarding the 15 Boolean expressions, with the correct answer as determined by your program. A print out of the program (or programs) that you used to test your hypotheses and the output of the program(s).