3

This will help you understand it better:

for(Object o : objects){//first loop
    for(int j = 0; i < y; j++){//second loop

        if(something_is_true)
            stop second loop , continue first loop 

    }
}

Am i supposed to use the continue keyword for that"?

user184994
  • 17,791
  • 1
  • 46
  • 52
user3535778
  • 135
  • 2
  • 15
  • You could use "while" instead of for, for the second loop – Waclock May 31 '14 at 21:24
  • 1
    add break; statement in your if condition. break always transfer control out to the current loop only. In your case if you put break statement in if block then it will always take you out to first loop. – Sumit Tyagi May 31 '14 at 21:30

8 Answers8

18

You can also use a label:

myLabel: for(Object o : objects){//first loop
    for(int j = 0; i < y; j++){//second loop

        if(something_is_true) {
            continue myLabel;
        }
    }
    //code that will be skipped if you continue to myLabel
    //but will not be skipped if you 'break' inside inner loop.
}
kajacx
  • 12,361
  • 5
  • 43
  • 70
3

No, use 'break'.

for(Object o : objects){//first loop
  for(int j = 0; i < y; j++){//second loop

    if(something_is_true)
       break;

  }
}
T.J. Crowder
  • 1,031,962
  • 187
  • 1,923
  • 1,875
Dávid Szabó
  • 2,235
  • 2
  • 14
  • 26
  • 1
    The question asked for continuing the first loop from the second loop, not breaking the second loop. This should not be the accepted answer or the question should be reformulated. (That matters in the case the first loop has some instructions after the second loop). – AW5 Aug 13 '20 at 03:05
  • I agree with @AW5 this is failing to meet the logical requirements wand will be misleading researchers. I am looking for the equivalent of `continue 2`, but it seems (after researching for a while) that javascript doesn't offer this kind of control. For anyone who does [tag:javascript] page curation on SO, this question has some duplicate page consolidation to do. – mickmackusa May 04 '21 at 00:36
  • Downvoted because this approach will still run code after the inner loop. The answer with the most votes makes this clear and should be accepted instead. – Brad Wood Aug 03 '22 at 18:20
0

You couuld use "while" instead of "for", for the second loop:

for(Object o : objects){//first loop
  int counter=0;
  while(!something_is_true && counter<y){//second loop
    counter++
    if(something happens)
      something_is_true=true;
 }
}
Waclock
  • 1,686
  • 19
  • 37
0

I think this is a better approach:

for(Object o : objects)
{
     int j = 0
     while(j < y && something_is_true)
     {
       do_whatever;
       j++;
     }
}
padawan
  • 1,295
  • 1
  • 17
  • 45
0

From Breaking out of nested loops in Java

You can use break with a label for the outer loop. For example:

public class Test {
  public static void main(String[] args) {
    outerloop:
    for (int i=0; i < 5; i++) {
      for (int j=0; j < 5; j++) {
        if (i * j > 6) {
          System.out.println("Breaking");
          break outerloop;
        }
        System.out.println(i + " " + j);
      }
    }
    System.out.println("Done");
  }
}

This prints:

0 0
0 1
0 2
0 3
0 4
1 0
1 1
1 2
1 3
1 4
2 0
2 1
2 2
2 3
Breaking
Done
Community
  • 1
  • 1
José D.
  • 4,175
  • 7
  • 28
  • 47
0

also you can use javascript labels.

its better choice if you do something after the inner loop inside each iteration of the outer loop, and you want to skip it and go to next outer loop iteration:

loop1: // an identifier to the first loop statement
for (...) {
   for (...) {
      if (...) continue loop1;
   }
   doSomething() // this also skips as well
}

but if you use break; to terminate the execution of inner loop, the part after the inner loop (doSomething() in the example above) still executes before the next iteration of outer loop start.

Javad NR
  • 11
  • 3
-1

No. Use break . That will break you out of the inner loop and you'll continue with the outer loop.

for(Object o : objects){//first loop
  for(int j = 0; i < y; j++){//second loop
    if(something_is_true) break;
  }
}
Spundun
  • 3,936
  • 2
  • 23
  • 36
  • @ Spundun: Get used to it. :-) People downvote, and people comment, and they're not usually the same people. (In fact, the closest thing to guidance you'll see from SO management and moderators is that saying why you downvoted is usually counter-productive for the site.) If you get an answer downvoted, look at it and ask why someone would, and if you don't figure it out don't worry about it. (I'm not seeing an issue with your answer, FWIW.) – T.J. Crowder Jun 01 '14 at 06:50
  • @T.J.Crowder Thanks for the support and taking the time to respond :) – Spundun Jun 01 '14 at 09:00
  • Downvoted because this approach will still run code after the inner loop. The answer with the most votes makes this clear and should be accepted instead. – Brad Wood Aug 03 '22 at 18:20
-1

If your objective is to get out of the second loop and continue the first you just need to call break. No need for continue (it's not even called).

Luis Alves
  • 1,286
  • 12
  • 32