|
|
|
| Value of x | Value of y | Value of x || y | | TRUE | | | | TRUE | | | | FALSE | | | | FALSE | | |
|
|
|
|
|
|
The following table summarizes the results of applying the ! operator to a logical expression (represented by Boolean variable x). |
|
|
|
|
| Value of x | Value of ! x | | TRUE | | | FALSE | |
|
|
|
|
|
|
Technically, the C++ operators !, &&, and || are not required to have logical expressions as operands. Their operands can be of any simple data type, even floating point types. You sometimes encounter C++ code that looks like this: |
|
|
|
|
|
|
|
|
float height;
Boolean badData;
.
.
.
cin >> height;
badData = !height; |
|
|
|
|
|
|
|
|
The ! operator yields 1 (TRUE) if its operandwhatever the data typehas the value zero (FALSE). The ! operator yields 0 (FALSE) if its operand has any nonzero value (TRUE). So, what the assignment statement above really is saying is, Set badData to TRUE if height equals 0.0 Although the assignment statement works correctly according to the C++ language, many programmers find the following statement to be more readable: |
|
|
|
|
|
|
|
|
badData = (height == 0.0); |
|
|
|
|
|
|
|
|
Throughout this text, we apply the logical operators only to logical expressions, not to arithmetic expressions. |
|
|
|
|
|