While 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. To learn about the break statement, visit Java break.
The continue statement in Java skips the current iteration of a loop (for, while, do...while, etc) and the control of the program moves to the end of the loop. And, the test expression of a loop is evaluated.
In the case of for loop, the update statement is executed before the test expression.
The continue statement is almost always used in decision-making statements (if...else Statement). It's syntax is:
continue;
How continue statement works?
Example 1: Java continue statement
class Test {
public static void main(String[] args) {
// for loop
for (int i = 1; i <= 10; ++i) {
// if value of i is between 4 and 9, continue is executed
if (i > 4 && i < 9) {
continue;
}
System.out.println(i);
}
}
}
Output:
1 2 3 4 9 10
In the above program, we are using for loop to print the value of i in each iteration. To know how for loop works, visit Java for loop. Here, notice the statement,
if (i > 5 && i < 9) {
continue;
}
This means when the value of i becomes more than 4 and less then 9, the print statement inside the loop is skipped. Hence we get the output with values 5, 6, 7, and 8 skipped.
Example 2: Java continue statement
The program below calculates the sum of 5 positive numbers entered by the user. If the user enters negative number or zero, it is skipped from the calculation.
To take input from the user, we have used the Scanner object. To learn more about Scanner, visit Java Scanner.
import java.util.Scanner;
class AssignmentOperator {
public static void main(String[] args) {
Double number, sum = 0.0;
// create an object of Scanner
Scanner input = new Scanner(System.in);
for (int i = 1; i < 6; ++i) {
System.out.print("Enter a number: ");
// takes double type input from the user
number = input.nextDouble();
// if number is negative, the iteration is skipped
if (number <= 0.0) {
continue;
}
sum += number;
}
System.out.println("Sum = " + sum);
}
}
Output:
Enter a number: 2.2 Enter a number: 5.6 Enter a number: 0 Enter a number: -2.4 Enter a number: -3 Sum = 7.8
In the above program, notice the line,
if (number < 0.0) {
continue;
}
This means when the user input negative numbers, the current iteration of the loop is skipped. And the next iteration is started.
Java continue and Nested Loop
In the case of nested loops, the continue skips the current iteration of the innermost loop.
Labeled continue Statement
Till now, we have used the unlabeled continue statement. It is used to terminate the innermost loop and switch statement. However, there is another form of continue statement in Java known as labeled contine.
We can use the labeled continue statement to terminate the outermost loop as well.
As you can see in the above image, we have used the label identifier to specify the outer loop. Now, notice how the continue statement is used (continue label;).
Here, the continue statement is skipping the current iteration of the labeled statement (i.e. outer loop). Then, the control of the program goes to the next iteration of the labeled statement (outer loop)
Example 3: labeled continue Statement
class LabeledContinue {
public static void main(String[] args) {
// the outer for loop is labeled as label
first:
for (int i = 1; i < 6; ++i) {
for (int j = 1; j < 5; ++j) {
if (i == 3 || j == 2)
// skips the iteration of label (outer for loop)
continue first;
System.out.println("i = " + i + "; j = " + j);
}
}
}
}
Output:
i = 1; j = 1 i = 2; j = 1 i = 4; j = 1 i = 5; j = 1
In the above example, the labeled continue statement is used to skip the current iteration of the loop labeled as first.
if (i==3 || j==2)
continue first;
Here, we can see the outermost for loop is labeled as first,
first:
for (int i = 1; i < 6; ++i) {..}
Hence, the iteration of the outer for loop is skipped if the value of i is 3 or the value of j is 2.
Note: The use of labeled continue is often discouraged as it makes your code hard to understand. If you are in a situation where you have to use labeled continue, refactor your code and try to solve it in a different way to make it more readable.

