CS120
Lab #2

This lab introduces 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 false (actually the value 0) if the comparison is false, and evaluate to true (the value 1) if the comparison is true. 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 is false otherwise
>   (greater than) Example: A > B is true if A is greater than B and is false otherwise
<=   (less than or equal to) Example: A <= B is true if A is less than or equal to B and is false otherwise
>=   (greater than or equal to) Example: A >= B is true if A is equal to or greater than B and is false otherwise
==   (equal) (note the double equal signs) Example: A == B is true if A is equal to B and is false otherwise
!=   (not equal) Example: A != B is true if A is not equal to B and is 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 symbol && means 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 is 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 it is false.
!     (NOT) example: !A is true if A is false and is false otherwise

This lab consists of three parts.

Part I: Hypothesize whether each of the 20 Boolean statements and mathematical expressions given below is true or false. Write your hypotheses in a file named hypotheses.txt that can be uploaded to Canvas. You will not be graded on whether you get these right or wrong, but you do need to turn them in.

Consider which expressions were the most difficult to understand. Create 3 more of your own expressions to try to clarify how Boolean and Mathematical expressions are evaluated in C/C++. Add these expressions along with your hypotheses about their values to the list given below (for a total of 23 expressions).

Part II: Write a program to determine whether the Boolean statements and mathematical expressions given below, plus the three that you created, are true or false. This program will be used to test your hypotheses. Remember that C++ (and C) use a 0 for false and 1 for true. Therefore, to determine the validity of a Boolean statement you only need to embed it in a cout statement. For example, the command:
cout << (7 < 9) || (4 < 0) << endl;
will print 0 if the statement is false and 1 if the statement is true.

To determine if a mathematical expression is treated as true or false an if-else statement can be used. For example, to determine whether the expression: 5*6 - 30 is treated as true or false you can use the code:
if (5*6 -30){
cout << "It's true\n";
} else {
cout << "It's false\n";
}

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

Part III: Use the results of your program to mark in your original file (containing your hypotheses) which hypotheses were correct and which ones were incorrect.

Boolean and Mathematical Expressions to Test:

  1. 0
  2. -7
  3. -1 + -2
  4. -7 + 7
  5. 7 < 8
  6. 10 > 10
  7. (7 < 10) || (7 > 10)
  8. (7 < 10) && (7 > 10)
  9. -5 >= 5
  10. (7 > 10) || (-5 == 5)
  11. !(6 == 7)
  12. (10 != 11) && (7 > 8)
  13. (7 == 6) || (7 == 7) || (7 == 8)
  14. (6 < (8 && 9)) [This one is tricky.]
  15. (1 == (6 < 8)) [Also tricky.]
  16. (0 == (0 || 1)) [Still more trickiness.]
  17. 6 < (5 + 2)
  18. 6 + (5 == 5)
  19. (6 < 7) + (7 >= 5)
  20. (0 < 7) - (0 < 7)

Turn in: Your file of hypotheses in hypotheses.txt regarding the expressions, with the correct answers as determined by your program. The source code of the program (or programs) that you used to test your hypotheses and the output of the program(s). All of these items should be submitted to your instructor via Canvas.