The differences and similarities between for, while and do…while (AS3)

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, and only want it to be 5 or 3. It could be done like this:

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
About these ads

10 thoughts on “The differences and similarities between for, while and do…while (AS3)

  1. David says:

    Good overview of the language conditionals. These are pretty much the same in most of C family of languages like PHP and stuff – so this is useful for any programmer.

    Also, some languages support break(5); if you need to exit more than one loop level.

  2. alxen says:

    Yea, I know, but I tried to keep it simple. And I’m not shure if you can use break(n); in AS3 so I didn’t mention it. Also, I’ve nearly never used it, so I don’t think they’ll have to either.

  3. [...] that any code below in that same function will not be exicuted. You can think of it like a break (The differences and similarities between for, while and do…while (AS3)), only for [...]

  4. Mark says:

    Thanks a bunch, I just had my need for continue for the first time and that was nice simple explanation

  5. kevin alex says:

    o thank u i was studding for my mid term exam and i was really confused

  6. Great post, thanks for writing it!

    I think there’s a mistake when you talk about the do…while loop though, the part where you say:

    “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 think you meant:

    “this is if you want to calculate a random number between 1 and 6, and you WANT it to be 5 or 3″

    in your example the random number is calculated as long as it’s different from 5 or 3, so the result will be 5 or 3.

  7. Arunjit Das says:

    Its very helpful.Thanks.

    Can you please elaborate the difference between for and while loop.

    • I think I already did though. The for-loop is simply syntactic sugar for a while-loop though (unless you use break and continue inside, in which case it’s a bit more difficult). A basic for-loop can easily be written as a while-loop. You just put initialization before the loop, and the incrementation at the bottom.

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s