<?xml version="1.0" encoding="UTF-8"?><rss version="2.0"
	xmlns:content="http://purl.org/rss/1.0/modules/content/"
	xmlns:dc="http://purl.org/dc/elements/1.1/"
	xmlns:atom="http://www.w3.org/2005/Atom"
	>
<channel>
	<title>Comments on: initializing for loop counter outside of loop in ActionScript</title>
	<atom:link href="http://www.mikechambers.com/blog/2007/10/23/initializing-for-loop-counter-outside-of-loop-in-actionscript/feed/" rel="self" type="application/rss+xml" />
	<link>http://www.mikechambers.com/blog/2007/10/23/initializing-for-loop-counter-outside-of-loop-in-actionscript/</link>
	<description>code = joy</description>
	<pubDate>Fri, 21 Nov 2008 02:13:31 +0000</pubDate>
	<generator>http://wordpress.org/?v=2.6.3</generator>
		<item>
		<title>By: william bingham</title>
		<link>http://www.mikechambers.com/blog/2007/10/23/initializing-for-loop-counter-outside-of-loop-in-actionscript/#comment-10833</link>
		<dc:creator>william bingham</dc:creator>
		<pubDate>Tue, 08 Jan 2008 00:28:21 +0000</pubDate>
		<guid isPermaLink="false">http://www.mikechambers.com/blog/2007/10/23/initializing-for-loop-counter-outside-of-loop-in-actionscript/#comment-10833</guid>
		<description>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 &#62; xmax) {sprite.x = 1;}
}</description>
		<content:encoded><![CDATA[<p>In case you hadn&#8217;t realized&#8230;<br />
The test condition doesn&#8217;t need to be about the initialized variable at all&#8230;</p>
<p>for(line=0; !feof(filebuffer); ++line){<br />
  linetext = fgets(filebuffer);<br />
  echo( line , &#8220;\t&#8221;, linetext);<br />
}</p>
<p>or for a graphic example</p>
<p>for (sprite.x=1, sprite.y=1; !collision(enemy, sprite); ++sprite.x){<br />
 playflyingsound();<br />
 if (sprite.x &gt; xmax) {sprite.x = 1;}<br />
}</p>
]]></content:encoded>
	</item>
	<item>
		<title>By: Samuel Neff</title>
		<link>http://www.mikechambers.com/blog/2007/10/23/initializing-for-loop-counter-outside-of-loop-in-actionscript/#comment-9499</link>
		<dc:creator>Samuel Neff</dc:creator>
		<pubDate>Wed, 24 Oct 2007 14:24:01 +0000</pubDate>
		<guid isPermaLink="false">http://www.mikechambers.com/blog/2007/10/23/initializing-for-loop-counter-outside-of-loop-in-actionscript/#comment-9499</guid>
		<description>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 &#60; 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;
  }
}</description>
		<content:encoded><![CDATA[<p>Here are the examples of why it&#8217;s different..</p>
<p>The following code works fine and prints nothing:</p>
<p>var a:Array = [];<br />
var i:int = 0;<br />
for(; i &lt; a.length; i++) {<br />
  trace(a[i]);<br />
}</p>
<p>but the modified version accesses an invalid index in the array:</p>
<p>var a:Array = [];<br />
var i:int = 0;<br />
for( ; ; i++) {<br />
  trace(a[i]);<br />
  if (i == 5) {<br />
    break;<br />
  }<br />
}</p>
]]></content:encoded>
	</item>
	<item>
		<title>By: Samuel Neff</title>
		<link>http://www.mikechambers.com/blog/2007/10/23/initializing-for-loop-counter-outside-of-loop-in-actionscript/#comment-9498</link>
		<dc:creator>Samuel Neff</dc:creator>
		<pubDate>Wed, 24 Oct 2007 14:23:09 +0000</pubDate>
		<guid isPermaLink="false">http://www.mikechambers.com/blog/2007/10/23/initializing-for-loop-counter-outside-of-loop-in-actionscript/#comment-9498</guid>
		<description>looks like it cut off my examples :(</description>
		<content:encoded><![CDATA[<p>looks like it cut off my examples :(</p>
]]></content:encoded>
	</item>
	<item>
		<title>By: Samuel Neff</title>
		<link>http://www.mikechambers.com/blog/2007/10/23/initializing-for-loop-counter-outside-of-loop-in-actionscript/#comment-9497</link>
		<dc:creator>Samuel Neff</dc:creator>
		<pubDate>Wed, 24 Oct 2007 14:20:19 +0000</pubDate>
		<guid isPermaLink="false">http://www.mikechambers.com/blog/2007/10/23/initializing-for-loop-counter-outside-of-loop-in-actionscript/#comment-9497</guid>
		<description>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 &lt;a&gt;</description>
		<content:encoded><![CDATA[<p>When you move the conditional outside the &#8220;for&#8221; and to the end of the inner braces, it&#8217;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&#8217;s a different loop.</p>
<p>That&#8217;s not to say it&#8217;s wrong, there are certainly situations where you want to do that, but it&#8217;s important to be aware of the difference.  Example:</p>
<p>The following code works fine and prints nothing:</p>
<p>var a:Array = [];<br />
var i:int = 0;<br />
for(; i <a></a></p>
]]></content:encoded>
	</item>
	<item>
		<title>By: Keith Peters</title>
		<link>http://www.mikechambers.com/blog/2007/10/23/initializing-for-loop-counter-outside-of-loop-in-actionscript/#comment-9496</link>
		<dc:creator>Keith Peters</dc:creator>
		<pubDate>Wed, 24 Oct 2007 11:18:43 +0000</pubDate>
		<guid isPermaLink="false">http://www.mikechambers.com/blog/2007/10/23/initializing-for-loop-counter-outside-of-loop-in-actionscript/#comment-9496</guid>
		<description>gah. stupid less than sign...

trying again.

for(var i=0, j=0; i &#60; 10, j &#60; 9; i++, j++)
{
	trace(i, j);
}</description>
		<content:encoded><![CDATA[<p>gah. stupid less than sign&#8230;</p>
<p>trying again.</p>
<p>for(var i=0, j=0; i &lt; 10, j &lt; 9; i++, j++)<br />
{<br />
	trace(i, j);<br />
}</p>
]]></content:encoded>
	</item>
	<item>
		<title>By: Keith Peters</title>
		<link>http://www.mikechambers.com/blog/2007/10/23/initializing-for-loop-counter-outside-of-loop-in-actionscript/#comment-9495</link>
		<dc:creator>Keith Peters</dc:creator>
		<pubDate>Wed, 24 Oct 2007 11:17:35 +0000</pubDate>
		<guid isPermaLink="false">http://www.mikechambers.com/blog/2007/10/23/initializing-for-loop-counter-outside-of-loop-in-actionscript/#comment-9495</guid>
		<description>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 </description>
		<content:encoded><![CDATA[<p>As I recall, in AS2, a for loop would compile into a while loop in byte code. Not sure if that&#8217;s true in AS3, but I wouldn&#8217;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.</p>
<p>Also, a lot of people don&#8217;t know that they can do multiple expressions in the for loop:</p>
<p>for(var i=0, j=0; i</p>
]]></content:encoded>
	</item>
	<item>
		<title>By: Mario Klingemann</title>
		<link>http://www.mikechambers.com/blog/2007/10/23/initializing-for-loop-counter-outside-of-loop-in-actionscript/#comment-9494</link>
		<dc:creator>Mario Klingemann</dc:creator>
		<pubDate>Wed, 24 Oct 2007 11:02:34 +0000</pubDate>
		<guid isPermaLink="false">http://www.mikechambers.com/blog/2007/10/23/initializing-for-loop-counter-outside-of-loop-in-actionscript/#comment-9494</guid>
		<description>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.</description>
		<content:encoded><![CDATA[<p>Alexandre: Never trust a performance test that you haven&#8217;t faked yourself. Or rather - that you haven&#8217;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.</p>
]]></content:encoded>
	</item>
	<item>
		<title>By: Alexandre Madurell</title>
		<link>http://www.mikechambers.com/blog/2007/10/23/initializing-for-loop-counter-outside-of-loop-in-actionscript/#comment-9492</link>
		<dc:creator>Alexandre Madurell</dc:creator>
		<pubDate>Wed, 24 Oct 2007 08:19:18 +0000</pubDate>
		<guid isPermaLink="false">http://www.mikechambers.com/blog/2007/10/23/initializing-for-loop-counter-outside-of-loop-in-actionscript/#comment-9492</guid>
		<description>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 :)</description>
		<content:encoded><![CDATA[<p>Hey Mike!</p>
<p>Hmmm&#8230; why would this be useful?&#8230; hmmm&#8230; tough question :D</p>
<p>Nah&#8230; seriously: Performance comes to mind. </p>
<p>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).</p>
<p>Who knows&#8230; maybe defining the variable outside the loop has a better performance (let me know if anyone tests it) ;)</p>
<p>Looking forwards to seeing you in Barcelona :)</p>
]]></content:encoded>
	</item>
</channel>
</rss>
