Well Flash ActionScript loops are tools used to execute a segment of code repeatedly for a number of times or while a certain condition is satisfied. This can save time and effort by not having to type the same code multiple times to repeat a process.
For example, if we wanted to duplicate a movie ten times, without a loop, we would have to type the same code ten times.
Using a loop to do this would reduce the amount of code significantly while generating the same identical result. Below is an example of the same command to duplicate a movie ten times using a loop.
for (i=1;i<11;i++){
duplicateMovieClip ("movie_mc", "new_"+i, i);
}
loop Types
There are three main types of ActionScript loops, the for loop (which we used in the example above), the while loop, and the do-while loop. The for and while loops do exactly the same thing but with different syntax, the do-while loop differs in one small aspect. We will go through all of these types in turn.
The For loop
I think that the For loop is the most commonly used because it is the most compact and the easiest to understand and use. You can use this loop to execute a number of code statements multiple times using this format:
for (counter; condition; action){
statements;
}
The example above shows the syntax used, the counter (i) sets the starting point for your counter, the condition will determine the point at which the loop will have to stop, and the action operate counter to eventually make it make the condition untrue to stop the loop. The code to be repeated is placed with in the curly brackets.
For example, if we want to output some text ten times, we can do it this way.
for (i=1; i<11; i++){
trace ("This code is repeated ten times");
}
In regular words the code above says, start counting i from 1, repeat the code below as long as i is less than 11, and add 1 to i each time the loop is repeated. We start with i as 1, when the code is repeated the first time then the value of i increases by 1 making it equal 2, when the code is repeated again it becomes 3, then 4, 5, 6, 7, 8, 9, and 10. After that it becomes 11, and at that moment the condition is no longer satisfied because i is not less than 11 (i>11), and so the loop ends at that point.
The While loop
The while loop repeats a set of code as long as the condition specified is true.
while (condition) {
statements
}
This looks very similar to a conditional, which execute a code once if the condition is satisfied, but here the code is executed repeatedly instead of just once. If the condition remained true forever, this means the loop will be repeated over and over again forever as well, and that should be avoided at all costs, because an infinite loop will crash the Flash player. So there is usually something within the statements that eventually makes the condition untrue.
var i = 1;
while (i < 5){
trace ("This code is repeated");
i++;
}
In the example above, the code will get repeated as long as i is less than 5, we made sure that i increases by 1 each time the loop is cycled through.
For more details , Please visit the source site :
http://www.oman3d.com/tutorial s/flash/loops/
Answered by
Alok Gupta
, an ibibo Guru,
at
1:34 AM on October 11, 2008