if (x <= INT_MAX && x >= INT_MIN) // use climits values
return true;
else
return false;
}
Here is a sample run of the program in Listing 6.7 on a system with a 32-bit int:
Yo, dude! Enter an integer value: 6234128679
Out of range -- please try again: -8000222333
Out of range -- please try again: 99999
You've entered the integer 99999
Bye
Program Notes
If you enter a too-large value to a program reading a type int, many C++ implementations simply truncate the value to fit, without informing you that data was lost. The program in Listing 6.7 avoids that by first reading the potential int as a double. The double type has more than enough precision to hold a typical int value, and its range is much greater. Another choice for holding the input value would be the long long type, assuming that it is wider than int.
The Boolean function is_int() uses the two symbolic constants (INT_MAX and INT_MIN), defined in the climits file (discussed in Chapter 3, “Dealing with Data”), to determine whether its argument is within the proper limits. If so, the program returns a value of true; otherwise, it returns false.
The main() program uses a while loop to reject invalid input until the user gets it right. You could make the program friendlier by displaying the int limits when the input is out of range. After the input has been validated, the program assigns it to an int variable.
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.
Alternative Representations
Not all keyboards provide all the symbols used for the logical operators, so the C++ Standard provides alternative representations, as shown in Table 6.3. The identifiers and, or, and not are C++ reserved words, meaning that you can’t use them as names for variables and so on. They are not considered keywords because they are alternative representations of existing language features. Incidentally, these are not reserved words in C, but a C program can use them as operators, provided that the program includes the iso646.h header file. C++ does not require using a header file.
Table 6.3. Logical Operators: Alternative Representations
The cctype Library of Character Functions