Using operators to manipulate values in expressions 45
Using operators to manipulate values in expressions
An expression is any statement that Flash can evaluate and that returns a value. You can create an
expression by combining operators and values or by calling a function.
Operators are characters that specify how to combine, compare, or modify the values of an
expression. The elements that the operator performs on are called operands. For example, in the
following statement, the
+ operator adds the value of a numeric literal to the value of the variable
foo; foo and 3 are the operands:
foo + 3
This section describes general rules about common types of operators, operator precedence, and
operator associativity. For detailed information on each operator mentioned here, as well as
special operators that don’t fall into these categories, see the entries in Chapter 12, “ActionScript
Dictionary,” on page 205.
Operator precedence and associativity
When two or more operators are used in the same statement, some operators take precedence over
others. ActionScript follows a precise hierarchy to determine which operators to execute first. For
example, multiplication is always performed before addition; however, items in parentheses take
precedence over multiplication. So, without parentheses, ActionScript performs the
multiplication in the following example first:
total = 2 + 4 * 3;
The result is 14.
But when parentheses surround the addition operation, ActionScript performs the addition first:
total = (2 + 4) * 3;
The result is 18.
When two or more operators share the same precedence, their associativity determines the order
in which they are performed. Associativity can be either left-to-right or right-to-left. For example,
the multiplication operator has an associativity of left-to-right; therefore, the following two
statements are equivalent:
total = 2 * 3 * 4;
total = (2 * 3) * 4;
For a table of all operators and their precedence and associativity, see Appendix B, “Operator
Precedence and Associativity,” on page 787.
Numeric operators
Numeric operators add, subtract, multiply, divide, and perform other arithmetic operations.
The most common usage of the increment operator is
i++ instead of the more verbose i = i+1.
You can use the increment operator before or after an operand. In the following example,
age is
incremented first and then tested against the number 30:
if (++age >= 30)
In the following example, age is incremented after the test is performed:
if (age++ >= 30)