C++ What is the "best" way to stop a for loop?

21,745

Solution 1

First, the loops behave sightly differently - the first one still prints number 7 while the other not.

In case you swapped the lines, it wouldn't make really difference for compiler, both solutions would be compiled in pretty much the same native code.

About the readability (and prone to errors): It depends, for such simple rule it's probably better and more readable to make it part of the condition. But when it comes more complicated, it may be worth introducing some temporary variable and then the break variant is easier to read. It's actually quite common for more complex algorithms to use break inside the loop, often even infinite loops with break inside.

Solution 2

As others have also noted, the two methods are not equivalent since one produces more outputs than another. However, I guess it's just a typo and you could have definitely identified that by running the code.

For comparison purposes, the first one is easier to generalize in the event you need to use more stopping conditions (it will be more readable than having a very long if) or do something else before stopping the loop. For instance, you might want to check first if myArray[x] == 7and then output something before breaking the loop, perhaps the reason for doing so. Therefore, the first method is better when your code is complex.

Performance-wise, these methods are quite similar.

Solution 3

The two loops are not equivalent. To make the first loop produce the same output as the second (at least, for the contents of the array you have specified) you'd need to change it to

for (int x = 0; x <  MAX_SIZE; x++)
{
     if (myArray[x] == 7)
     {
        break;
     }
     cout << myArray[x] << " ";
}

In practice, most people will find your second form more readable because there is a single obvious condition to end the loop, rather than two separate places where the loop can be terminated. And that is the one I would normally use.

The fact you think the form you had it was equivalent suggests the readability concern was affecting you. So the second form would definitely be preferable.

Share:
21,745
Galaxy
Author by

Galaxy

Updated on July 09, 2022

Comments

  • Galaxy
    Galaxy almost 2 years

    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.

    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.

    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 <  MAX_SIZE; x++) {
       if (myArray[x] == 7) {
          break;
       }
       cout << myArray[x] << “ “; // fixed
    }
    
    for (int x = 0; (x < MAX_SIZE) && (myArray[x] != 7); x++) {
       cout << myArray[x] << “ “;
    }
    
  • Galaxy
    Galaxy over 7 years
    Thank you. That form of the loop is what I really meant.