In Java, the continue keyword is used to stop execution of a current iteration in a for loop or a while loop, then advance to the next iteration. The statements after the continue keyword won’t be executed. The syntax is as follows:

for/while (expressions) {
    // statemenst 1...
    if (condition) {
        continue;
    }
    // statements 2...
} 

The following example uses continue keyword to skip the even value of the variable i, thus the for loop will print out all the odd numbers from 1 to 100:

for (int i = 1; i < 100; i++) {
    if (i % 2 == 0) {
        continue;
    }
    System.out.println(i);
}

Similarly, the following while loop will produce a list of even numbers from 1 to 100:

int count = 1;
while (count <= 100) {
    if (count % 2 != 0) {
        count++;
        continue;
    }
    System.out.println(count);
    count++;
}

Related keywords: for, whileSee all keywords in Java.

 

Related Topics:

 

Other Recommended Tutorials:


About the Author:

is certified Java programmer (SCJP and SCWCD). He started programming with Java in the time of Java 1.4 and has been falling in love with Java since then. Make friend with him on Facebook and watch his Java videos you YouTube.



Add comment

   


Comments 

#1robin2015-11-14 08:14
please define continue keyword
Quote