, comma operator

(expression1 , expression2 [, expressionN... ])

Evaluates expression1, then expression2, and so on. This operator is primarily used with the for loop statement and is often used with the parentheses () operator.

Availability: ActionScript 1.0; Flash Player 4

Operands

expression1 : Number - An expression to be evaluated.

expression2 : Number - An expression to be evaluated.

expressionN : Number - Any number of additional expressions to be evaluated.

Returns

Object - The value of expression1, expression2, and so on.

Example

The following example uses the comma (,) operator in a for loop:

for (i = 0, j = 0; i < 3 && j < 3; i++, j+=2) { 
 trace("i = " + i + ", j = " + j); 
} 
// Output: 
// i = 0, j = 0 
// i = 1, j = 2

The following example uses the comma (,) operator without the parentheses () operator to show that the comma operator is of lower precedence than assignment (=) operator:

var v:Number = 0; 
v = 4, 5, 6; 
trace(v); // output: 4 

The following example uses the comma (,) operator with the parentheses () operator and illustrates that the comma operator returns the value of the last expression:

var v:Number = 0; 
v = (4, 5, 6); 
trace(v); // output: 6 

The following example uses the comma (,) operator without the parentheses () operator and illustrates that the comma operator sequentially evaluates all of the expressions. The first expression, v + 4 is assigned to the variable v because the assignment (=) operator is of higher precedence than the comma operator. The second expression, z++, is evaluated and z is incremented by one.

var v:Number = 0; 
var z:Number = 0; 
v = v + 4 , z++, v + 6; 
trace(v); // output: 4 
trace(z); // output: 1 

The following example is identical to the previous example except for the addition of the parentheses () operator, which changes the order of operations such that the comma operator is evaluated before the assignment (=) operator:

var v:Number = 0; 
var z:Number = 0; 
v = (v + 4, z++, v + 6); 
trace(v); // output: 6 
trace(z); // output: 1 

See also

() parentheses operator


Flash CS3


 

Send me an e-mail when comments are added to this page | Comment Report

Current page: http://livedocs.adobe.com/flash/9.0/main/00001266.html