Java Keyword Continue

The reserved keyword continue is used to stop executing the current iteration within a for loop or a while loop. Everything below the continue will not be executed and the execution will jump back up to the next iteration in the code.

Let’s start by defining the proper syntax for the continue statement in Java.

//JavaCodeDepot Sample

while(express is true) {

    if(condition is met) {
        continue;
    }

}


Now let’s add some more detail to our sample code that implements the continue statement using a standard for loop. Notice that only odd numbers are printed out.

//JavaCodeDepot Sample

int maxIterations = 50;

for(int ctr=0; ctr < maxIterations; ctr++) {

    if(ctr % 2 == 0 ) {
        continue;
    }
    System.out.println("Odd Number="+ctr);

}


Here is another piece of sample code that implements the continue statement using a while loop, but now we are only printing out the even numbers to the screen.

//JavaCodeDepot Sample

int maxIterations = 1;

while(maxIterations < 50) {

    if(maxIterations % 2 != 0 ) {
          maxIterations++;
          continue;
    }
    System.out.println("Even Number="+maxIterations);
            
    maxIterations++;

}


The complete list of Java reserved keywords contains a total of 50 keywords and 3 literal words that are unable to be used as variable names.

Add a Comment

Your email address will not be published. Required fields are marked *