Flash AS3 Tutorials Center | Interesting Flash Animation Tutorials | Valuing Flash Animation Tutorials | Flash ActionScript and Animation Home

AS3 Beginner Tutorials | AS3 Basic Lessons | AS3 Valuing Courses | AS3 Components Tutorials | AS3 and PHP Interaction Tutorials
AS3 Practical Tutorials | AS3 Animation Techniques | AS3 Transition Effects Tutorials | AS3 Download Upload Files | AS3 Particle Systems
Communication Between Flash Movies with AS3 | AS3 and JavaScript interaction | AS3 Matrix Transformation | AS3 Physics Simulation Tutorials

How to Break a For Loop in Flash ActionScript 3

Sometimes this is necessary to break or stop a looping action when a certain criteria is satisfied.

Please update flash player to view this Flash ActionScript tutorial!

Flash Tutorial Content:

The break action instructs Flash to skip the rest of the loop body, stop the looping action, and execute the statement following the loop statement.

The complete Flash Movie is shown as above, you may try how it works before you start this Flash tutorial.

Flash ActionScript Codes:

//Set the multiline parameter of the Text Field to true
//The <br> tag creates a line break in the Text Field.
//Must set the text field to be a multiline Text Field to use this tag.
output_txt.multiline = true;

 

//Declare a string to the data
//Also assign an empty value to this string (myString)
var myString:String = "";

 

//Declare i as an integer
var i:int;

 

//Execute the For Loop Statement
//A For Loop requires three expressions:
//a variable i is set to an initial value - zero
//a conditional determines when the looping ends - when i < 5
//an expression changes the value of the variable with each loop - i++ (increase by one)
for (i = 0; i < 5; i++)
{

    myString = myString + "The value of i is: " + i + "<BR>";

    // If i is equal to 5 then break the for loop
    if (i == 5) {
     myString = myString + "The for loop will break (stop) when i is " + i + "<BR>";
     break;
    }

}

 

//Display the string to Text Field
output_txt.htmlText = myString;

Download Flash Source File:

Flash Source File for-loop-with-break.fla

Remarks:

The break function also works for while and do while looping.