|

Often
you want to ask more than one relational question
at a time. "Is it true that x is greater than y,
and also true that y is greater than z?" A program
might need to determine that both of these conditions
are true, or that some other condition is true,
in order to take an action.
Imagine
a sophisticated alarm system that has this logic:
"If the door alarm sounds AND it is after six p.m.
AND it is NOT a holiday, OR if it is a weekend,
then call the police." C++’s three logical operators
are used to make this kind of evaluation. These
operators are listed in Table 4.2.
Table 4.2. The Logical Operators.
|
Operator
Symbol
|
Example
|
|
AND
&&
|
expression1
&& expression2
|
|
OR
||
|
expression1
|| expression2
|
|
NOT
!
|
!expression
|
Logical
AND
A
logical AND statement evaluates two expressions,
and if both expressions are true, the logical AND
statement is true as well. If it is true that you
are hungry, AND it is true that you have money,
THEN it is true that you can buy lunch. Thus,
if
( (x == 5) && (y == 5) )
would
evaluate TRUE if both x and y are equal to 5, and
it would evaluate FALSE if either one is not equal
to 5. Note that both sides must be true for the
entire expression to be true.
Note
that the logical AND is two && symbols.
A single & symbol is a different operator.
Logical
OR
A
logical OR statement evaluates two expressions.
If either one is true, the expression is true. If
you have money OR you have a credit card, you can
pay the bill. You don’t need both money and a credit
card; you need only one, although having both would
be fine as well. Thus,
if
( (x == 5) || (y == 5) )
evaluates
TRUE if either x or y is equal to 5, or if both
are.
Note
that the logical OR is two || symbols. A single
| symbol is a different operator.
Logical
NOT
A
logical NOT statement evaluates true if the expression
being tested is false. Again, if the expression
being tested is false, the value of the test is
TRUE! Thus
if
( !(x == 5) )
is
true only if x is not equal to 5. This is exactly
the same as writing
if
(x != 5)
Relational
Precedence
Relational
operators and logical operators, being C++ expressions,
each return a value: 1 (TRUE) or 0 (FALSE). Like
all expressions, they have a precedence order (see
Appendix A) that determines which relations are
evaluated first. This fact is important when determining
the value of the statement
if
( x > 5 && y > 5 || z > 5)
It
might be that the programmer wanted this expression
to evaluate TRUE if both x and y are greater than
5 or if z is greater than 5. On the other hand,
the programmer might have wanted this expression
to evaluate TRUE only if x is greater than 5 and
if it is also true that either y is greater than
5 or z is greater than 5.
If
x is 3, and y and z are both 10, the first interpretation
will be true (z is greater than 5, so ignore x and
y), but the second will be false (it isn’t true
that both x and y are greater than 5 nor is it true
that z is greater than 5).
Although
precedence will determine which relation is evaluated
first, parentheses can both change the order and
make the statement clearer:
if
( (x > 5) && (y > 5 || z > 5) )
Using
the values from earlier, this statement is false.
Because it is not true that x is greater than 5,
the left side of the AND statement fails, and thus
the entire statement is false. Remember that an
AND statement requires that both sides be true—something
isn’t both "good tasting" AND "good for you" if
it isn’t good tasting.
NOTE: It is often a good idea to use
extra parentheses to clarify what you want to group.
Remember, the goal is to write programs that work
and that are easy to read and understand.
More
About Truth and Falsehood
In
C++, zero is false, and any other value is true.
Because an expression always has a value, many C++
programmers take advantage of this feature in their
if statements. A statement such as
if
(x) // if x is true (nonzero)
x
= 0;
can
be read as "If x has a nonzero value, set it to
0." This is a bit of a cheat; it would be clearer
if written
if
(x != 0) // if x is nonzero
x
= 0;
Both
statements are legal, but the latter is clearer.
It is good programming practice to reserve the former
method for true tests of logic, rather than for
testing for nonzero values.
These
two statements also are equivalent:
if
(!x) // if x is false (zero)
if
(x == 0) // if x is zero
The
second statement, however, is somewhat easier to
understand and is more explicit.
DO
put parentheses around your logical tests to make
them clearer and to make the precedence explicit.
DO use braces in nested if statements to
make the else statements clearer and to avoid bugs.
DON’T use if(x) as a synonym for if(x !=
0); the latter is clearer. DON’T use if(!x)
as a synonym for if(x == 0); the latter is clearer.
NOTE: It is common to define your own
enumerated Boolean (logical) type with enum Bool
{FALSE, TRUE};. This serves to set FALSE to 0 and
TRUE to 1.
Conditional
(Ternary) Operator
The
conditional operator (?:) is C++’s only ternary
operator; that is, it is the only operator to take
three terms.
The
conditional operator takes three expressions and
returns a value:
|
(expression1)
? (expression2) : (expression3)
|
This
line is read as "If expression1 is true, return
the value of expression2; otherwise, return the
value of expression3." Typically, this value would
be assigned to a variable.
Listing
4.9 shows an if statement rewritten using the conditional
operator.
Listing
4.9. A demonstration of the conditional operator.
1:
// Listing 4.9 - demonstrates the conditional operator
2:
//
3:
#include <iostream.h>
4:
int main()
5:
{
6:
int x, y, z;
7:
cout << "Enter two numbers.\n";
8:
cout << "First: ";
9:
cin >> x;
10:
cout << "\nSecond: ";
11:
cin >> y;
12:
cout << "\n";
13:
14:
if (x > y)
15:
z = x;
16:
else
17:
z = y;
18:
19:
cout << "z: " << z;
20:
cout << "\n";
21:
22:
z = (x > y) ? x : y;
23:
24:
cout << "z: " << z;
25:
cout << "\n";
26:
return 0;
27:
}
Output:
Enter two numbers.
First: 5
Second: 8
z: 8
z: 8
Analysis: Three integer variables are
created: x, y, and z. The first two are given values
by the user. The if statement on line 14 tests to
see which is larger and assigns the larger value
to z. This value is printed on line 19. The conditional
operator on line 22 makes the same test and assigns
z the larger value. It is read like this: "If x
is greater than y, return the value of x; otherwise,
return the value of y." The value returned is assigned
to z. That value is printed on line 24. As you can
see, the conditional statement is a shorter equivalent
to the if...else statement.
|