Using Logical (Boolean) Operators

     

Logical operators allow you to make decisions based on evaluating two or more expressions. Typically, you use expressions that naturally and intuitively yield Boolean values.

The three logical operators are AND ( && ), OR (), and NOT ( ! ).

The logical NOT operator returns the Boolean opposite of the single operand that it precedes. Thus, if an expression returns true , with the logical NOT operator, you get false . For instance, say you have a movie clip, myClip , on the Stage, and it is visible:

 trace(myClip._visible); // true trace(!myClip._visible); // false 

Similarly, if an expression returns false and you prefix it with the logical NOT operator, you get true . For instance, say you have a function, checkKillList() , that returns false :

 trace(checkKillList()); // false trace(!checkKillList()); // true 

The AND operator answers the question, "Are both of these expressions true?"

The OR operator answers the question, "Is either of these expressions true?"

The following four pseudocode examples involve two expressions that are either true or false:

Example #1:

 IF starting point is San Francisco AND destination is Berkeley THEN take Bay Area Rapid Transit 

Example #2:

 IF user hits Cancel OR an error occurs THEN cancel operation 

Example #3:

 IF user provides correct password AND user is NOT on "kill" list THEN grant user access 

Example #4:

 IF movie clip does NOT exist OR movie clip is NOT visible THEN tell user "Sorry, that movie clip is unavailable! " 

NOTE

The symbol for OR is two vertical lines. On most keyboards, you produce the vertical line by using the Shift key plus the backslash ( \ ) key, at the far right of the QWERTY row.


The preceding four examples might look like this in ActionScript:

Example #1:

 if ((start == "San Francisco") && (end == "Berkeley") ) {      wayToGo = "BART"; } 

Example #2:

 if ( (input == cancel)  (input == error) ) {      cancelOperation(); } 

Example #3:

 if ( (checkPassword() ) && (!checkKillList() ) {      grantAccess(); } 

Example #4:

 if ( (!myClip)  (!myClip._visible) ) {      returnError(unavailable) } 

You can always duplicate the logic of logical operators by using multiple if statements. For instance, the AND operator is the equivalent of two nested if statements. Examples #1 and #3 look like this converted into nested if statements:

 if (start == "San Francisco") {      if (end == "Berkeley") {           wayToGo = "BART";      } } if (checkPassword()) {      if (!checkKillList()) {           grantAccess();      } } 

Mostly, programmers think of the choice between AND and nested if statements as just one of taste. Some programmers find nested if statements more readable. The AND operator is more concise .

You can replace the OR operator with an if-else-if statement. For example, Example #2 looks like this converted into an if-else-if statement:

 if (input == cancel) {      cancelOperation(); } else if (input == error) {      cancelOperation(); } 

In this case, the if-else-if statement has nothing to recommend it. The OR operator is both more concise and more readable.

Actually, if statements do not exactly replicate the functionality of the AND or OR operator. The if statements return Boolean values, true or false . The AND and OR operators, on the other hand, actually return one of their operands. The AND operator returns its second operand if both operands resolve to true ; otherwise , it returns whichever operand resolves to false . The OR operator returns its first operand, if it resolves to true : otherwise, it returns its second operand. The AND and OR operators are usually used in an if statement, and the if statement converts the returned operand to a Boolean.

Table A.8 gives some examples of OR statements, their return values as displayed by a trace() statement, and to which Boolean the return value resolves.

Note that what is actually returned by the OR statement in each example is identical to one side of the OR statement. For instance, in the statement ["one", "two"] "hi" , the array ["one", "two"] becomes true when converted to a Boolean; therefore, the OR statement returns the array, which a trace() statement displays as one, two .

In practice, the values returned by AND and OR statements are seldom utilized directly. They're just converted into Booleans by if statements.

Table A.8. OR Statements, Their Returns, and Boolean Equivalents

OR Statement

Trace (Return)

Boolean Equivalent of Return

"ho" "hi"

hi

false

"32" "hi"

32

true

32 "hi"

32

true

Infinity "hi"

Infinity

true

["one", "two"] "hi"

one, two

true

myClip "hi"

_level0.myClip

true

{eyes : "green", age : 32} "hi"

[object Object]

true




Using Macromedia Studio MX 2004
Special Edition Using Macromedia Studio MX 2004
ISBN: 0789730421
EAN: 2147483647
Year: N/A
Pages: 339

flylib.com © 2008-2017.
If you may any questions please contact us: flylib@qtcs.net