Flylib.com

Books Software

 
 
 

Operator Precedence


Operator Precedence

As you have just seen, several operators can be strung together into one statement. But as you can also see, the order in which they are evaluated is not simply left to right. That is because of operator precedence , which refers to the order in which operators are evaluated when more than one appears in the same statement. As you might have gathered from the previous example, the multiplication operator has a higher precedence than the addition operator. That's why, regardless of the order we type them in, the multiplication is always executed first. And in a similar way, the assignment operator has lower precedence than either multiplication or division. Therefore, the result is not assigned until all the other operators have been evaluated. If you want to force arithmetic operations into a certain order, you can use parentheses. The following example demonstrates the use of parentheses, although they should be familiar to you from algebra:

result1 = (2 + 2) * 2;

As you might expect, the parentheses force the addition to execute first. The result of that addition is then multiplied by 2. Finally, a result of 8 is assigned to the variable result1 .

Table 2.1 lists all the operators in Flash from highest to lowest precedence.



More Operators

We can't take the time to talk about every operator available in ActionScript right now. Instead I want to look at some of the more common ones here and save the more obscure operators for later when our games have a specific use for them. Let's first consider the preincrement and post-increment operators.

Pre-Increment and Post-Increment (++)

Consider the following example:

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

The output of this script, shown in Figure 2.11, demonstrates the use of the pre- and post-increment operators. These operators add 1 to the variable they operate on. The only difference between putting the ++ before and putting it after has to do with when the variable is incremented.

click to expand
Figure 2.11: The pre- and post-increment operators both add 1 to a variable ”the difference is when it happens.

When a variable is used in an expression along with the pre-increment operator (the operator goes before the variable), the variable is incremented; afterward, its value is plugged into the rest of the expression. On the other hand, when the post-increment operator is used (the operator goes after the variable), the expression is evaluated with the original (non-incremented) value; afterward, the variable is incremented.

Pre-Decrement and Post-Decrement (--)

Let's have another example to further demonstrate the pre- and post-operations. This time, we'll use the decrement operator, which subtracts 1 from the variable. In terms of pre and post, the decrement operator works identically to the increment operator:

myNumber = 10;
result = --myNumber * 2;
trace(result);
myNumber = 10;
result = myNumber-- * 2;
trace(result);

This output of this script, revealed in Figure 2.12, shows the first result to be 18 and the second result to be 20. That's because the first result used the pre-decrement operator, so the myNumber variable was first reduced from 10 to 9 and then multiplied by 2 for a result of 18. In the second case, the post-decrement operator was used, which caused the multiplication to be evaluated first while the value of myNumber was still at 10; afterward, myNumber was decremented down to 9.

click to expand
Figure 2.12: The pre- and post-decrement operators work identically to increment except that they subtract 1 instead of adding it.

Modulus (%)

The modulus operator, which is invoked by using the percent symbol, is an interesting little operator that has more uses than you might expect at first glace. In essence, modulus gives you the remainder after integer division. In other words, the operand before modulus is divided by the operand after modulus, and the remainder is what ends up coming out of the expression. An example will make things clearer:

trace( 5 % 2 );
trace( 10 % 2 );
trace( 10 % 3 );
trace( 1 % 5 );
trace( 7 % 0 );

The script, whose output can be seen in Figure 2.13, outputs the following values: 1 , , 3 , 1 , NaN . Let's consider each line one at a time. The first line shows 5 modulus 2. The result of 5 / 2 is 2 with a remainder of 1, so 1 is the output. The second line shows 10 modulus 2.

click to expand
Figure 2.13: The modulus operator gives the remainder after integer division.

Because 10 can be divided by 2 a total of 5 times with no remainder, the output is . The third line shows 10 modulus 3. When 3 is divided into 10, it can go 3 times with a remainder of 1, so the output is 1 . On line 4, the script says 1 modulus 5. Because 5 cannot be divided into 1, the remainder is 1, and so is the output. Finally, in line 5, the expression reads 7 modulus 0, but modulus of 0 is undefined in mathematics, so the expression cannot properly evaluate. As a result, the output is NaN , which stands for Not a Number. NaN is Flash's way of telling you that the expression cannot be evaluated as a real number.

Note  

If you assign an expression that evaluates as NaN to a variable, the variable receives a value of NaN . You can test to see whether a variable has been assigned this value using the isNaN function. (Functions are described in detail in Chapter 3.)

Compound Assignments (+=, -=, *=, /=, %=)

Many of your scripts will involve evaluating expressions and assigning the result to a variable. While doing this, you will notice that a great deal of the time, you are using the same variable in the expression that you are assigning to. For example, if you had a variable and you wanted to add 100 to it, you could do something like the following:

myNumber = myNumber + 100;

In ActionScript, however, you could write that expression in a more efficient way that involves the use of what is called a compound assignment . Consider the following alternative statement:

myNumber += 100;

Notice that the operator has changed from a simple equal sign to a plus-equal sign. The results of both lines of script are identical; the only difference is that the later requires fewer keystrokes. Many programmers find that the compound assignment is easier to read as well.

Compound assignment operators are available for addition, subtraction, multiplication, division, modulus, and several bitwise operations.

Note  

Bitwise operations are special operations that can manipulate a variable on a bit-by-bit basis. These operations can be faster to execute and can sometimes allow elegant solutions to somewhat complex problems. But it is rare to have a situation that requires them. Consult the ActionScript dictionary for more information on the bitwise operators.