Wednesday, January 9, 2013

Operator precedence and associativity

Operator precedence and associativity determine the order in which operators are processed. Although it may seem natural to those familiar with arithmetic 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. ActionScript defines a default operator precedence that you can alter using the parentheses (()) operator. For example, the following code alters the default precedence in the previous example to force the compiler to process the addition (+) operator before the multiplication (*) operator.
var sumNumber:uint = (2 + 3) * 4; // uint == 20
You may encounter situations in which two or more operators of the same precedence appear in the same expression. In these cases, the compiler uses the rules of associativity to determine which operator to process first. All of the binary operators, except the assignment operators, are left-associative, which means that operators on the left are processed before operators on the right. The assignment operators and the conditional (?:) operator, are right-associative, which means that the operators on the right are processed before operators on the left. 
For example, consider the less than (<) and greater than (>) operators, which have the same precedence. If both operators are used in the same expression, the operator on the left is processed first because both operators are left-associative. This means that the following two statements produce the same output:
trace (3 > 2 < 1); // output: false
trace ((3 > 2) < 1); // output: false
The greater than (>) operator is processed first, which results in a value of true because the operand 3 is greater than the operand 2. The value true is then passed to the less than (<) operator, along with the operand 1. The following code represents this intermediate state:
trace ((true) < 1);
The less than (<) operator converts the value true to the numeric value 1 and compares that numeric value to the second operand 1 to return the value false ( the value 1 is not less than 1).
trace (1 < 1); // Output: false
You can alter the default left associativity with the parentheses (()) operator. You can instruct the compiler to process the less than (<) operator first by enclosing that operator and its operands in parentheses. The following example uses the parentheses (()) operator to produce a different output using the same numbers from the previous example.
trace (3 > (2 < 1)); // output: true
The less than (<) operator is processed first, which results in a value of false because the operand 2 is not less than the operand 1. The value false is then passed to the greater than (>) operator, along with the operand 3. The following code represents this intermediate state:
trace (3 > (false));
The greater than (>) operator converts the value false to the numeric value 0 and compares that numeric value to the other operand 3 to return true (the value 3 is greater than 0).
trace (3 > 0); // Output: true
The following table lists the operators for ActionScript 3.0 in order of decreasing precedence. Each row of the table contains operators of the same precedence. An operator has higher precedence than an operator appearing below it in the table.


Group Operator
Primary [] {x:y} () f(x) new x.y x[y] <></> @ :: ..
Postfix x++ x--
Unary ++x --x + - ! T(x) typeof void
Multiplicative * / %
MAdditive + -
Shift << >> >>>
Relational < > <= >= instanceof in is
Equality == != === !==
Bitwise AND &
Bitwise XOR ^
Bitwise OR |
Logical AND &&
Logical OR ||
Conditional ?:
Assignment = *= /= %= += -= <<= >>= >>>= &= ^= |=
Comma ,
Programming Actionscript 3. Powered by Blogger.