Mike Chambers

code = joy

initializing for loop counter outside of loop in ActionScript

with 8 comments

Continuing my series of posts of interesting, but not too useful ActionScript tips (which I learned from studying Objective-C), did you know that you dont have to initialize your counter variable within a for loop.

For example, this is perfectly valid:

package {
	import flash.display.Sprite;

	public class LoopTest extends Sprite
	{
		public function LoopTest()
		{
			var i:int = 0;
			for(; i < 5; i++)
			{
				trace(i);
			}

		}
	}
}

As you can see, the variable is initialized outside of the loop.

You also don’t have to include the test condition in the loop:

package {
	import flash.display.Sprite;

	public class LoopTest extends Sprite
	{
		public function LoopTest()
		{
			var i:int = 0;
			for(; ; i++)
			{
				trace(i);

				if(i > 5)
				{
					break;
				}
			}

		}
	}
}

However, if you do this (which you probably shouldn’t) you have to have code in the loop to exit, or else you will freeze up the player with an infinite loop.

And what the heck, lets get rid of incrementing the variable in the loop definition:

package {
	import flash.display.Sprite;

	public class LoopTest extends Sprite
	{
		public function LoopTest()
		{
			var i:int = 0;
			for(; ; )
			{
				trace(i);

				if(i > 5)
				{
					break;
				}

				i++;
			}

		}
	}
}

I can’t think of any reasons off the top of my head why this would be useful, but if you have any thoughts / uses, post them in the comments.

Written by mikechambers

October 23rd, 2007 at 11:43 pm

Posted in ActionScript

8 Responses to 'initializing for loop counter outside of loop in ActionScript'

Subscribe to comments with RSS or TrackBack to 'initializing for loop counter outside of loop in ActionScript'.

  1. Hey Mike!

    Hmmm… why would this be useful?… hmmm… tough question :D

    Nah… seriously: Performance comes to mind.

    I attended the Optimizing ActionScript 3.0 Performance session at MAX by Cédric Tabin, last week in Barcelona, and he gave some tips I would have never thought of (like i+=1 having better performance than i++ depending on datatype).

    Who knows… maybe defining the variable outside the loop has a better performance (let me know if anyone tests it) ;)

    Looking forwards to seeing you in Barcelona :)

  2. Alexandre: Never trust a performance test that you haven’t faked yourself. Or rather - that you haven’t confirmed yourself. I have also attended Cédrics session and one of the things I wrote down because it sounded so surprising was the i+=1 being faster than the i++. Unfortunately when I made a quick test for myself the i++ was actually faster. So maybe certain other circumstances have to be right that this works.

    Mario Klingemann

    24 Oct 07 at 3:02 am

  3. As I recall, in AS2, a for loop would compile into a while loop in byte code. Not sure if that’s true in AS3, but I wouldn’t be surprised. What you are doing here is basically using a for loop as a while loop. Whatever the use case, it might be a better use case for a while loop.

    Also, a lot of people don’t know that they can do multiple expressions in the for loop:

    for(var i=0, j=0; i

    Keith Peters

    24 Oct 07 at 3:17 am

  4. gah. stupid less than sign…

    trying again.

    for(var i=0, j=0; i < 10, j < 9; i++, j++)
    {
    trace(i, j);
    }

    Keith Peters

    24 Oct 07 at 3:18 am

  5. When you move the conditional outside the “for” and to the end of the inner braces, it’s actually different code now. A for loop will execute both the initialization and the test prior to first iteration so if the initialization results in a failed test, the loop never runs at all. If you move the condition inside the loop to the end, it’s a different loop.

    That’s not to say it’s wrong, there are certainly situations where you want to do that, but it’s important to be aware of the difference. Example:

    The following code works fine and prints nothing:

    var a:Array = [];
    var i:int = 0;
    for(; i

    Samuel Neff

    24 Oct 07 at 6:20 am

  6. looks like it cut off my examples :(

    Samuel Neff

    24 Oct 07 at 6:23 am

  7. Here are the examples of why it’s different..

    The following code works fine and prints nothing:

    var a:Array = [];
    var i:int = 0;
    for(; i < a.length; i++) {
    trace(a[i]);
    }

    but the modified version accesses an invalid index in the array:

    var a:Array = [];
    var i:int = 0;
    for( ; ; i++) {
    trace(a[i]);
    if (i == 5) {
    break;
    }
    }

    Samuel Neff

    24 Oct 07 at 6:24 am

  8. In case you hadn’t realized…
    The test condition doesn’t need to be about the initialized variable at all…

    for(line=0; !feof(filebuffer); ++line){
    linetext = fgets(filebuffer);
    echo( line , “\t”, linetext);
    }

    or for a graphic example

    for (sprite.x=1, sprite.y=1; !collision(enemy, sprite); ++sprite.x){
    playflyingsound();
    if (sprite.x > xmax) {sprite.x = 1;}
    }

    william bingham

    7 Jan 08 at 4:28 pm

Leave a Reply