Tutorial details:
Written by: Jesse Stratford
Time: 20 - 30 minutes
Difficulty Level: Intermediate
Requirements: Flash 5.
Topics Covered: Do/While Loops, While Loops, For Loops, Loop the loops, Fruity Loops, Hoolah Hoops...
Assumed knowledge: Instance, Paths, Get Property, Set Property. See the relevant tutorials on this site.

Grab the source to the above movie if you want it.

In this tutorial I'm going to give you some background info into loops, their uses and how they work. If you don't understand any of the syntax in this tutorial, that's OK, as I'm going to be using some syntax which might look a little complicated in order to give practical examples, but I'll keep it as easy as I can.

Loops are one of the most valuable assets of programming, in my opinion. In Flash they are invaluable as they can save you having to type out commands manually when you can give a base case and loop. The simplest example is this: I have a variable X which I want to increment 100 times. I could do this by typing

x += 1; // or
x++; // or even
x = x+1

they all do same thing.

but I'd have to either (i) type it 100 times, or; (ii) make it a looping movie clip or some such, which would mean it would take 100 times the length of a frame (about a 12th of a second, so about 12 seconds) just to count to 100. Not too practical huh? So instead we use loops. This is the most basic example of each of the loop syntax:

Example 1

for (y=0; y<100; y++) {

Example 2
while (x<100) {

Example 3
do {
 } while (x<100);

OK so that just looks like scary code, you want to know what it does right?

Example 1 Description
This is a For Loop, my personal favorite for no reason in particular. For Loops take 3 arguments in their first line: the initialization value, the condition, and the action, respectively. Then all the actions we want to perform go within the curly braces of the For Loop. So, in worded form this code could be read as:
"Start with the variable y equal to zero. Perform the actions within the curly braces, (in this case, add one to x) then add one to y. If, when these actions are completed, y is still less than 100, repeat these actions"
Note that in this example I used 2 variables, one for the For Loop's own counter and one for the action we wanted to carry out, (increment a variable), this is just to keep the code from being too confusing.

Example 2 Description
This is a While Loop. It works in almost the same manner but you must adapt the condition within the curly braces. If I say "Bark like a dog while I hold up my hand", you would start barking and I could keep my hand up forever, and you would get a very sore throat. So I have to drop my hand at some point. This is the same with the While loop; it's condition must be met at some point (within a bounded number of calculations) or it will crash the Flash player. I don't actually know how many calculations Flash limits itself to but I'm doing experiments with Alpha-Beta searching at the moment and in my opinion the limit is too low :o) (If you can be bothered reading the tech notes, let me know the limit and I'll add it here).
In our For Loop above, we state that we want to increment the counter each time the loop is run, (each time a loop is run through it is called an 'iteration'), so we don't need to make modifications to our conditions within the loop itself. In While and Do While Loops however, we must alter the condition within the loop (or the condition must be changing during the process of the loop) to prevent performance drain.
If I had done this:
while (x<100) {

I would have crashed my Flash player because x will always be less than 100, since we are not changing it within the loop. Sure we're changing it outside the loop, but Flash wants to do all the calculations within the loop before it moves on, so it will get confused and stop working.

Example 3 Description
This is a Do While loop, it's basically the same as the previous example but the syntax is different. Essentially the difference between a While loop and a Do-While loop is that the condition of the Do-While is checked at the end of the loop. As such, even if the condition is false to begin with, the contents of the loop will run at least once, because the condition isn't checked until after the loop is run.

Great. But so what? So plenty, turn the page...