++ increment operator

++expression
expression++

A pre-increment and post-increment unary operator that adds 1 to expression . The expression can be a variable, element in an array, or property of an object. The pre-increment form of the operator (++expression) adds 1 to expression and returns the result. The post-increment form of the operator (expression++) adds 1 to expression and returns the initial value of expression (the value prior to the addition).

The pre-increment form of the operator increments x to 2 (x + 1 = 2) and returns the result as y:

var x:Number = 1; 
var y:Number = ++x; 
trace("x:"+x); //traces x:2 
trace("y:"+y); //traces y:2

The post-increment form of the operator increments x to 2 (x + 1 = 2) and returns the original value of x as the result y:

var x:Number = 1; 
var y:Number = x++; 
trace("x:"+x); //traces x:2 
trace("y:"+y); //traces y:1

Availability: ActionScript 1.0; Flash Player 4

Operands

expression : Number - A number or a variable that evaluates to a number.

Returns

Number - The result of the increment.

Example

The following example uses ++ as a post-increment operator to make a while loop run five times:

var i:Number = 0; 
while (i++ < 5) { 
 trace("this is execution " + i); 
} 
/* output: 
 this is execution 1 
 this is execution 2 
 this is execution 3 
 this is execution 4 
 this is execution 5 
*/

The following example uses ++ as a pre-increment operator:

var a:Array = new Array(); 
var i:Number = 0; 
while (i < 10) { 
 a.push(++i); 
} 
trace(a.toString()); //traces: 1,2,3,4,5,6,7,8,9,10 

This example also uses ++ as a pre-increment operator.

var a:Array = []; 
for (var i = 1; i <= 10; ++i) { 
 a.push(i); 
} 
trace(a.toString()); //traces: 1,2,3,4,5,6,7,8,9,10 

This script shows the following result in the Output panel: 1,2,3,4,5,6,7,8,9,10 The following example uses ++ as a post-increment operator in a while loop:

// using a while loop 
var a:Array = new Array(); 
var i:Number = 0; 
while (i < 10) { 
 a.push(i++); 
} 
trace(a.toString()); //traces 0,1,2,3,4,5,6,7,8,9 

The following example uses ++ as a post-increment operator in a for loop:

// using a for loop 
var a:Array = new Array(); 
for (var i = 0; i < 10; i++) { 
 a.push(i); 
} 
trace(a.toString()); //traces 0,1,2,3,4,5,6,7,8,9 

This script displays the following result in the Output panel:

0,1,2,3,4,5,6,7,8,9 

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/00001279.html