< previous page page_194 next page >

Page 194
The following pairs of expressions are equivalent:
ExpressionEquivalent Expression
!(a == b)a != b
!(a == b || a == c)a !== b && a != c
!(a == b && c > d)a !=b || c <= d

Take a close look at these expressions to be sure you understand why they are equivalent. It may help to try evaluating them with some values for a, b, c, and d. Notice the pattern here: The expression on the left is just the one to its right with ! added and the relational and logical operators reversed (for example, == instead of ! = and ¦¦ instead of &&). Remember this pattern. It allows you to rewrite expressions in the simplest form.*
Logical operators can be applied to the results of comparisons. They also can be applied directly to variables of type Boolean. For example, instead of writing
isElector = (age >>= 18 && district == 23);
to assign a value to Boolean variable isElector, we could use two intermediate Boolean variables, isVoter and isConstituent:
isVoter = (age >= 18);
isConstituent = (district == 23);
isElector = isVoter && isConstituent;
The two tables below summarize the results of applying && and ¦¦ to a pair of logical expressions (represented here by Boolean variables x and y).
Value of xValue of y:Value of x && y
TRUE
TRUE
TRUE
TRUE
FALSE
FALSE
FALSE
TRUE
FALSE
FALSE
FALSE
FALSE

3e26ecb1b6ac508ae10a0e39d2fb98b2.gif 3e26ecb1b6ac508ae10a0e39d2fb98b2.gif
*In Boolean algebra, the pattern is formalized by a theorem called DeMorgan's Law.

 
< previous page page_194 next page >