Even More Operators


So far, the operators you have seen resolve to either a number or a string. But in fact, that leaves out several important operators. The missing ones resolve to a boolean value instead. This becomes important when we start talking about conditionals and loops , but before we get to those, let's take a look at some of the more important operators that resolve to a boolean value.

Greater Than (>, >=)

Here's another throwback to high school algebra. The greater than operator works as follows : One operand is placed before the greater than symbol, and one is placed after it. If the first operand has a higher value than the second, the expression evaluates to true . If the first operand does not have a higher value, the expression evaluates to false . As usual, an example can help illustrate the point:

 trace( 10 > 50 );     //line 1 trace( 5 > 3 );     //line 2 trace( 2 > 2 );   //line 3 

If you test the output, you'll see that it reads false , true , false . That's because 10 is not greater than 50, 5 is greater than 3, and 2 is not greater than 2.

The greater than or equal to operator works identically to the greater than operator. The only exception is that if the first operand is equal to the second operand, the expression will be true . This should be clear, but let's have another example just to make sure. We'll use the same script as the prior example, but we'll change the operator to greater than or equal to:

 trace( 10 >= 50 );    //line 1 trace( 5 >= 3 );      //line 2 trace( 2 >= 2 );      //line 3 

The output will now read false , true , true .

Less Than (<, <=)

These two operators work identically to the greater than operators except that the operands are reversed . In other words, when we're using the less than operator, if the first operand has a lower value than the second, the expression will evaluate to true ; otherwise , it will evaluate to false . Here's an example just to make sure everything is clear:

 trace( 10 < 50 );     //line 1 trace( 5 < 3 );       //line 2 trace( 2 < 2 );       //line 3 trace( 10 <= 50 );    //line 4 trace( 5 <= 3 );      //line 5 trace( 2 <= 2 );      //line 6 

The output will read true , false , false , true , false , true .

Equivalence (==)

This operator is similar to greater than except that the expression will only evaluate as true if both operands have the same value. One important thing to keep in mind is that the equivalence operator is a double equal symbol. Remember that the single equal symbol is used for assignment. Because of that, the equivalence operator is forced to be something different, and in the case of C++, Java, and ActionScript, the chosen operator is a double equal symbol. Yes, you guessed it. Here's an example:

 trace( 10 == 4 ); trace( 0 == 0 ); trace( 8 == 8 ); trace( "Hello" == "GoodBye" ); trace( "Eggplant" == "Eggplant" ); 

The output, as you might expect, reads false , true , true , false , true . As you can see, equivalence works with strings as well as numbers . In fact, the greater than and less than operators work on strings as well, but it's quite rare to need this feature, so I left it out of these examples.

Caution  

You will find yourself accidentally substituting an assignment operator when you need an equivalence operator. This is a common mistake, but it can cause some unexpected results when you are testing your movie. Make sure to keep your eyes open for this mistake when you are looking for bugs in your script. This is exactly the kind of logical error I was talking about in Chapter 1, "The Flash Authoring Tool"; it doesn't show up in the Output panel.

Not Equal To (!=)

The not equal to operator works exactly opposite to the equivalence operator. If the two operands are equal, the statement will be false ; otherwise, it will be true . Let's see an example of the not equal to operator:

 trace( 5 != 10 );                     //line 1 trace( "Hello" != "GoodBye" );       //line 2 trace( "Eggplant" != "Eggplant" );   //line 53 

The output reads true , true , false . All three lines use the not equal to operator. It resolves to be true when the operands are not equivalent and false when they are.

The NOT Operator (!)

The NOT operator is used to toggle the value of any boolean variable, literal, or expression. If you have a boolean that you want to change the value of, you can flip it from true to false or false to true by applying the NOT operator before the variable. Consider the following script:

 trace( !true );      //line 1 "not true" trace( !false );     //line 2 "not false" trace( ! (10 > 5) ); //line 3 "not (10 less than 5)" trace( ! (10 < 5) ); //line 4 "not (10 greater than 5)" 

In lines 1 and 2, the boolean literals are simply toggled with the NOT operator. In lines 3 and 4, the result of some expression is toggled with the NOT operator. Line 3 first evaluates 10 greater than 5 as true ; then it toggles before outputting false . Line 4, in contrast, evaluates 10 less than 5 as false ; then it toggles before outputting true .




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