Statements and Operators: Chapter 10 - Essential ActionScript
by David Stiller, Rich Shupe, Darren Richardson, Jen deHaanThis excerpt is from Essential ActionScript. More than two years in the making, ActionScript 3.0 presents perhaps the most substantial upgrade to Flash's programming language ever. The enhancements to ActionScript's performance, feature set, ease of use, cleanliness, and sophistication are simply staggering. Revolutionary improvements abound. Essential ActionScript 3.0 is an update to Essential ActionScript 2.0, once again focusing on the core language and object-oriented programming with some coverage of the Flash Player API. Approximately half of the book focuses on the new features and functionality of ActionScript 3.0, while the rest focuses on changes between the 2 and 3 releases.
Table of Contents
This chapter provides a reference-style overview of ActionScript's statements and operators—many of which we've already seen in this book. Rather than discussing each statement and operator in isolation, this book teaches the use of statements and operators in the context of other programming topics. Accordingly, this chapter lists many crossreferences to discussion and usage examples found elsewhere in this book. For information on operators not covered in this book, see Adobe's ActionScript Language Reference.
Statements are one kind of directive, or basic program instruction, consisting of a keyword (command name reserved for use by the ActionScript language) and, typically, a supporting expression.
Table 10-1, “ActionScript statements” lists ActionScript's statements, their syntax, and purpose.
Table 10-1. ActionScript statements
An operator is a symbol
or keyword that manipulates, combines, or transforms data. For
example, the following code uses the multiplication operator
(*) to multiply 5 times 6:
5 * 6;
Though each operator has its own specialized task, all operators share a number of general characteristics. Before we consider the operators individually, let's see how they behave generally.
Operators perform actions using the data values (operands) supplied. For example, in the
operation 5 * 6, the numbers 5 and 6
are the operands of the
multiplication operator (*).
Operations can be combined to form complex expressions. For example:
((width * height) - (Math.PI * radius * radius)) / 2
When expressions become very large, consider using variables to hold interim results for both convenience and clarity. Remember to name your variables descriptively. For example, the following code has the same result as the preceding expression but is much easier to read:
var radius:int = 10; var height:int = 25; var circleArea:Number = (Math.PI * radius * radius); var cylinderVolume:Number = circleArea * height;
Operators are sometimes categorized according to how many operands they take (i.e., require or operate on). Some ActionScript operators take one operand, some take two, and one even takes three:
-x // One operand x * y // Two operands (x == y) ? "true result" : "false result" // Three operands
Single-operand operators are called unary operators; operators that take two operands are called binary operators; operators that take three operands are called ternary operators. For our purposes, we'll look at operators according to what they do, not the number of operands they take.
Operators' precedence determines which operation is performed first in an expression with multiple operators. For example, when multiplication and addition occur in the same expression, multiplication is performed first:
4 + 5 * 6 // Yields 34, because 4 + 30 = 34
The expression 4 + 5 * 6 is
evaluated as "4 plus the product of
5 * 6" because the * operator has higher precedence than the
+ operator.
Similarly, when the less-than (<) and concatenation (+) operators occur in the same expression,
concatenation is performed first. For example, suppose we want to
compare two strings and then display the result of that comparison
during debugging. Without knowing the precedence of the
< and + operators, we might mistakenly use the following
code:
trace("result: " + "a" < "b");
Due precedence of the < and
+ operators, the preceding code yields
the value:
false
whereas we were expecting it to yield:
result: true
To determine the result of the expression "result: " + "a" < "b", ActionScript performs
the concatenation operation first (because + has a higher precedence than <). The result of concatenating "result:" with "a" is
a new string, "result: a".
ActionScript then compares that new string with "b", which yields false because the first character in "result: a" is alphabetically greater than
"b".
When in doubt, or to ensure a different order of operation, use parentheses, which have the highest precedence:
"result: " + ("a" < "b") // Yields: "result: true"
(4 + 5) * 6 // Yields 54, because 9 * 6 = 54
Even if not strictly necessary, parentheses can make a complicated expression more readable. The expression:
x > y || y == z // x is greater than y, or y equals z
may be difficult to comprehend without consulting a precedence table. It's a lot easier to read with parentheses added:
(x > y) || (y == z) // Much better!
The precedence of each operator is listed later in Table 10-2, “ActionScript operators”.
As we've just seen, operator precedence indicates the pecking order of operators: those with a higher precedence are executed before those with a lower precedence. But what happens when multiple operators occur together and have the same level of precedence? In such a case, we apply the rules of operator associativity, which indicate the direction of an operation. Operators are either left-associative (performed left to right) or right-associative (performed right to left). For example, consider this expression:
b * c / d
The * and / operators are left-associative, so the
* operation on the left (b * c) is performed first. The preceding example
is equivalent to:
(b * c) / d
In contrast, the = (assignment)
operator is right-associative, so the expression:
a = b = c = d
says, "assign d to c, then assign c to
b, then assign b to a," as in:
a = (b = (c = d))
Unary operators are right-associative; binary operators are
left-associative, except for the assignment operators, which are
right-associative. The conditional operator (?:) is also right-associative. Operator
associativity is fairly intuitive, but if you're getting an
unexpected value from a complex expression, add extra parentheses
to force the desired order of operations. For further information
on operator associativity in ActionScript, see Adobe's
documentation.
The operands of most operators are typed. In strict mode, if a value used for an operand does not match that operand's datatype, the compiler generates a compile-time error and refuses to compile the code. In standard mode, the code compiles, and at runtime, if the operand's type is a primitive type, ActionScript converts the value to the operand's datatype (according to the rules described in Chapter 8, Datatypes and Type Checking, in the section "the section called “Conversion to Primitive Types”"). If the operand's type is not a primitive type, ActionScript generates a runtime error.
For example, in strict mode, the following code causes a type
mismatch error because the datatype of the division (/) operator's operands is Number, and the value "50" does not belong to the Number datatype:
"50" / 10
In standard mode, the preceding code does not cause a
compile-time error. Instead, at runtime, ActionScript converts the
String value "50" to the Number
datatype, yielding 50, and the entire expression has the value
5.
To compile the preceding code without causing an error in strict
mode, we must cast the String value
to the required datatype, as follows:
Number("50") / 10
Some operators' operands are untyped, which allows the result of
the operation to be determined at runtime based on the datatypes of
the supplied values. The + operator,
for example, performs addition when used with two numeric operands,
but it performs concatenation when either operand is a string.
The datatypes of each operator's operands are listed in Adobe's ActionScript Language Reference.
Table 10-2, “ActionScript operators” lists ActionScript's operators, their precedence value, a brief description, and a typical example of their use. Operators with the highest precedence (at the top of the table) are executed first. Operators with the same precedence are performed in the order they appear in the expression, usually from left to right, unless the associativity is right to left.
Note that with the exception of the E4X operators, this book does not provide exhaustive reference information for ActionScript's operators. For details on a specific operator, consult Adobe's ActionScript Language Reference. For information on the bitwise operators, see the article "Using Bitwise Operators in ActionScript" at http://www.moock.org/asdg/technotes/bitwise.
Table 10-2. ActionScript operators
