
This is the Title of the Book, eMatter Edition
Copyright © 2007 O’Reilly & Associates, Inc. All rights reserved.
134
|
Chapter 5: Operators
if (xPos < 0 || xPos > 100) {
trace ("xPos is not between 0 and 100 inclusive.");
}
Note that the variable xPos must be included in each comparison. The following
code shows a common mistaken attempt to check
xPos’s value twice:
// Oops! Forgot xPos in the comparison with 100
if (xPos < 0 || > 100) {
trace ("xPos is not between 0 and 100 inclusive.");
}
Logical AND
Like the logical OR operator, logical AND is used primarily to execute a block of
code conditionally—in this case, only when both of two conditions are met. The log-
ical AND operator takes the general form:
operand1 && operand2
Both operand1 and operand2 can be any valid expression. In the simplest case, in
which both operands are Boolean expressions, logical AND returns
false if either
operand is
false and returns true only if both operands are true. In summary:
true && false // false because second operand is false
false && true // false because first operand is false
true && true // true because both operands are true
false && false // false because both operands are false (either is sufficient)
Let’s see how the logical AND operator is used in two examples. In Example 5-4, we
execute a trace() statement only when two variables are both greater than 50.
Because the expressions x>50 and y>50 are both
true ...