Note that there are some explanatory texts on larger screens.

plurals
  1. POGuideline: while vs for
    text
    copied!<p><em>Disclaimer: I tried to search for similar question, however this returned about every C++ question... Also I would be grateful to anyone that could suggest a better title.</em></p> <p>There are two eminent loop structure in C++: <code>while</code> and <code>for</code>.</p> <ul> <li>I purposefully ignore the <code>do ... while</code> construct, it is kind of unparalleled</li> <li>I know of <code>std::for_each</code> and <code>BOOST_FOREACH</code>, but not every loop is a <code>for each</code></li> </ul> <p>Now, I may be a bit tight, but it always itches me to <em>correct</em> code like this:</p> <pre><code>int i = 0; while ( i &lt; 5) { // do stuff ++i; // I'm kind and use prefix notation... though I usually witness postfix } </code></pre> <p>And transform it in:</p> <pre><code>for (int i = 0; i &lt; 5; ++i) { // do stuff } </code></pre> <p>The advantages of <code>for</code> in this example are multiple, in my opinion:</p> <ol> <li><strong>Locality</strong>: the variable i only lives in the scope of the loop</li> <li><strong>Pack</strong>: the loop 'control' is packed, so with only looking at the loop declaration I can figure if it is correctly formed (and will terminate...), assuming of course that the loop variable is not further modified within the body</li> <li>It may be inlined, though I would not always advised it (that makes for tricky bugs)</li> </ol> <p>I have a tendency therefore not to use <code>while</code>, except perhaps for the <code>while(true)</code> idiom but that's not something I have used in a while (pun intended). Even for complicated conditions I tend to stick to a <code>for</code> construct, though on multiple lines:</p> <pre><code>// I am also a fan of typedefs for (const_iterator it = myVector.begin(), end = myVector.end(); it != end &amp;&amp; isValid(*it); ++it) { /* do stuff */ } </code></pre> <p>You could do this with a <code>while</code>, of course, but then (1) and (2) would not be verified.</p> <p>I would like to avoid 'subjective' remarks (of the kind "I like for/while better") and I am definitely interested to references to existing coding guidelines / coding standards.</p> <p><strong>EDIT</strong>: </p> <p>I tend to really stick to (1) and (2) as far as possible, (1) because locality is recommended >> <a href="http://www.gotw.ca/publications/c++cs.htm" rel="nofollow noreferrer">C++ Coding Standards: Item 18</a>, and (2) because it makes maintenance easier if I don't have to scan a whole body loop to look for possible alterations of the control variable (which I takes for granted using a <code>for</code> when the 3rd expression references the loop variables).</p> <p>However, as <code>gf</code> showed below, while do have its use:</p> <pre><code>while (obj.advance()) {} </code></pre> <p>Note that this is not a rant against <code>while</code> but rather an attempt to find which one of <code>while</code> or <code>for</code> use depending on the case at hand (and for sound reasons, not merely liking).</p>
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload