The Comma Operator

     

The comma operator is used primarily to declare two or more variables in a single statement, as in this example:

 var x=1, y=2, z=3; 

The preceding is equivalent to

 var x=1; var y=2; var z=3; 

This operator can come in handy when you want to initialize two or more index variables in a for loop, as in this example:

 for (i = 0, j = 0,  k = 0; i < 50;  i++, j -= i, k--) {      trace(i+" "+j+" "+k); } 

The output is as follows :

 0 0 -10 1 -1 -11 2 -3 -12 3 -6 -13 . . . 

The return value of the comma operator, which is the resolved value of the final operand, isn't used in either of the preceding cases. In general, the return value of the comma operator is not useful. For instance, it is useless (though not illegal) to have several comma-separated terms in the middle (test) portion when you're setting up a for loop because only the final term would be evaluated. For instance, the following does exactly the same thing as the previous for loop because j >= k, k < i is ignored and only i < 50 is tested on each loop:

 for (i = 0, j = 0,  k = -10; j >= k, k < i, i < 50;  i++, j -= i, k--) {      trace(i+" "+j+" "+k); } 



Using Macromedia Studio MX 2004
Special Edition Using Macromedia Studio MX 2004
ISBN: 0789730421
EAN: 2147483647
Year: N/A
Pages: 339

flylib.com © 2008-2017.
If you may any questions please contact us: flylib@qtcs.net