This example program teaches you how to use the Continue statements in Java programming language. There are two types of continue statements in Java, Unlabeled Continue Statement and Labeled Continue Statement. Here we will explain you both the statements with the help of example program.
Java Continue refers to Continue statement in java, used for skipping the current iteration of Boolean expression. Java Continue is a form of expression used in Boolean expression. Sometimes Java Continue is used with break statement and sometime not.
Basically Java Continue is used for stopping the execution of the program and transfers control back to start of the loop.
Moreover, you can say that the continue statement in Java lets you jump out of the current iteration of a loop and then continue with the next iteration.
Java Continue is used to control loops :- for, while and do while loop.
There are two forms of continue statement in Java.
- Unlabeled Continue Statement
- Labeled Continue Statement
Unlabeled Continue Statement in Java
This form of statement causes skips the current iteration of innermost for, while or do while loop.
Example:
public class ContinueExample { public static void main(String[] args) { System.out.println("This example prints Odd Numbers till 100"); for (int i=1; i<=100; i++) { if(i%2 == 0) continue; //Continue the loop if i is not Odd Number System.out.println(i); //Prints Odd number } } }
2. Labeled Continue Statement in Java
A label is an identifier, used for identify the execution. It is put at the start of the loop followed by a colon. Labeled Continue Statement skips the current iteration of an outer loop marked with the given label.
Example:
public class ContinueLabelExample { public static void main(String[] args) { System.out.println("This is an example of Continue Label in Java!"); int desiredcount = 100; int i=0; MAIN_LOOP: while(i<=desiredcount){ for(i=0;;i++){ if (desiredcount > 50) { break MAIN_LOOP; } desiredcount += i; } } } }