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.




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