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)
Summary of Contents for FLASH MX 2004 - ACTIONSCRIPT
Page 1: ...ActionScript Reference Guide...
Page 8: ...8 Contents...
Page 12: ......
Page 24: ...24 Chapter 1 What s New in Flash MX 2004 ActionScript...
Page 54: ...54 Chapter 2 ActionScript Basics...
Page 80: ...80 Chapter 3 Writing and Debugging Scripts...
Page 82: ......
Page 110: ...110 Chapter 5 Creating Interaction with ActionScript...
Page 112: ......
Page 120: ...120 Chapter 6 Using the Built In Classes...
Page 176: ......
Page 192: ...192 Chapter 10 Working with External Data...
Page 202: ...202 Chapter 11 Working with External Media...
Page 204: ......
Page 782: ...782 Chapter 12 ActionScript Dictionary...
Page 793: ...Other keys 793 221 222 Key Key code...
Page 794: ...794 Appendix C Keyboard Keys and Key Code Values...
Page 798: ...798 Appendix D Writing Scripts for Earlier Versions of Flash Player...
Page 806: ...806 Appendix E Object Oriented Programming with ActionScript 1...
Page 816: ...816 Index...