How to break multiple repetitive statements at the same time

Asked 2 years ago, Updated 2 years ago, 121 views

From the source code below, Is there a way to get out of the overlapped repeat door at once using break?

int main(){
    for(int i=0; i<A; i++){
        //...
        for (int j=0 ; j<B; j++) {
            //...
            for (int k=0; k<C; k++) {
                if (D) {
                    break; // here
                }
                //...
            }
            //...
        }
        //...
    }
}

c++ for-loop break nested-loops

2022-09-22 22:25

1 Answers

In Java or other languages, you can name loop and solve it C++ doesn't have that capability.

It can't be a break You can solve it by writing goto or by creating flag.

The code below is to create endflag to exit the repeated statement overlapped with conditional statements

int main(){
    bool endflag = 0;
    for(int i=0; i<A; i++)
    {
        for (int j=0 ; j<B; j++)
        {
            if (C) {
                endflag = 1;
                break;
            }
        }
        if (endflag) {
            break;
        }
    }
}


2022-09-22 22:25

If you have any answers or tips


© 2024 OneMinuteCode. All rights reserved.