Java Continue Statement

Suppose you are working with loops. It is sometimes desirable to skip some statements inside the loop or terminate the loop immediately without checking the test expression.

In such cases, break and continue statements are used. Visit this page to learn about break statement in Java.

The continue statement skips the current iteration of a loop (for, while, and do...while loop).

When continue statement is executed, control of the program jumps to the end of the loop. Then, the test expression that controls the loop is evaluated. In case of for loop, the update statement is executed before the test expression is evaluated.

The continue statement is used in loop control structure when you need to immediately jump to the next iteration of the loop. It can be used with for loop or while loop.

The Java continue statement is used to continue loop. It continues the current flow of the program and skips the remaining code at specified condition. In case of inner loop, it continues only inner loop.

It is almost always used with decision making statements (if...else Statement).

Syntax:
jump-statement; continue;
Java Continue Statement Example
Example:
public class ContinueExample { public static void main(String[] args) { for(int i=1;i<=10;i++) { if(i==5){ continue; } System.out.println(i); } } }


Output:
1 2 3 4 6 7 8 9 10
import java.util.Scanner; public class ContinueExample { public static void main(String[] args) { int m,n; Scanner p=new Scanner(System.in); System.out.println("Enter number of elements:"); m=p.nextInt(); System.out.println("Which number is skyp for natural numbers and number enter less then m value:"); n=p.nextInt(); for(int i=1;i<=m;i++) { if(i==n) { continue; } System.out.println(i); } } }


Output:
Enter number of elements: 15 Which number is skyp for natural numbers and number enter less then m value: 7 1 2 3 4 5 6 8 9 10 11 12 13 14 15
Java Continue Statement with Inner Loop
public class ContinueExampleInner { public static void main(String[] args) { for(int i=1;i<=3;i++) { for(int j=1;j<=3;j++) { if(i==2&&j==2) { continue; } System.out.println(i+" "+j); } } } }


Output:
1 1 1 2 1 3 2 1 2 3 3 1 3 2 3 3
import java.util.Scanner; public class ContinueExample { public static void main(String[] args) { int m,n,q,r; Scanner p=new Scanner(System.in); System.out.println("Enter number of elements:"); m=p.nextInt(); System.out.println("Enter number of elements:"); n=p.nextInt(); System.out.println("Which number is skyp for natural numbers and number enter less then m value:"); q=p.nextInt(); System.out.println("Which number is skyp for natural numbers and number enter less then n value:"); r=p.nextInt(); for(int i=1;i<=m;i++){ for(int j=1;j<=n;j++){ if(i==q&&j==r){ continue; } System.out.println(i+" "+j); } } } }


Output:
Enter number of elements: 10 Enter number of elements: 5 Which number is skyp for natural numbers and number enter less then m value: 7 Which number is skyp for natural numbers and number enter less then n value: 3 1 1 1 2 1 3 1 4 1 5 2 1 2 2 2 3 2 4 2 5 3 1 3 2 3 3 3 4 3 5 4 1 4 2 4 3 4 4 4 5 5 1 5 2 5 3 5 4 5 5 6 1 6 2 6 3 6 4 6 5 7 1 7 2 7 4 7 5 8 1 8 2 8 3 8 4 8 5 9 1 9 2 9 3 9 4 9 5 10 1 10 2 10 3 10 4 10 5



Instagram