- Home
- Tutorials
- Flash
- Intermediate
- The power of nested loops
The power of nested loops

Page 2 of 2
Neil Webb
Neil Webb is a Flash Platform Developer currently living and working in England. He joined the site when it was just a baby - look at it now! Neil has previously worked for Cambridge University, FIFA and Hutchinson Whampoa among others.
What if you were told to sort them in to suits, and then give them to your friend who would sort each suit in to numerical order? That's not so bad. What order did those functions fire-off in again?!
Hang on, this cheeky person now wants you to sort two packs of cards! Why I oughtta!
Okay, let's return to our code, but this time we'll change the trace outputs. Very-little code is being changed here, but just look at what happens! Yep there's been a lot of code in this tutorial so far but just have a look at the trace outputs for this next example and everything will fall into place!
myLoop1();//call the first function
function myLoop1(){
for(i=1; i<3; i++){
trace("person one passes a deck of cards to person two");
myLoop2();//call the second loop from within the first loop
}
trace ("finished deck");
}
function myLoop2(){
for(j=1; j<5; j++){
trace("__person two passes suit " + j +"/4 from deck " + i + " to person 3");
myLoop3();//call the second loop from within the first loop
}
trace ("_finished sorting suit " + (j-1) + " of deck " + i);
}
function myLoop3(){
for(p=1; p<13; p++){
trace("______person three sorts card number " + p + "/12 from deck " + i+ ", suit " + j );
}
trace ("_finished sorting cards from suit " + j);
}
[The eagle-eyed amongst you will have noticed that I changed the loop values for each loop too. We had two decks of cards, so the loop in function 1 had to iterate twice. There are four suits in every deck so function 2's loop had to iterate four times, and there are twelve cards in each suit so the last loop had to iterate twelve times. I also started my variables off at 1 rather than 0 to make the output easier to read]. Here's a bit of our output:

It works in reverse too, very much like a car assembly line.
Throw a bit of data at function1, it adds something (or calculates something) and passes the finished result to function2, and so on.
If you've ever seen or used XML you will know that it has a tree-structure, as does your folder-view in Windows Explorer (PC users: press the windows key and 'e' to bring up Windows Explorer). Let's have a look at them both. For Windows Explorer you will see something like this:

XML is based on the same organisation principle:

Hey I've got an idea...say I wanted to let the user retrieve all the chapter-titles for my book? Well, what if we pass the whole book to function 1, which in turn passes each chapter to function 2, which passes each chapter-title to function 3.......
I hope you're starting to see just how powerful all this can be. I've tried to keet this tutorial as generic as possible because loops can be used for so many things, not just sorting data, and I didn't want to get bogged down explaining strings or maths functions.....
What you do inside those loops and how many nesting levels you have is pretty much up to you. I'm sure you can find many more uses. Enjoy :)

