Logical Operators


The logical operators go hand in hand with conditional statements and loops because they allow you to evaluate more complex conditionals in a single expression. For example, if I asked you to write a script that would print out a message if myNumber was between 20 and 30 using only what we have discussed so far, you would have to first use an if statement to see if myNumber was greater than 20; then you would nest another if within that to test whether myNumber was less than 30. Logical operators allow us to do both tests in a single conditional statement.

A convenient tool for understanding logical relations is called a Truth Table (TT). It maps all possible values of the operands to their corresponding compound result as it pertains to each operator. This might not make sense yet, but as soon as you see the first TT, it will be clear.

The AND Operator (&&)

If we have two expressions that evaluate to true or false and we put them together separated by the && operator, the entire compound expression will evaluate to true only if the first two expressions both evaluate to true . Consider this example:

 trace( true && true ); trace( true && false ); trace( false && true ); trace( false && false ); 

You'll notice that the output reads true , false , false , false because in only the first line do both expressions evaluate to true . We can use a similar expression to solve the problem posed to you earlier. You should now be able to construct the following statement to solve the problem of myNumber being between 20 and 30:

 if( myNumber >= 20 && myNumber <= 30 ){ trace("in range");} 

The TT for the && operator is shown in Table 2.2.

 
Table 2.2: TT for Logical AND (&&)

x

y

(x && y)

false

false

false

false

true

false

true

false

false

true

true

true

Tip  

You can combine more than one logical operator in the same expression. Nothing is stopping you from stringing 10 separate expressions together, all separated by && operators. This isn't a good idea, however; the code would be hard to read and modify.

The OR Operator ()

The operator is quite similar to the && operator, except that the expression is true if either of the expressions evaluates to true . This is often useful for testing whether any of a variety of conditions is true . Let's see an example:

 trace( true  true ); trace( true  false ); trace( false  true ); trace( false  false ); 

This time the output reads true , true , true , false because in the first three lines, one of the two expressions is true . Only in the last case are both expressions false .

The TT for the operator is shown in Table 2.3.

 
Table 2.3: TT for Logical OR ()

x

y

(x y)

false

false

false

false

true

true

true

false

true

true

true

true

Logical Operator Precedence

Not only can you combine multiple expressions with logical operators, but you can also combine multiple compound statements with logical operators. It's often convenient to use both && operators and operators in the same conditional statement. This brings up the precedence order between logical operators. There are three possible scenarios here:

  • The && and operators have the same precedence, in which case they will be evaluated left to right.

  • && has higher precedence, so all && will be evaluated first.

  • has higher precedence, so all will be evaluated first.

In fact, the second case is true . In ActionScript, the && operator has a higher precedence than . Using this knowledge, try to figure out what the output of the following script will be:

 trace( true && false  true && true ); trace( true  false && true  false ); trace( true && true && true  false ); 

The output is actually true , true , true . Remember that when you have more than one operator of the same precedence or the same type, they are evaluated left to right. As we get into the games , you'll see complex conditional statements with several logical operators popping up all over the place. The more time you spend with them, the easier it will be to immediately realize what the expression is doing and more importantly, how to construct your own complex conditional statements.

Caution  

It is common programming practice to use parentheses to indicate the logical operator precedence. In other words, even though you know the && operator is evaluated first, you can always set the && and apart with parentheses just to make the script more readable. This way you don't have to remember which has higher precedence; you can just use parentheses to force the order of your choice.

Using NOT (!) and the Logical Operators

You will often see logical operators in combination with the ! operator. There are often several ways to construct a compound conditional, and it's often much more intuitive to use a ! sign to get the behavior you desire . Remember: The ! operator changes true to false and false to true . This is expressed in Table 2.4.

 
Table 2.4: TT for Logical NOT (!)

x

( !x )

false

true

true

false

For example, if you wanted to set up an if statement that would execute only if x were not equal to 50, you would have several choices, including each of the following equivalent statements:

 if( x<50  x>50 ){trace("hi");} if( x==50 ){} else{trace("hi");} 

Both of these equivalent statements will work, but a more elegant and intuitive solution follows :

 if( x != 50 ){trace("not 50");} 

This solution not only uses less script, but it's also more readable. This is the foundation of using the NOT operator with logical operators. Consider the following:

 if( x!= 50 && y < 12 && z != 12.3 ){} 

Statements like this are common, and you should get used to seeing ! operators in your conditionals. Finally, let's consider the following two equivalent statements:

 if( x && y ){} if( !(!x  !y) ){} 

As I said before, these two statements are equivalent, although they might not appear to be at first glance. If you are confused by this example, try building a TT for both lines and then compare them. Any two statements that have an identical TT are equivalent statements. The actual tables are given to you in Table 2.5.

 
Table 2.5: TT for (! ( !x !y ))

x

y

!x

!y

( !x !y )

!( !x !y )

false

false

true

true

true

false

false

true

true

false

true

true

true

false

false

true

true

true

true

true

false

false

false

true

Logical Short Circuit

One final trick with regards to logical operators is a feature called short circuiting. Consider this: A compound expression involving a logical && operator evaluates to true only if both expressions are true . Therefore, if the first expression evaluates to false , the entire thing will evaluate to false no matter what the second expression evaluates to. What reason is there to even evaluate the second expression? In fact, there is no reason; ActionScript does not evaluate it. This is called short circuiting a logical expression. Consider the following code:

 myNumber = 10; false && myNumber++; trace(myNumber); 

The output of this script is 10 because myNumber is never incremented. The logical expression short circuits when it evaluates false on the left side of the && operator.

Note  

When a number variable is evaluated as a boolean, 0 evaluates to false , and anything else ( non-zero ) evaluates to true .

The same thing happens with the operator, except that a short circuits when the first expression evaluates to true . When a statement has either part being true , the entire expression evaluates to true , so this short circuit makes sense. We look at a similar example for comprehensiveness:

 myNumber = 10; true  myNumber++; trace(myNumber); 

Again, the output is 10 because myNumber never gets a chance to increment due to a short circuit of the logical or operator.




Macromedia Flash MX 2004 Game Programming
Macromedia Flash MX 2004 Game Programming (Premier Press Game Development)
ISBN: 1592000363
EAN: 2147483647
Year: 2004
Pages: 161

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