Logical Operator Facts

As mentioned earlier in this chapter, the C++ logical OR and logical AND operators have a lower precedence than relational operators. This means that an expression such as this

x > 5 && x < 10

is interpreted this way:

(x > 5) && (x < 10)

The ! operator, on the other hand, has a higher precedence than any of the relational or arithmetic operators. Therefore, to negate an expression, you should enclose the expression in parentheses, like this:

!(x > 5)     // is it false that x is greater than 5
!x > 5       // is !x greater than 5

Incidentally, the second expression here is always false because !x can have only the values true or false, which get converted to 1 or 0.

The logical AND operator has a higher precedence than the logical OR operator. Thus this expression:

age > 30 && age < 45 || weight > 300

means the following:

(age > 30 && age < 45) || weight > 300

That is, one condition is that age be in the range 31–44, and the second condition is that weight be greater than 300. The entire expression is true if one or the other or both of these conditions are true.

You can, of course, use parentheses to tell the program the interpretation you want. For example, suppose you want to use && to combine the condition that age be greater than 50 or weight be greater than 300 with the condition that donation be greater than 1,000. You have to enclose the OR part within parentheses:

(age > 50 || weight > 300) && donation > 1000

Otherwise, the compiler combines the weight condition with the donation condition instead of with the age condition.

Although the C++ operator precedence rules often make it possible to write compound comparisons without using parentheses, the simplest course of action is to use parentheses to group the tests, whether or not the parentheses are needed. It makes the code easier to read, it doesn’t force someone else to look up some of the less commonly used precedence rules, and it reduces the chance of making errors because you don’t quite remember the exact rule that applies.

C++ guarantees that when a program evaluates a logical expression, it evaluates it from left to right and stops evaluation as soon as it knows what the answer is. Suppose, for example, that you have this condition:

x != 0  && 1.0 / x > 100.0

If the first condition is false, then the whole expression must be false. That’s because for this expression to be true, each individual condition must be true. Knowing the first condition is false, the program doesn’t bother evaluating the second condition. That’s fortunate in this example because evaluating the second condition would result in dividing by zero, which is not in a computer’s repertoire of possible actions.

..................Content has been hidden....................

You can't read the all page of ebook, please click here login for view all page.
Reset
18.191.74.66