Java Continue Statement

The Java Continue statement is one of the most useful one 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 examples

The syntax of the Continue Statement in Java Programming language is as follows:

continue;

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

Java Continue in For Loop

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

// Java Continue Statement
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

In this Java continue statement example, we are not going to explain the for loop. If you do not understand Java code, then please visit our article For Loop.

  • Inside the for loop we placed If Statement to test whether (i % 2 != 0).
  • If this condition is True, then the continue statement 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, then it will skip the Java continue statement and prints 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 Java Continue Statement inside the While Loop with example. This program allows the user to enter any integer values. Next, it uses the continue statement, and it will display 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, then please visit our article Java While Loop. Inside the While loop, we placed If condition to test whether i is equal to 4 or 8.

  • If this condition is True, the Java 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, whenever the iteration break, that value be printed from this message.
  • If the condition is false, then it will skip the continue statement and prints that number as output (In Our case 0,1,2,3,4,6,7,8,10).