<?xml version="1.0" encoding="UTF-8" ?>
<?xml-stylesheet type="text/xsl" href="http://blogs.msdn.com/utility/FeedStylesheets/rss.xsl" media="screen"?><rss version="2.0" xmlns:dc="http://purl.org/dc/elements/1.1/" xmlns:slash="http://purl.org/rss/1.0/modules/slash/" xmlns:wfw="http://wellformedweb.org/CommentAPI/"><channel><title>Why the compiler can't autoconvert foreach to for</title><link>http://blogs.msdn.com/oldnewthing/archive/2004/04/21/117574.aspx</link><description>Because the semantics are different.</description><dc:language>en-US</dc:language><generator>CommunityServer 2.1 SP1 (Build: 61025.2)</generator><item><title>RE: Why the compiler can't autoconvert foreach to for</title><link>http://blogs.msdn.com/oldnewthing/archive/2004/04/21/117574.aspx#117576</link><pubDate>Wed, 21 Apr 2004 17:01:00 GMT</pubDate><guid isPermaLink="false">91d46819-8472-40ad-a661-2c78acb4018c:117576</guid><dc:creator>kpako@yahoo.com (Dare Obasanjo)</dc:creator><description>My favorite movie subject all at once period was Antz and A Bug's Life. The Deep Impact/Armageddon period was also interesting. </description></item><item><title>re: Why the compiler can't autoconvert foreach to for</title><link>http://blogs.msdn.com/oldnewthing/archive/2004/04/21/117574.aspx#117595</link><pubDate>Wed, 21 Apr 2004 17:29:00 GMT</pubDate><guid isPermaLink="false">91d46819-8472-40ad-a661-2c78acb4018c:117595</guid><dc:creator>Eric Lippert</dc:creator><description>On a related topic, the way JScript/JScript .NET handle this is a little weird.  I wrote a couple of articles on that a while back:&lt;br&gt;&lt;br&gt;&lt;a target="_new" href="http://blogs.msdn.com/ericlippert/archive/2003/09/22/53063.aspx"&gt;http://blogs.msdn.com/ericlippert/archive/2003/09/22/53063.aspx&lt;/a&gt;&lt;br&gt;&lt;br&gt;&lt;a target="_new" href="http://blogs.msdn.com/ericlippert/archive/2003/10/01/53134.aspx"&gt;http://blogs.msdn.com/ericlippert/archive/2003/10/01/53134.aspx&lt;/a&gt;&lt;br&gt;&lt;br&gt;Remember 1988, when all those terrible mind-body-switch movies came out at the same time?   That was weird.&lt;br&gt;</description></item><item><title>RE: Why the compiler can't autoconvert foreach to for</title><link>http://blogs.msdn.com/oldnewthing/archive/2004/04/21/117574.aspx#117630</link><pubDate>Wed, 21 Apr 2004 18:13:00 GMT</pubDate><guid isPermaLink="false">91d46819-8472-40ad-a661-2c78acb4018c:117630</guid><dc:creator>nospamplease75@yahoo.com (Haacked)</dc:creator><description>To prove your point, I had written an enumerator called PageRangeEnumerator.  You give it a string like &amp;quot;1-4,7,18+&amp;quot; and it would only enumerate over those pages.</description></item><item><title>re: Why the compiler can't autoconvert foreach to for</title><link>http://blogs.msdn.com/oldnewthing/archive/2004/04/21/117574.aspx#117710</link><pubDate>Wed, 21 Apr 2004 20:01:00 GMT</pubDate><guid isPermaLink="false">91d46819-8472-40ad-a661-2c78acb4018c:117710</guid><dc:creator>Clinton Pierce</dc:creator><description>Larry Wall talks about optimizing for the Programmer as well as for Runtime speed.  I'll give up quite a few CPU cycles to maintain foreach(...) over a corresponding for(...) loop anytime.&lt;br&gt;&lt;br&gt;</description></item><item><title>re: Why the compiler can't autoconvert foreach to for</title><link>http://blogs.msdn.com/oldnewthing/archive/2004/04/21/117574.aspx#117776</link><pubDate>Wed, 21 Apr 2004 21:55:00 GMT</pubDate><guid isPermaLink="false">91d46819-8472-40ad-a661-2c78acb4018c:117776</guid><dc:creator>David Kafrissen</dc:creator><description>You have described why the compiler cannot optimize one form into another, but you did not describe why one form is less efficient than the other.</description></item><item><title>re: Why the compiler can't autoconvert foreach to for</title><link>http://blogs.msdn.com/oldnewthing/archive/2004/04/21/117574.aspx#117781</link><pubDate>Wed, 21 Apr 2004 21:59:00 GMT</pubDate><guid isPermaLink="false">91d46819-8472-40ad-a661-2c78acb4018c:117781</guid><dc:creator>Raymond Chen</dc:creator><description>My topic is &amp;quot;Why the compiler can't autoconvert foreach to for&amp;quot;, not &amp;quot;Which one is faster&amp;quot;. That's a topic for somebody else (try the links I provided).</description></item><item><title>re: Why the compiler can't autoconvert foreach to for</title><link>http://blogs.msdn.com/oldnewthing/archive/2004/04/21/117574.aspx#119652</link><pubDate>Sun, 25 Apr 2004 01:20:00 GMT</pubDate><guid isPermaLink="false">91d46819-8472-40ad-a661-2c78acb4018c:119652</guid><dc:creator>JFo</dc:creator><description>It turns out that sometimes a foreach can be faster than a for loop - some people have been converting their loops from this:&lt;br&gt;&lt;br&gt;foreach (Control c in this.Controls) {&lt;br&gt; // ...&lt;br&gt;}&lt;br&gt;&lt;br&gt;to this:&lt;br&gt;&lt;br&gt;for (int i = 0; i &amp;lt; this.Controls.Count; i++) {&lt;br&gt;  // ...&lt;br&gt;}&lt;br&gt;&lt;br&gt;Since you're now accessing the &amp;quot;Controls&amp;quot; property on every iteration instead of using the enumerator it significantly slows down the loop.</description></item><item><title>re: Why the compiler can't autoconvert foreach to for</title><link>http://blogs.msdn.com/oldnewthing/archive/2004/04/21/117574.aspx#121505</link><pubDate>Tue, 27 Apr 2004 22:07:00 GMT</pubDate><guid isPermaLink="false">91d46819-8472-40ad-a661-2c78acb4018c:121505</guid><dc:creator>indranil banerjee</dc:creator><description>but this just begs the question: Why were they designed to be semantically different?&lt;br&gt;&lt;br&gt;Sure, &amp;quot;depending on the circumstances, it may be better for the program to crash than to produce incorrect results&amp;quot;. And sometimes the other way around. The language designer can never know. So why the inconsistency? &lt;br&gt;&lt;br&gt;I would choose the more permissive of the two choices, apply it consistently to both keywords and leave error handling to the programmer.&lt;br&gt;&lt;br&gt;BTW, JFo a really smart foreach would generate this :-&lt;br&gt;&lt;br&gt;int end = this.Controls.Count&lt;br&gt;for (int i = 0; i &amp;lt; end; ++i) { &lt;br&gt;// ... &lt;br&gt;} &lt;br&gt;&lt;br&gt;thats what happens in STL&lt;br&gt;&lt;br&gt;</description></item><item><title>re: Why the compiler can't autoconvert foreach to for</title><link>http://blogs.msdn.com/oldnewthing/archive/2004/04/21/117574.aspx#121558</link><pubDate>Tue, 27 Apr 2004 22:51:00 GMT</pubDate><guid isPermaLink="false">91d46819-8472-40ad-a661-2c78acb4018c:121558</guid><dc:creator>Raymond Chen</dc:creator><description>They are different because &amp;quot;foreach&amp;quot; is a generalized enumerator whereas &amp;quot;for&amp;quot; works only for integer-indexed objects. One is generalized and one is specialized.</description></item><item><title>re: For = Foreach</title><link>http://blogs.msdn.com/oldnewthing/archive/2004/04/21/117574.aspx#126510</link><pubDate>Wed, 05 May 2004 19:34:00 GMT</pubDate><guid isPermaLink="false">91d46819-8472-40ad-a661-2c78acb4018c:126510</guid><dc:creator>Nick's Delphi Blog</dc:creator><description /></item><item><title>re: Why the compiler can't autoconvert foreach to for</title><link>http://blogs.msdn.com/oldnewthing/archive/2004/04/21/117574.aspx#173375</link><pubDate>Mon, 05 Jul 2004 19:03:00 GMT</pubDate><guid isPermaLink="false">91d46819-8472-40ad-a661-2c78acb4018c:173375</guid><dc:creator>Raymond Chen</dc:creator><description>Commenting on this entry has been closed.</description></item></channel></rss>