GoTo Next Iteration in For Loop in java

JavaFor Loop

Java Problem Overview


Is there a token in java that skips the rest of the for loop? Something like VB's Continue in java.

Java Solutions


Solution 1 - Java

continue;

continue; key word would start the next iteration upon invocation

For Example

for(int i= 0 ; i < 5; i++){
 if(i==2){
  continue;
 }
System.out.print(i);
}

This will print

0134

See

Solution 2 - Java

Try this,

  1. If you want to skip a particular iteration, use continue.

  2. If you want to break out of the immediate loop, use break

  3. If there are 2 loop, outer and inner.... and you want to break out of both the loop from the inner loop, use break with label (another question about label).


Example:

continue

for(int i=0 ; i<5 ; i++){

    if (i==2){
   
      continue;
    }
 }

break

for(int i=0 ; i<5 ; i++){
    
        if (i==2){
       
          break;
        }
     }

break with label

lab1: for(int j=0 ; j<5 ; j++){
     for(int i=0 ; i<5 ; i++){
    
        if (i==2){
       
          break lab1;
        }
     }
  }
     

Solution 3 - Java

If you want to skip current iteration, use continue;.

for(int i = 0; i < 5; i++){
    if (i == 2){
        continue;
    }
 }

Need to break out of the whole loop? Use break;

for(int i = 0; i < 5; i++){
    if (i == 2){
        break;
    }
}

If you need to break out of more than one loop use break someLabel;

outerLoop:                                           // Label the loop
for(int j = 0; j < 5; j++){
     for(int i = 0; i < 5; i++){
        if (i==2){
          break outerLoop;
        }
     }
  }

*Note that in this case you are not marking a point in code to jump to, you are labeling the loop! So after the break the code will continue right after the loop!

When you need to skip one iteration in nested loops use continue someLabel;, but you can also combine them all.

outerLoop:
for(int j = 0; j < 10; j++){
     innerLoop:
     for(int i = 0; i < 10; i++){
        if (i + j == 2){
          continue innerLoop;
        }
        if (i + j == 4){
          continue outerLoop;
        }
        if (i + j == 6){
          break innerLoop;
        }
        if (i + j == 8){
          break outerLoop;
        }
     }
  }

Solution 4 - Java

As mentioned in all other answers, the keyword continue will skip to the end of the current iteration.

Additionally you can label your loop starts and then use continue [labelname]; or break [labelname]; to control what's going on in nested loops:

loop1: for (int i = 1; i < 10; i++) {
    loop2: for (int j = 1; j < 10; j++) {
        if (i + j == 10)
            continue loop1;

        System.out.print(j);
    }
    System.out.println();
}

Solution 5 - Java

Use the continue keyword. Read here.

>The continue statement skips the current iteration of a for, while , or do-while loop.

Solution 6 - Java

use continue keyword .

EX:

for(int i = 0; i < 10; i++){
  if(i == 5){
    continue;
   }
}

Attributions

All content for this solution is sourced from the original question on Stackoverflow.

The content on this page is licensed under the Attribution-ShareAlike 4.0 International (CC BY-SA 4.0) license.

Content TypeOriginal AuthorOriginal Content on Stackoverflow
Questionuser1277170View Question on Stackoverflow
Solution 1 - JavajmjView Answer on Stackoverflow
Solution 2 - JavaKumar Vivek MitraView Answer on Stackoverflow
Solution 3 - JavananaView Answer on Stackoverflow
Solution 4 - JavaWormboView Answer on Stackoverflow
Solution 5 - JavaKazekage GaaraView Answer on Stackoverflow
Solution 6 - JavaSumit SinghView Answer on Stackoverflow