1

Is there any way to break this without if/else conditionals for each layer?

#include <iostream>

using namespace std;

int main()
{
    for (int i = 0; i < 20; i++)
    {
        while (true) 
        { 
            while (true) 
            { 
                break; break; break; 
            } 
        }
    }
    
    cout << "END";

    return 0;
}
Andreas Wenzel
  • 22,760
  • 4
  • 24
  • 39
  • Why do you have 3 breaks inside the same loop? You just need 1 for each loop. – Jason Sep 02 '22 at 01:34
  • You could use `goto`, but you are probably better off rethinking your design. – jkb Sep 02 '22 at 01:34
  • Whenever you find yourself in such a situation, the real question to ask is: why is my logic is so convoluted that something like that is needed. This is an indication that things should be broken up and refactored, to simplify the logic to the point that this is no longer needed. – Sam Varshavchik Sep 02 '22 at 01:45
  • 2
    Though the above is just an example, I don't agree. I'm pretty comfortable with flow control, and things often require 3 layers, especially in gamedev... Heck, a simple 2D scan requires two... – user19816087 Sep 02 '22 at 02:50

3 Answers3

3

You can wrap the logic in a function or lambda.

Instead of break; break; break; (which won't work) you can return;.

#include <iostream>
using namespace std;
int main()
{
    auto nested_loops = []
    {
        for (int i = 0; i < 20; i++)
        {
            while (true) 
            { 
                while (true) 
                { 
                    // Done with all the looping
                    return;
                } 
            }
        }
    };

    nested_loops();
    cout << "END";
    return 0;
}

Or (same effect, different style)

#include <iostream>
using namespace std;
int main()
{
    [] {
        for (int i = 0; i < 20; i++)
        {
            while (true) 
            { 
                while (true) 
                { 
                    // Done with all the looping
                    return;
                } 
            }
        }
    } ();

    cout << "END";
    return 0;
}
Drew Dormann
  • 59,987
  • 13
  • 123
  • 180
  • 2
    You can do `[]{ ... }();` then you do not need to give it a name – Slava Sep 02 '22 at 01:50
  • @Slava yep! I debated that style. Or `std::invoke( []{...} );` if you prefer to know _immediately_ that the lambda will be invoked immediately, Styles vary. – Drew Dormann Sep 02 '22 at 01:53
0

If you want to break individual loop, then you can use break in their respect loop.

Putting too many or single break within a loop, will only break that loop that it is within it.


#include <iostream>
using namespace std;
int main()
{
    [] {
        for (int i = 0; i < 20; i++)
        {
            while (true) 
            { 
                while (true) 
                { 
                    break;
                } 
                break;
            }
            break;
        }
    } ();

    cout << "END";
    return 0;
}

Jitu DeRaps
  • 144
  • 1
  • 9
0

In order to break out of a nested loop, you can use the goto statement.

#include <iostream>

using namespace std;

int main()
{
    for (int i = 0; i < 20; i++)
    {
        while (true) 
        { 
            while (true) 
            { 
                goto break_outer_loop;
            } 
        }
    }

break_outer_loop:
    
    cout << "END";

    return 0;
}

Note that goto should generally be avoided. However, for breaking out of a nested loop, it is generally considered acceptable.

Andreas Wenzel
  • 22,760
  • 4
  • 24
  • 39