Main Content

You are here:

While Loops

Welcome to this free Flash tutorial, which is about the basic syntax of another staple food of the ActionScript programmer, the while loop. Thankfully for me (having to type this) the implementation of while loops in both ActionScript 2 and 3 are identical. Hooray!

While loops are basically the younger brother of for loops, in the sense they need a little more attention, or they'll end up burning the metaphorical house down. The basic syntax for one of these while loops looks like this:

while (condition) {
                                //do something here
                                }
                                

You'll notice that actually they look pretty darn similar to if statements, which you can read about in detail on the If and Else tutorial page. Because of this, it may help to think of a while loop as a repeated if statement. At the start of every iteration (run through) the condition is checked. If this comes up as false, the loop stops.

Good while loops

So, here is an example of a while loop that works:

var x = 1;
while (x < 5) {
        trace("x is NOT 5, but less");
        x++;
}
In this loop, x is incremented every iteration until it becomes 5, at which point the condition comes up as false, and the loop stops. The system listed above (with x as a counter) is quite probably going to be how you use a while loop. As with for loops, you can print or use the counter in what you do:

var x = 1;
while (x < 5) {
        trace("x is " + x);
}

You don't have to use a counter though. As long as you have a condition that will eventually turn false (do make sure of this), then you can do much more. For instance, you can incorporate the counter into your condition in a different way:

var i = 1;
var anArray:Array = ["1","2","3",""];
while(anArray[i] != ""){
        trace(anArray[i]);
        i++;
}

In the above example, we use a more complicated condition. While loops, though, can look better and be short than their for loop conterparts. The for loop you would have to construct if you wanted to remove all the elements in an array, returning them one-by-one would be more complex than the while loop alternative, for example:

var anArray:Array = ["1","2","3",""];
while(anArray.length > 0){
        trace(anArray.shift()); //Traces '1' then '2', '3', ''
        //shift(): Removes the first element from an array and returns that element.
}

Bad while loops

WARNING: If you don't change something, causing the it'll just continue ad infinitum. A demonstration will follow.

var x = 1;
while (x < 5) {
        trace("x is NOT 5, but less");
}

This loop is really not, because it eats up memory, crashing the browser. In the example, x always remains at 1. Unlike in a for loop, it hasn't changed at all. It is always going to be less than 5, so the loop is never going to stop.

Of course, the only way you're going to find out how to implement them in the situation you require is to experiment, so go and enjoy yourself...

Harry.

Comments