English | 简体中文 | 繁體中文 | Русский язык | Français | Español | Português | Deutsch | 日本語 | 한국어 | Italiano | بالعربية
In this tutorial, you will learn about the continue statement in Java and the statements marked as continue with the help of examples.
When dealing with loops, sometimes it is necessary to skip certain statements in the loop or immediately terminate the loop without checking the test expression.
In this case, break and continue statements will be used. To understand the break statement, please visitJava break.
The continue statement in Java skips the current iteration of the loop (for, while, do…while, etc.), and the control of the program moves to the end of the loop. Then it calculates the test expression of the loop.
For the for loop, the update statement is executed before the test expression.
The continue statement is almost always used in (if ... else statementsIt is used in decision statements. Its syntax is:
continue;
class Test { public static void main(String[] args) { // for loop for (int i = 1; i <= 10; ++i) { //If the value of i is in4and9between, then execute continue if (i > 4 && i < 9) { continue; } System.out.println(i); } } }
Output:
1 2 3 4 9 10
In the above program, we use a for loop to print the value of i at each iteration. To understand how the for loop works, please visitJava for Loops. Please note the following statements here:
if (i > 5 && i < 9) { continue; }
This means when the value of i is greater than4less than9When, it will skip the print statement in the loop. Therefore, we will skip the value5、6、7and8output.
The following program calculates the sum of5the sum of positive numbers. If the user enters a negative number or zero, it will be skipped from the calculation.
To accept user input, we use this Scanner object. For more information about Scanner, please visitJava Scanner.
import java.util.Scanner; class AssignmentOperator { public static void main(String[] args) { Double number, sum = 0.0; //Create Scanner object Scanner input = new Scanner(System.in); for (int i = 1; i < 6; ++i) { System.out.print("Enter a number: "); //Accept double type data input number = input.nextDouble(); //Skip iteration if number is negative 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, please note the following lines:
if (number < 0.0) { continue; }
This means that when the user enters a negative number, the current iteration of the loop will be skipped. The next iteration begins.
IfNested loopsThen the continue statement skips the current iteration of the innermost loop.
So far, we have used unlabeled continue statements. It is used to terminate the innermost loop and switch statements. However, there is another form of continue statement in Java, called labeled continue.
As shown in the figure above, we use the label identifier to specify the outer loop. Now, please note how to use the continue statement (continue label;).
Here, the continue statement skips the current iteration of the labeled statement (i.e., the outer loop). Then, the program control transfers to the next iteration of the labeled statement (outer loop).
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) //Skipping the iteration of the labeled loop (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 current iteration of the loop marked as first is skipped using the labeled continue statement.
if (i ==3 || j ==2) continue first;
Here, we can see that the outermost for loop is marked as first,
first: for (int i = 1; i < 6; ++i) {..}
Therefore, if i is equal to3or j is equal to2then skip the iteration of the outer for loop.
NoteIt is generally not recommended to use labeled continue as it makes the code difficult to understand. If you are in a situation where you must use labeled continue, please refactor the code and try to solve it in a different way to make it more readable.