C++ – How to break out of nested loops in C++?

breakc

I am a beginner in C++, and I am wondering how to break out of nested loops. Is there a break(2)?

#include <iostream>

using namespace std;

int main() {
    for (int x = 5; x < 10; x++) {
        for (int j = 6; j < 9; j++) {
            for (int b = 7; b < 12; b++) {
                // Some statements
                // Is break(2) right or wrong
                // or can I use 'break; break;'?
            }
        }
    }
}

Best Answer

You can use goto. It's essentially the same function

#include <iostream>

using namespace std;

int main() {
    for(int x = 5; x < 10; x++) {
        for(int j = 6; j < 9; j++) {
            for(int b = 7; b < 12; b++) {
                if (condition)
                    goto endOfLoop;
            }
        }
    }

    endOfLoop:
    // Do stuff here
}