CS 1440 Lab 2
Cover sheetPrevious ActivityNext Activity

Activity 2-6: Logical Expressions

Activity Procedure


A logical expression is any expression that can be tested to see if it is true or false; that is, tested to see if it is satisfied or not satisified.

The simplest form of a logical expression consists of two expressions that are compared with a comparison operator. The operators are shown in the table below. Note that some of the operators are two symbols and that no space is allowed between the two symbols.

Comparison Operators
C++ operator
English
C++ example
= =
equal to x+x == 2*x
!=
not equal to answer != 'N'
<
less than count < 3
<=
less than or equal to time <= limit
>
greater than time > limit
>=
greater than or equal to age >= 21

You can build logical expressions by combining two comparisons using the "and" and "or" operators. The C++ operator for "and" is && and || is the C++ operator for "or."

When combining two comparisons with && , the entire expression is true provided both of the comparisons are true. For example the combined expresssion,

(x > 2) && (x < 7)

is only true when (x > 2) is true AND (x < 7) is true. Thus, this expression is true when x has the values 3, 4, 5, or 6. When x has the value 7, the comparison (x > 2) is true because 7 is greater than 2. But (x < 7) is false since 7 is not less than 7 (it is equal). Since both comparisons were not true, the entire expression is false.

When combining two comparisons with ||, the entire expression is true provided one or both of the comparisons are true. For example the combined expresssion,

(y < 0) || (y < 3)

is true when (y < 0) is true AND/OR (y < 3) is true. Thus, this expression is true when y has any negative value, 0, 1, and 2. When y has the value -1, the comparison (y < 0) is true and (y < 3) is also true. Hence, the entire expression is true. When y has the value 0, the comparison (y < 0) is false, but (y < 3) is true. And again, the entire expression is true. When y has the value 3, the comparison (y < 0) is false, and (y < 3) is also false. Since neither comparison is true, the entire expression is false.

The C++ NOT operator is written "!". For example,

!(x>3 && x<5)

means that it is not the case that x is greater than 3 and less than 5. This is the same as the expression

x<=3 || x>=5

We have now looked at three kinds of operators: arithmetic (+, -, *, /, %), relational (<, >, <=, >=, = =, !=) and logical (!, &&, ||). All three varieties can be mixed in a single expression, so we need a "precedence chart" to determine the order of evaluation. Here it is:
 
 
( ) 
!
*, /, %
+, -
<, <=, >, >=, = =, !=
&&
||

For example,

3 < 2 || 5 > 1 && !(5 == 7)

is

false || true && !false
false || true && true
false || true
true

In C++ false in actually stored as 0; any non-zero number is regarded as "true". When C++ wants to say "true" it will return the value 1, so the value of the expression above is actually 1.

Follow this link to practice evaluating logical expressions. Again, we strongly recommend that you keep trying until you get five correct in a row.

IF-ELSE Statement examples

Here are some examples of correct IF-ELSE statements. Of course, the output depends on the values of the variables used in the logical expressions. Try out a couple of these examples by creating a small program and copying-and-pasting in the example code. Make sure to declare and give values to each of the variables. Investigate what output results from different variable values.


if  ( (temperature >= 95) || (humidity >= 90) )
        cout << "Whew! Where is some AC?";


if  ( x > -1 && x < 1 )
        cout << "x is zero!!";
else
        cout << "x is non-zero!!";


if  ( time > limit )
{
        overlimit = overlimit + 1;
        cout << overlimit << " times over the limit.";
}


if  ( time != alarm )
        time = time + 1;
else
{
        time = time + 1;
        cout << "Wake up!!";
}