I have a design

while(true){ for(;;) {} } 

How do I get out of an infinite while loop inside a for loop?

  • 3
    So goto :) - user6550
  • Another throw, C++ times in tags :) - Outtruder
  • 2
    @Outtruder setjmp is still possible - theoretically. But please, don't! - Alexei Averchenko
  • You just do not need to create two deaf cycles (and I wonder why outside the while , and inside for ?), If you are supposed to leave them once. And variables to check the conditions for continuing the cycle - the most normal way. - Outtruder
  • Old epic topic (more than 1000 posts) on this topic - rsdn.ru/forum/philosophy/57977.flat.1 - Nikolay

5 answers 5

Unfortunately, unlike Java, C and C ++ do not support a mechanism like a break label , so the most clean option is to use goto:

 int main() { while (true) { for (;;) { goto breakAll; } } breakAll: puts("I'm out!"); } 

You can also use the flag:

 int main() { bool running = true; // bool определен в stdbool.h while (running) { for (;;) { running = false; break; } } puts("I'm out!"); } 

There is no big difference between these options.

In addition to this case, goto also used in good C style for error handling:

 bars_t foo() { bar_t *bar1 = malloc(sizeof(*bar1)); if (!bar1) { goto cleanupNothing; } bar_t *bar2 = malloc(sizeof(*bar2)); if (!bar2) { goto cleanupBar1; } // ... return (bars_t) { .one = bar1, .two = bar2 }; // в обратном порядке определения cleanupBar2: free(bar2); cleanupBar1: free(bar1); cleanupNothing: return (bars_t){0}; } 

In C ++, this is not necessary, since There is RAII, but still go out of the cycles through the goto .

  • 2
    And here, by the way, a lot of tags are not necessary, because free() bursts and NULL :-) - user6550

I'll add a couple of ways.

The specified code is placed in a separate function. And now you can go through the usual return .

Method two - you just need to create a variable flag. And check it out. Somewhere so

 bool go = true; while(go){ for(;;) { if (...) { go = false; break;} } 

But this is a bad way.

  • The most normal way. - Outtruder
  • moreover, it comes out with cycles of any nesting :) - KoVadim
 try { while (true) { for (;;) { throw 1; } } } catch (int) { //вышли из двух вечных циклов одновременно } 
  • Some esoteric - by Alexei Averchenko
  • one
    Exceptions should be used for exceptional situations. - Athari
  • "Exceptions should be used for exceptional situations" - but is the way out of two eternal cycles at once is not an exceptional situation? - outcast
  • @outcast: No This is quite a common situation. Exceptional does not mean "I have never come across," which means "the program should arise as an exception, not a rule." - VladD
  • Exit from 2! infinite cycles is the rule? Wow, I never would have thought :) It’s not hard to see all the alternatives either suggest changing the cycles, which obviously contradicts the original condition (one of them ceases to be infinite), or it looks extremely doubtful (with goto). Except for the option of making the inner loop into a separate function and return false / break pairs, but this will complicate the program by adding 1 more entity to it. - outcast
 { while(1) { for(;;) { if(...) goto AWAY; } } AWAY: ; } 
     inline bool func(){ bool exit,only_break; for(;;){ if(exit){ return 1; } if(only_break){ break; } } return 0; } int main(){ while(1){ if(func()){ break; } } }