Advertisement |
The "While" Loop
The while loop has the same exact function as the for loop, but it is written in a different format. Instead of having all the information necessary to run the loop in a single place, the while loop only requires the condition to be provided to the loop itself and leaves the rest for the programmed to supply in the surrounding code.
while (condition){
statements;
}
statements;
}
You might have noticed that the general format of a while loop looks almost identical to an if conditional. An if conditional will be executed only once if the condition is satisfied while the while loop will be executed repeatedly while the condition is satisfied.
In order to use this loop you must create your own counter and then create your own action to invalidate the condition later. The code which we have created earlier can be ported in this loop format this way:
var i:Number = 0;
while (i < 10){
new MovieClip();
i++;
}
while (i < 10){
new MovieClip();
i++;
}
You should note that it is much easier to create an infinite loop by mistake using a while conditional as you might forget to provide the action required to increase the value of the counter. You should always use the for loop for your basic usage instead of attempting to use the while loop if your program does not require it specifically.
0 comments: