This article is meant for those who attend the class IKT2 at Vågsbygd VGS, yet it is still written in english in case anyone else wants to read this post. I do not claim to be an expert on the subject, yet I will try to describe the differences and similarities between the for-loop, the while-loop and the do…while-loop. I will also comment on the switch-case. Last I’ll briefly go trough break and continue. If anyone has any questions or comments, please write them in english, or they will not be answered.
The first one you learn and the easiest one to use is the while-loop. It is pretty straight forward. It is used to do something (whatever you like) while a statement is true. The syntax of the while-loop is like this:
| ActionScript 3 code | |
1 2 3 4 |
while(condition) { //Do something } |
The while loop can be used to do something a number of times. For example the next code will trace out every number between 0 and 15 (including 0, not including 15).
| ActionScript 3 code | |
1 2 3 4 5 6 |
var i:Number = 0; while(i < 15) { trace(i); i++; } |
However, when itterating through something like this it is much more common to use the for-loop. In cases like this, there is no difference except the syntax. This example does exactly the same as the previous one, only it uses the for-loop instead of the while-loop.
| ActionScript 3 code | |
1 2 3 4 |
for(var i:Number = 0; i < 15; i++) { trace(i); } |
As you can see, this is done with a lot less code, yet the result is exactly the same. The syntax of the for-loop is as following:
| ActionScript 3 code | |
1 2 3 4 |
for(initilization; condition; incrementation) { //Do something } |
I realize that the name “incrementation” is not correct, yet it’s the best I can come up with. The point is that you do something several times. I usually use the for-loop instead of the while-loop, except when it’s something I can’t count. For example if I’m reading a file, I use a while loop as long as there’s still something left to read. Other than that I use the for-loop.
Occationally you want to make shure something happens at least once. In that case you’ll want to use the do…while-loop. A good example of this is if you wan’t to calculate a random number between 1 and 6, yet you don’t want it to be 5 or 3. I would do that like:
| ActionScript 3 code | |
1 2 3 4 5 6 |
var rndNum:Number; do { rndNum = Math.floor(Math.random() * 6) + 1; } while(rndNum != 5 && rndNum != 3); |
This first creates an empty variable called rndNum, then it keeps generating a new random number until rndNum is not equal to rndNum 5 or 3. Here I use the while-loop because I don’t know how many times I need to recalculate rndNum. It might be none or it might be a hundred.
Last I’d like to give a simple example of the switch-case condition. The switch-case is only used for special cases. I only use the switch-case when I KNOW that the input is going to be one in a range of others. For instance, if I asked you for a number between 1 and 10, and would treat you differently based on your result, I’d use the switch-case. I better example is a simple program where the user is asked to enter a number (a choice) between one and three. This could be used for a menu (press 1 to save, press 2 to load and press 3 to exit). For example like this:
| ActionScript 3 code | |
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 |
var input:Number = Number(inputField.text); switch(input) { case 1: //Save break; case 2: //Load break; case 3: //Exit break; default: //Display "incorrect input" break; } |
Thats basically it about the switch-case.
Two more things that is needed to be fully able to use loops and switch-case statements is the understanding of break and continue. The break is used to exit a loop. For instance the next loop will only trace out the numbers 0 to 10 (including 10), even though the loop itself looks like it is to trace out 11, 12, 13 and 14 as well.
| ActionScript 3 code | |
1 2 3 4 5 6 7 8 |
for(var i:Number = 0; i < 15; i++) { trace(i); if(i == 10) { break; } } |
There actually isn’t anything more to it. All the break does is exit the loop it’s in. One thing worth mentioning though is that the switch-case is also treated as a loop in that aspect. That’s why we always use break before the next case, to make shure that when it has done what it was meant to, it exits the switch-case.
The last topic is the continue statement. It is used to skip the current loop-run. This next code will trace out every number from 0 to (including) 14 exept the number 5.
| ActionScript 3 code | |
1 2 3 4 5 6 7 8 |
for(var i:Number = 0; i < 15; i++) { if(i == 5) { continue; } trace(i); } |
The continue is hard to explain, and from my experiece rarely used, yet it might be useful to know. However, you should be careful when using it with the while-loop.
That’s about it for this time. If you have any questions, please leave me a comment.
Special thanks to:
- Eirik Løhaugen Fjærbu
- Johanne Rasmussen


