0

Say we have following code and when break we want to break out of the inner and outer loop instead of just the inner loop and go directly to blablabla. How can we do this in C++?

for (int i = 0; i < m; i++) {
    for (int j = 0; j < n; j++) {
         if (some condition) {
             // Do something and break...
             break; // Breaks out of the inner loop
         }
    }
}

blablabla
...
user3692521
  • 2,563
  • 5
  • 27
  • 33
  • 3
    My heretic answer would be **[goto](http://c2.com/cgi/wiki?StructuredProgrammingWithGoToStatements)**. Now grep your torches and forks and kill me ;-) – Oncaphillis Nov 11 '14 at 18:59
  • Could you give a more concrete example? I've never seen a case where you would want to break out of multiple loops, at least not in properly written code. Most of the examples I've seen are a result of trying to do too much in a single function. – James Kanze Nov 11 '14 at 19:34

7 Answers7

2

"Go directly to [...]" sound like a perfect job for goto. And it is !

for (int i = 0; i < m; i++) {
    for (int j = 0; j < n; j++) {
         if (some condition) {
             // Do something and break...
             goto afterLoop; // Breaks out of both loops
         }
    }
}
afterLoop:
// More stuff
Quentin
  • 62,093
  • 7
  • 131
  • 191
  • 2
    Why should we add unnecessary branches and complicated loop conditions, when we have a tool that does exactly what we need with minimal work? This is the optimal way to do it. It is efficient, and the code's purpose and execution is clear to understand. – Serge Nov 11 '14 at 19:10
  • 1
    @Serge all that common sense won't hold back all of `goto`'s detractors. Dogmas. – Quentin Nov 11 '14 at 19:16
1

There are two ways to solve it:

Solution 1: use boolean confition instead of a second for loop

for (int i = 0; i < m; i++) {
int j = 0;
bool conditionOut=false;
    while (!conditionOut && j<n)
    {
       if (some condition)
       {
          conditionOut =true;
          break;
       }
    }
}

Solution 2: use goto (Stroustrup mentions it as onle of the few examples where goto is useful in C++)

Gabriel
  • 3,564
  • 1
  • 27
  • 49
0

You could build in a variable check in the outer loop, lets say:

bool end;

In the inner loop, at some point, you want to break out and set end to true. You also need a if-condition.

hazzard
  • 151
  • 1
  • 1
  • 5
0

You can use "goto" for this purposes.

0

For example

for ( int i = 0; i < m; i++ ) 
{
    bool exit = false;

    for ( int j = 0; !exit && j < n; j++ ) 
    {
         if ( ( exit = some condition ) ) 
         {
             // Do something and break...
         }
    }

    if ( exit ) break;
}

Or

bool exit = false;

for ( int i = 0; !exit && i < m; i++ ) 
{
    for ( int j = 0; !exit && j < n; j++ ) 
    {
         if ( ( exit = some condition ) ) 
         {
             // Do something and break...
         }
    }
}
Vlad from Moscow
  • 301,070
  • 26
  • 186
  • 335
0

Use another int variable.

for (int i = 0, Out = 0; i < m && Out == 0; i++) {
    for (int j = 0; j < n && Out == 0; j++) {
         if (some condition) {
             Out = 1;
         }
    }
}
MKAROL
  • 316
  • 3
  • 11
0

You could extract the loops out to a separate function and use return.

void doTheLoop(int n, int m) {
    for (int i = 0; i < m; i++) {
        for (int j = 0; j < n; j++) {
             if (some condition) {
                 // Do something and break...
                 return;
             }
        }
    }
}

doTheLoop(n, m);
//blablabla ...
Chris Drew
  • 14,926
  • 3
  • 34
  • 54