Java Continue Statement

Java Continue Statement


The continue statement in Java is used to continue loop. The continue statement is used to continue the flow of the program and it skips the remaining code at a specified condition. When the continue statement is used inside the inner loop then only the inner loop continues.

The following is the syntax of the continue statement in Java:

Syntax:

continue;


Java Continue statement example:

Consider the following example in which we have used the continue statement that is based on a condition to continue the “for” loop:

CODE:

public class ExampleContinue {

public static void main (string [] args) {

for (int j = 0; j < 10; j++) {

if (j == 4) {

continue;

}

system. out. println (j);

}

}

}

OUTPUT:

0

1

2

3

4

5

6

7

8

9

In the above example, we used a ‘for’ loop in which the counter variable is initialized at 0 and the loop is executed 10 times. When the counter variable becomes equal to 4 then the loop continues and in this way we have the output of numbers from 0 to 9.


Java continue statement with inner loop:

When the continue statement is used inside the inner loop then only the inner loop will be continued.

Consider the following example in which we have used the continue statement within the inner loop of the nested loop.

CODE:

public class ExampleContinue {

public static void main (string [] args) {

for (int j = 1; j <= 3; j++) {

for (int i = 1; i <= 3; i++) {

if (j ==2 && i == 2) {

continue;

}

system. out. println (j + “ ” + i);

}

}

}

}

OUTPUT:

1 1

1 2

1 3

2 1

2 3

3 1

3 2

3 3

In the above example we used the ‘continue’ statement inside the inner loop. When the counter variables of both inner and outer loops become equal to 2 then the inner loop in continued that is the print statement is not executed then and it can be seen in the output that 2 and 2 are not printed.