5.2. Relational and Logical Operators
Table 5.2. Relational and Logical Operators
Logical AND and OR Operators
The logical operators treat their operands as conditions (Section 1.4.1, p. 12). The operand is evaluated; if the result is zero the condition is false, otherwise it is true. The overall result of the AND operator is TRue if and only if both its operands evaluate to TRue. The logical OR (||) operator evaluates to true if either of its operands evaluates to true. Given the forms
expr2 is evaluated if and only if expr1 does not by itself determine the result. In other words, we're guaranteed that expr2 will be evaluated if and only ifIn a logical AND expression, expr1 evaluates to TRue. If expr1 is false, then the expression will be false regardless of the value of expr2. When expr1 is true, it is possible for the expression to be true if expr2 is also TRue.In a logical OR expression, expr1 evaluates to false; if expr1 is false, then the expression depends on whether expr2 is true.
expr1 && expr2 // logical AND
expr1 || expr2 // logical OR

In this case, we combine our two tests in the condition in the while. First we test whether it has reached the end of the string. If not, it refers to a character in s. Only if that test succeeds is the right-hand operand evaluated. We're guaranteed that it refers to an actual character before we test to see whether the character is a space or not. The loop ends either when a space is encountered or, if there are no spaces in s, when we reach the end of s.
string s("Expressions in C++ are composed...");
string::iterator it = s.begin();
// convert first word in s to uppercase
while (it != s.end() && !isspace(*it)) {
*it = toupper(*it); // toupper covered in section 3.2.4 (p. 88)
++it;
}
Logical NOT Operator
The logical NOT operator (!) treats its operand as a condition. It yields a result that has the opposite truth value from its operand. If the operand evaluates as nonzero, then ! returns false. For example, we might determine that a vector has elements by applying the logical NOT operator to the value returned by empty:
The subexpression
// assign value of first element in vec to x if there is one
int x = 0;
if (!vec.empty())
x = *vec.begin();
evaluates to TRue if the call to empty returns false.
!vec.empty()
The Relational Operators Do Not Chain Together
The relational operators (<, <=, >, <=) are left associative. The fact that they are left associative is rarely of any use because the relational operators return bool results. If we do chain these operators together, the result is likely to be surprising:
As written, this expression will evaluate as true if k is greater than one! The reason is that the left operand of the second less-than operator is the TRue/ false result of the firstthat is, the condition compares k to the integer values of 0 or 1. To accomplish the test we intended, we must rewrite the expression as follows:
// oops! this condition does not determine if the 3 values are unequal
if (i < j < k) { /* ... */ }
if (i < j && j < k) { /* ... */ }
Equality Tests and the bool Literals
Section 5.12.2 (p. 180) a bool can be converted to any arithmetic typethe bool value false converts to zero and true converts to one.

Either val is itself a bool or it is a type to which a bool can be converted. If val is a bool, then this test is equivalent to writing
if (val == true) { /* ... */ }
which is shorter and more direct (although admittedly when first learning the language this kind of abbreviation can be perplexing).More importantly, if val is not a bool, then comparing val with true is equivalent to writing
if (val) { /* ... */ }
which is very different from
if (val == 1) { /* ... */ }
in which any nonzero value in val is true. If we write the comparison explicitly, then we are saying that the condition will succeed only for the specific value 1.
// condition succeeds if val is any nonzero value
if (val) { /* ... */ }
Exercises Section 5.2
Exercise 5.5:Explain when operands are evaluated in the logical AND operator, logical OR operator, and equality operator.Exercise 5.6:Explain the behavior of the following while condition:
Exercise 5.7:Write the condition for a while loop that would read ints from the standard input and stop when the value read is equal to 42.Exercise 5.8:Write an expression that tests four values, a, b, c, and d, and ensures that a is greater than b, which is greater than c, which is greater than d.
char *cp = "Hello World";
while (cp && *cp)