Java continue Statement
Overview
In this tutorial, we will learn about the Java continue statement. When a continue statement is executed the loop iteration is skipped.
continue statement
The continue statement is used in iteration loops. When the continue statement is encountered, the remaining part of the loop body in the current iteration is not executed and the control moves to the next iteration. continue is used to skip iterations in a loop if a specific condition is satisfied.
Example
Let’s see an example of the continue statement in the while loop. In the while/do while loop, the control moves to the condition part on encountering the continue statement. We will illustrate the use of the continue statement through the following code listing:
/* * continue statement demo * Java Tutorials - www.TestingDocs.com * */ public class ContinueDemo { public static void main(String[] args) { int i = 0; while (i < 10) { i++; if (i == 5) { // skip when i is 5 continue; } System.out.println("i= "+ i); } } }
Program Output
i= 1
i= 2
i= 3
i= 4
i= 6
i= 7
i= 8
i= 9
i= 10
Another Example
Let’s use the continue statement inside a for loop. For example, in the below program, we will skip the for loop when i is an even number. We will illustrate the use of the continue statement through the following code listing.
public class ContinueDemo { public static void main(String[] args) { // TODO Auto-generated method stub for (int i = 1; i < 10; i++) { if (i % 2 ==0) { continue; // This will skip when i is even } System.out.println("i=" +i); } } }
That’s it. A similar statement the break statement is used to terminate the entire loop.
—
Java Tutorials
Java Tutorial on this website:
https://www.testingdocs.com/java-tutorial/
For more information on Java, visit the official website :