Programming Habits: Add increment variation

Especially when building loops, you will need to add an increment to an existing operand or Number variable. There is a logical way to do this and a shorthand alternative using the "++" operator.

This:

i=i+1;

is the same as this:

i++;

AS3 Loops: Simple "while" loop

var i:Number= 0;

while (i < 5) {
trace([i]);
i = i+1;
}

AS3 Loops: Simple "for" loop

for (var i:Number=0; i < 5; i++) {
trace(i);
}

AS3 Loops: For Loop breakdown

for( init ; condition ; next ) {
method

}
//init counts how many times the loop is executed
//condition how far to count up too
//next is what we count by
//what you want to loop goes in here

Actionscript:

for (var i:Number=0; i < vExample; i++) {
tMessages.appendText([i]+" , " );
}

AS3 Loops: While Loop breakdown

init;
while( condition ) {
method

next;

}
//init counts how many times the loop is executed
//condition how far to count up too
//next is what we count by
//what you want to loop goes in here

Actionscript:

var i:Number=0;
while(i<10){
trace (testArray[i]);
i=i+1
}

Loops: Create a "for loop" in AS3

Create a button movieclip on your stage.
Give it an instance name of bStart.

Create an input text box on your stage.
Give it an instance name of itSteps.

Create a dynamic text box on your stage.
Give it an instance name of tMessages.

Actionscript:

bStart.addEventListener(MouseEvent.CLICK, fLoop);

itSteps.restrict = "0-9";
itSteps.maxChars = 3;

function fLoop(event:MouseEvent):void {
tMessages.text="";
var vString:String = itSteps.text;
var vExample:Number = Number(vString);
for (var i:Number=0; i < vExample; i++) { tMessages.appendText([i]+", "); } }

Example:

Get Adobe Flash player