In the previous section, we learned about conditionals, which allow us to make choices in programming.
In this section, we’ll look at using loops to repeat actions multiple times, and save time.
Loops
Loops are like what they sound – something used to ‘loop’ through several times, doing the same thing each time.
For Loops
The most used loop in programming is the ‘FOR’ loop.
This is because of it’s structure is set up for you do define everything you need to to go from the beginning of an array to the end.
For example:
for(var i:int=0;i
You can see that it does the same thing as above, but in much less room. Any time you have objects that you need to do the same thing to, a for loop is much faster, and sometimes the only sane way to do things.
To explain what is actually happening:
'for(var i=0;'
This part is just starting the for loop, with the first thing before the semi colon (';') being setting up anything that will be needed in the for loop.
Here, we just create the an integer named 'i' (typically used, because it represents the index of the array, as well as one character so quick to type), and set it to 0, or the first element of our array (the String with 'Goomba Steve' as the value).
We start of at the beginning, because in the next part of the for loop, we tell it where to stop.
'i
var i:int=0;
if(i
You'll find that storing things in an array and using a for loop to loop through them is very helpful, fast and time efficient, and find yourself using them often.
There is another way to loop through for loops in Actionscript 3.0, which saves some time to type out.
for(var i in goombas)
{
trace("The name of the Goomba in goombas["+i+"] is: "+goombas[i]);
}
//this will print out:
//The name of the Goomba in goombas[0] is: Goomba Steve
//The name of the Goomba in goombas[1] is: Goomba Bob
//The name of the Goomba in goombas[2] is: Goomba Bruce
This will do the same thing as if you typed out "for(var i:int=0;iWhile Loops
While loops are basically just the second part of the for loop. If something is true, the while loop will keep repeating until it is false, and exit.
These can get you into trouble if you create a loop that never exits!
An Example:
var age:int=6;
while(age<40)
{
trace("you are only "+age+", you are still young!");
age++; //this adds 1 to age- the same as using: age=age+1;
}
trace("you are now 40. Life is over");
In this example, we set the age to 6, and the while loop checks if the age is less than 40. If so, it runs what is in the brackets, which prints out the age, and increases it by one. The loop will continue to run through repeatedly until the 'while' condition is met (age<40).
When the age finally reaches 40, the loop will exit and it will print out the statement below the loop.
So for output, you will see:
"you are only 6, you are still young!" (in loop first time, age=6, going to add 1 to it)
"you are only 7, you are still young!" (in loop second time, age=7, going to add 1 to it)
"you are only 8, you are still young!" (in loop second time, age=8, going to add 1 to it)
...
...
"you are only 39, you are still young!" (in loop second time, age=39, going to add 1 to it)
(age is now 40, so will exit loop before tracing out the sentence again)
you are now 40. Life is over
Do While Loops
Another loop type is the "do while" loop. This is slightly different than a while loop, in that it will first run what is in the brackets, than check if it is still true, then repeat - vs. checking before running.
An example:
var age:int=22;
do
{
age++; //this adds 1 to age- the same as using: age=age+1;
trace("age is now"+age);
}
while(age<5);
//output from above will be:
//"age is now 22"
//and exit since now it finds that age is not under 5, which wasn't checked before the loop(because it's a do-while loop)
var age2:int=22;
while(age2<5)
{
age2++;
trace("age2 is now"+age2);
}
//never enters this loop because it checks before entering the loop, and finds that age2 is NOT less than 22
The 'do-while' loop will first do whatever is in the brackets(add one to the age and print out the age), then check if the condition is still true, find that it's not, and exit.
When it gets to the 'while' loop, it will check beforehand if the condition is true, find that it is not (age2 is not less than 5), and will skip running the loop.
The do while loop is useful when you want to make sure something will run at least once, before attempting to loop.
Stopping, or Skipping in a Loop (Break and COntinue)
Sometimes you'll find yourself trying to optimize your code better, or save processing time, and a good way to do it is to only go through an array until you find what you're looking for.
Or there may be times you might change something in the loop, and not want it affecting the rest of the loops, so you need a way to exit a loop before reaching the end.
The method of doing this is called adding a 'break' into the loop.
For example:
var names:Array = ["Tim", "Sally", "Dude", "Ralphy", "Wiggum"];
var position:int=-1;
for(var i in names)
{
if(names[i]=="Dude")
{
trace("Found the dude!");
position=i;
break;
}
else
trace("Ah, it's only "+names[i]);
}
if(position!=-1)
trace("The dude was in position "+position);
else
trace("we did not find the dude!");
//this will trace out:
//Ah, it's only Tim
//Ah, it's only Sally
//Found the dude!
//The dude was in position 2
We first create an array called 'names', with a bunch of random names in it.
Next, we create a variable named position, set to negative 1(-1), that we'll use to store the position of the dude. We set it as negative 1, because their cannot be any negative indexes of the array, so if it is still negative after the loop, we know we didn't find the dude at all.
We create the for loop, check if the value in the array is 'Dude', if so print out 'Found the dude', store the position, then use break, or exit out of the array.
If we didn't find the dude, we print out "Ah, it's only " and whoever we did find, and continue with the loop.
After the loop, we check to see if the position found is not negative one- if it isn't we print out where we found the dude, if it is negative 1, we print out we didn't find him.
The main value in this, is that we skipped having to check the other names, and were able to exit the array even before we got to the else. If we had hundreds of names to go through, this would save a lot of time if the name was n't at the end.
Continue
Continue is used in a simmilar way to "break", but instead of exiting the loop, it just skips over everything else in that loop, and continues with the next one.
For example:
var robots:Array = ["Robot1", "Robot2", "Robot3"];
for(var i in robots)
{
trace("I am "+robots[i]+", please do not destroy me");
if(robots[i]=="Robot2")
{
trace(robots[i]+" was spared.");
continue;
}
trace("Machine is now destroying "+robots[i]);
}
//this will print out:
//I am Robot1, please do not destroy me
//Machine is now destroying Robot1
//I am Robot2, please do not destroy me
//Robot2 was spared.
//I am Robot3, please do not destroy me
//Machine is now destroying Robot3
As you can see, when you create a continue, it will skip everything else in that loop, and go onto the next one. Robot 2 survived, but Robot 1 and 3 weren't so lucky.
Conclusion
In this section you learned the basics of using loops. For loops will be used the most commonly, but the other two are good to know about as well.
In the next section, we'll introduce functions, which will let you re-use code, and have blocks devoted to just certai actions.
Other Articles in this Series
Hi dude, great tutorial, but 1 thing really bothers me:
You saythat (var i in names) is the same as doing i++ everytime when the loop ends. Why is ”in” adding i++? What does the ”in” mean?
It’s basically a more “natural sounding” way of going through a loop of objects, and is a little faster to type.
Like saying “for each apple in the bag”, or each “cookie in the box”.
It’s faster to type out that notation, but some other languages don’t use it, so I generally just stick to going through a for loop using the ‘i++’ way, unless I’m just trying to make something very fast.
thanks for the tutorial but i have a question.
i followed everything to the letter for the last 2 parts but i keep getting error messages for a section the does not exist i.e “40 expecting right brace before program end”. i could really use any kind of help i can get.you can contact me through email
Thanks again
Hey, that’s probably just because you are missing a closing brace somewhere, and they aren’t even by the end of the program. So like :
class hello
{
function waveandshout()
{
}//closing brace for the function/method-
}//closing brace for the class- one of them must be missing somewhere