2

Here is the structure of my program:

for(loop1){

  if(condition1)
  {
    for(loop2)
    {
       for(loop3)
       {
         if(condition1_3)
           {
            As condition1_3 is true continue with loop2's next iteration, 
            No need to execute loop3's remaining iteration  
           }
           else
           {
             As condition1_3 is false no need to execute remaining part of loop3 and
             loop 2 but continue with loop1's remaining part i.e. condition1_1
           }
        }
     }
  }
  if(condition1_1)
  {
  some code here
  }
}

I know I have to use continue and break statement but don't understand exactly how? Please tell me how to achieve this mechanism?

newfurniturey
  • 37,556
  • 9
  • 94
  • 102
DCoder
  • 3,486
  • 7
  • 36
  • 68
  • Its not great practice but you can use a named break. I'll just lookup the syntax – Richard Tingle Jul 09 '14 at 12:48
  • This should give you what you need: [Breaking out of nested loops in Java](http://stackoverflow.com/questions/886955/breaking-out-of-nested-loops-in-java) – Richard Tingle Jul 09 '14 at 12:48
  • 1
    Your logic doesn't make sense... it seems loop 3 can only ever have 1 iteration so it doesn't make sense to have a 3rd loop at all – musefan Jul 09 '14 at 12:51
  • basically in any iteration of loop3, you go through a block that says `No need to execute loop3's remaining iteration`... – njzk2 Jul 09 '14 at 12:52
  • 1
    I know you don't wait for such clever comments, but I really recommend to try to avoid these structures. They will bite you once at 2a.m., when you are under pressure and tired try to solve a problem there. Try to get rid of the condition1 by whatever guardian is possible and to outsource loop3 into an own function with a speaking name. It will be smoothly readable with a single glance. (end of Sunday's Word ^^) – peter_the_oak Jul 09 '14 at 12:55

7 Answers7

2

If I understand your question, you could use the labeled continue,

loop1: for (;;) {
  if (condition1) {
    loop2: for (;;) {
      loop3: for (;;) {
        if (condition1_3) {
          continue loop2;
        } else {
          continue loop1;
        }
      }
    }
  }
}
Elliott Frisch
  • 198,278
  • 20
  • 158
  • 249
1

You can do as follows:

for(loop1){

  if(condition1)
  {
    for(loop2)
    {
       boolean flag=false;
       for(loop3)
       {
         if(condition1_3)
           {
            break; 
           }
           else
           {
             flag=true;
             break;
           }
        }
        if(flag)
          break;
     }
  }
  if(condition1_1)
  {
  some code here
  }
}
G.S
  • 10,413
  • 7
  • 36
  • 52
0

To break out and not continue the loop3 use break; if (condition1_3) break; The break; statement only breaks out of the current loop i think, not the loop it is nested in.

Edit: Missread the question To break out of both loops you can make a boolean flag before both loops and at the end of both loops have an if (breakOut) break; This will solv the question

Alexander Sagen
  • 127
  • 1
  • 12
0

Do like the following -

for(loop1){
  boolean flag = false;

  if(condition1)
  {
    for(loop2)
    {
       if(flag == true)
         break;

       for(loop3)
       {
         if(condition1_3)
           {    
             break;
           }
           else
           {
              flag = true;
              break;
           }
        }
     }
  }
  if(condition1_1)
  {
  some code here
  }
}
sjain
  • 23,126
  • 28
  • 107
  • 185
0

Have a exit flags which is set when you want to exit a particular loop and check in each respective loops whether to continue or exit.

boolean exitLoop2 = false;

for(loop1){

 if(condition1)
 {
    for(loop2)
    {
       for(loop3)
       {
         if(condition1_3)
         {
             //As condition1_3 is true continue with loop2's next iteration, 
             //No need to execute loop3's remaining iteration  
             break;
         }
         else
         {
            //As condition1_3 is false no need to execute remaining part of loop3 and
            //loop 2 but continue with loop1's remaining part i.e. condition1_1
            exitLoop2 = true;
            break;
          }

       }
       if(exitLoop2)
       {
           break;
       }
    }
 }
 if(condition1_1)
 {
    some code here
 }

}

vinayknl
  • 1,242
  • 8
  • 18
0

An option would be to use variables - but you have to think about correct setting of values:

boolean runFor1 = true;
boolean runFor2 = true;
boolean runFor3 = true;
for(loop1 && runFor1)
{
    for(loop2 && runFor2)
    {
       for(loop3 && runFor3)
       {
           setRunForX here to skip/unskip loops
       }
     }
  }
}
SUhrmann
  • 632
  • 1
  • 8
  • 26
  • This won't allow for breaking out of loop 2 so as to avoid the rest of the code in loop 2 itteration, as appears to be the request – musefan Jul 09 '14 at 12:57
0

I'm not really shure what do you mean exactly. For me it simply looks like you want to jump out of the loops if a specific condition is satisfied. So you can either can define a label for your loops and use the continue labelLoopX; statement or you can satisfy the exit condition of loop3 if condition1_3==true resp. the exit condition of loop3 and loop2 if condition1_3==false. e.g. if loop3 looks like for ( int i = 0; i < 10; i++ ) like this:

if(condition1_3){
     //do something important
     i=10; //sets i to 10 and condition i < 10 is unsatified
}
else{
     //do some other important stuff
     i=10; //satify loop3's exitcondition
     j=10; //satify loop2's exitcondition
}
dormux
  • 76
  • 3