title: C++ What is the "best" way to stop a for loop?
body: <p>Let's say that you want to make a for loop in C++, but in addition to the determined condition you also know when, or if the loop should stop. Note that such a condition does not have to be necessarily hard-coded, nor should the loop have such a determining value all the time. This is just an example, perhaps useful for random loops, or ones who's contents are determined during run time. Let's keep this example simple and focus on a solution that could be applied to any such related task in programming.</p>
  
  <p>I would like to compare these two solutions. The first one uses a breaks statement and the second one uses a multiple or combined condition. Which one is the "best" or "right" way to accomplish the task, in terms of efficiency or reliability. Which one is less prone to bugs? Fell free to suggest another solution.</p>
  
  <pre><code>const int MAX_SIZE = 10;
  int myArray[MAX_SIZE] = {1, 2, 3, 4, 5, 6, 7, 8, 9, 10};
  
  // two ways to stop a loop
  
  for (int x = 0; x &lt;  MAX_SIZE; x++) {
     if (myArray[x] == 7) {
        break;
     }
     cout &lt;&lt; myArray[x] &lt;&lt; “ “; // fixed
  }
  
  for (int x = 0; (x &lt; MAX_SIZE) &amp;&amp; (myArray[x] != 7); x++) {
     cout &lt;&lt; myArray[x] &lt;&lt; “ “;
  }
  </code></pre>