nima tajfar
nima tajfar

Reputation: 85

how break loop inside another loop

if there is 3 loop inside each other how could i break to upper level loop i mean:

there is

while (abc) {
    for (int dep =0 ; dep<b ; dep++)  {
         for (int jwe=0 ; jwe<g ; jwe++) {
             if (! (ef || hf) ) {
             //here is where i want to break to loop while
             //or in other purpose (other situation) i need 
             //to know how i could break to first loop  
             //i mean (for (int dep =0 ; dep< b ; dep++)
             }
         }
    }
} 

Would somebody please help me , how after if , i could break to the while loop , or how i could break to first loop "for" .

Upvotes: 1

Views: 1853

Answers (6)

vonbrand
vonbrand

Reputation: 11791

This is one of the (rare) cases in which goto is the clearest construct:

while (abc) {
    for (int dep =0 ; dep<b ; dep++)  {
         for (int jwe=0 ; jwe<g ; jwe++) {
             if (! (ef || hf) ) {
                 // Do anything needed before leaving "in the middle"
                 goto out;
             }
         }
    }
}
out:
// Continue here

Make sure the indentation doesn't hide the label.

Upvotes: 2

Anirudh Ramanathan
Anirudh Ramanathan

Reputation: 46728

Just set the counter of the outer loop to a value such that it wont run again.

while (abc) {
    for (int dep =0 ; dep<b ; dep++)
    for (int jwe=0 ; jwe<g ; jwe++)
     if (! (ef || hf) ) {
         //here is where you want to break to the while-loop
         //abc = 0; here will make it exit the entire while as well
         dep = b; //in order to exit the first for-loop
         break;
     }
} 

Upvotes: 4

Fred
Fred

Reputation: 8602

continue_outer_loop = true;
while (abc) {

  for ( int i = 0; i < X && continue_outer_loop; i++) {
    for ( int j = 0; j < Y; j++ {

       if (defg) {
         continue_outer_loop = false;  // exit outer for loop
         break;                        // exit inner for loop
       }

    }
  }
}

Upvotes: 0

Varaquilex
Varaquilex

Reputation: 3463

Use an int variable, int terminate = 0; for example and place it in while loop along with your condition while(true && terminate == 0). When you want to break outer loop, set the variable to 1 before breaking from the inner loops.

Upvotes: 0

Nanhe Kumar
Nanhe Kumar

Reputation: 16297

int breakForLoop=0;
    int breakWhileLoop=0;
    while (abc) {

        for (int dep = 0;dep < b;dep++) {

            for (int jwe = 0;jwe < g; jwe++) {


                if (!(ef || hf)) {
                    breakForLoop=1;
                    breakWhileLoop=1;
                    break;

                }
            }
            if(breakForLoop==1){
                break;
            }
        }
        if( breakWhileLoop==1){
                break;
            }
    } 

Upvotes: 0

Hunter McMillen
Hunter McMillen

Reputation: 61512

Some languages (Java included) support breaking to labels:

outerLoop: // a label
while(true) {
   for(int i = 0; i < X; i++) {
      for(int j = 0; j < Y; j++) {
         // do stuff 
         break outerLoop; // go to your label
      }
   }
}

Upvotes: 0

Related Questions