title: For loop construction and code complexity
body: <p>My group is having some discussion and strong feelings about for loop construction.</p>
  
  <p>I have favored loops like:</p>
  
  <pre><code>   size_t x;
     for (x = 0; x &lt; LIMIT; ++x) {
        if (something) {
            break;
        }
        ...
     }
  
     // If we found what we're looking for, process it.
     if (x &lt; LIMIT) {
         ...
     }
  </code></pre>
  
  <p>But others seem to prefer a Boolean flag like:</p>
  
  <pre><code>   size_t x;
     bool found = false;
     for (x = 0; x &lt; LIMIT &amp;&amp; !found; ++x) {
        if (something) {
            found = true;
        }
        else {
            ...
        }
     }
  
     // If we found what we're looking for, process it.
     if (found) {
         ...
     }
  </code></pre>
  
  <p>(And, where the language allows, using "for (int x = 0; ...".)</p>
  
  <p>The first style has one less variable to keep track of and a simpler loop header.  Albeit at the cost of "overloading" the loop control variable and (some would complain), the use of break.</p>
  
  <p>The second style has clearly defined roles for the variables but a more complex loop condition and loop body (either an else, or a continue after found is set, or a "if (!found)" in the balance of the loop).</p>
  
  <p>I think that the first style wins on code complexity.  I'm looking for opinions from a broader audience.  Pointers to actual research on which is easier to read and maintain would be even better.  "It doesn't matter, take it out of your standard" is a fine answer, too.</p>
  
  <p>OTOH, this may be the wrong question.  I'm beginning to think that the right rule is "if you have to break out of a for, it's really a while."</p>
  
  <pre><code>bool found = false;
  x = 0;
  while (!found  &amp;&amp; x &lt; LIMIT) {
      if (something) {
          found = true;
          ...handle the thing...
      }
      else {
          ...    
      }
      ++x;
  }
  </code></pre>
  
  <p>Does what the first two examples do but in fewer lines.  It does divide the initialization, test, and increment of x across three lines, though.</p>