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: