Java Continue Statement

The Java Continue statement is one of the most useful ones that control the flow of loops. We generally use this inside the For Loop, While Loop, and Do While. While executing these, if the Javac compiler finds the Java Continue statement inside them, it will stop the current iteration and starts the new iteration from the beginning.

For example, If we have ten statements inside a loop. If we want to skip executing the second 5 statements (statement6 — statement10) when a certain condition is True; otherwise, it has to execute all the ten inside it. In this situation, we place the If clause after the 5th line, and within the if block, place the Java continue statement. If the condition is True, the Javac compiler will stop executing 6 to 10. Otherwise, it will execute 1 to 10.

Java Continue Statement examples

The syntax of the Java Continue Statement is as follows:

continue;

We want to share two examples to display the working functionality of the Java Continue statement in both For loop and the While loop.

Continue in For Loop

This program will show how to use Java Continue Statement in For Loop with an example. This program allows the user to enter any integer value. Then it will display the Even and Odd numbers inside the given range.

package ConditionalStatements;

import java.util.Scanner;

public class ContinueStatement {
	private static Scanner sc;
	public static void main(String[] args) {
		int number;
		sc = new Scanner(System.in);		
		System.out.println(" Please Enter any Integer Value: ");
		number = sc.nextInt();
		int i;
		for (i = 1; i <= number; i++)  {
			if(i % 2 != 0)  {
				System.out.format("\n Odd Numbers = %d (Skipped By Continue)\n", i);
				continue;
			}
			System.out.format(" Even Numbers = %d ",i);
		}
	}
}
Java Continue Statement 1

We will not explain the for loop in this Java continue statement example. If you do not understand Java code, please visit our article, For Loop.

  • We placed the If Statement inside the for loop to test whether (i % 2 != 0).
  • If this condition is True, the continue statement will be executed, and the iteration will stop at that number without printing the other: System.out.format(” Even Numbers = %d “,i);.
  • If the condition is false, it will skip the continue statement and print that value as output (In Our case, Even Number).

Java Continue Statement in While Loop Example

In this Java program, we will show how to use Continue Statement inside the While Loop with an example. This program allows the user to enter any integer values. Next, it uses the continue statement, displaying all the values from 0 to a given number except 4 and 8.

package ConditionalStatements;

import java.util.Scanner;

public class ContinueinWhile {
 private static Scanner sc;
 public static void main(String[] args) {
 int number;
 sc = new Scanner(System.in); 
 System.out.println("Please Enter any Integer Value below 10:");
 number = sc.nextInt();
 int i = 0;
 while (i <= number)  {
 if(i == 4 || i == 8)  {
 System.out.format("Skipped = %d \n", i);
 i++;
 continue;
 }
 System.out.format("Values = %d \n", i);
 i++;
 }
 }
}
Please Enter any Integer Value below 10: 
10
Values = 0 
Values = 1 
Values = 2 
Values = 3 
Skipped = 4 
Values = 5 
Values = 6 
Values = 7 
Skipped = 8 
Values = 9 
Values = 10 

Here we are not going to explain the While loop. If you do not understand While Loop, please visit our article Java While Loop. Inside the While loop, we placed the If condition to test whether i is equal to 4 or 8.

  • If this condition is True, the continue statement will be executed, and the iteration will stop at that number without printing the other: System.out.format(” Values = %d \n”, i);. For better understanding, we placed System.out.format(” Skipped = %d \n”, i); inside the If condition. So, that value is printed from this message whenever the iteration breaks.
  • If the condition is false, it will skip the continue statement and print that number as output (In Our case, 0,1,2,3,4,6,7,8,10).