EasyManua.ls Logo

MACROMEDIA FLASH 8-LEARNING ACTIONSCRIPT 2.0 IN FLASH - Page 179

MACROMEDIA FLASH 8-LEARNING ACTIONSCRIPT 2.0 IN FLASH
830 pages
Print Icon
To Next Page IconTo Next Page
To Next Page IconTo Next Page
To Previous Page IconTo Previous Page
To Previous Page IconTo Previous Page
Loading...
About operators 179
You can manipulate the value of a variable using operators while a condition is true. For
example, you can use the increment operator (
++) to increment the variable i while the
condition is true. In the following code, the condition is
true while i is less than the value of
10. While that is true, you increment
i one number higher using i++.
var i:Number;
for (i = 1; i < 10; i++) {
trace(i);
}
The Output panel displays the numbers 1 through 9, which is i incrementing in value until it
reaches the end condition (
i is equal to 10), when it stops. The last value displayed is 9.
Therefore, the value of
i is 1 when the SWF file starts playing, and 9 after the trace completes.
For more information on conditions and loops, see About statements” on page 141.
About operator precedence and associativity
When you use two or more operators in a statement, some operators take precedence over
other operators. Operator precedence and associativity determine the order in which
operators are processed. ActionScript has a hierarchy that determines which operators execute
before others. There is a table that outlines this hierarchy at the end of this section.
Although it may seem natural to those familiar with arithmetic or basic programming that the
compiler processes the multiplication (
*) operator before the addition (+) operator, the
compiler needs explicit instructions about which operators to process first. Such instructions
are collectively referred to as operator precedence.
You can see an example of operator precedence when you work with the multiplication and
addition operators:
var mySum:Number;
mySum = 2 + 4 * 3;
trace(mySum); // 14
You see the output of this statement is 14, because multiplication has a higher operator
precedence. Therefore, 4 * 3 is evaluated first and the result is added to 2.
You can control what happens by enclosing expressions in parentheses. ActionScript defines a
default operator precedence that you can alter using the parentheses (
()) operator. When you
put parentheses around the addition expression, ActionScript performs the addition first:
var mySum:Number;
mySum = (2 + 4) * 3;
trace(mySum); // 18
Now the output of this statement is 18.

Table of Contents

Related product manuals