< previous page page_195 next page >

Page 195
Value of xValue of yValue of x || y
TRUE
TRUE
TRUE
TRUE
FALSE
TRUE
FALSE
TRUE
TRUE
FALSE
FALSE
FALSE

The following table summarizes the results of applying the ! operator to a logical expression (represented by Boolean variable x).
Value of xValue of ! x
TRUE
FALSE
FALSE
TRUE

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.

 
< previous page page_195 next page >