What is a continue statement in Java and how to use it?



This article will help you understand what the continue statement in Java Programming language is.

Continue Statement

The statement continue, is just the opposite of Break statement. As soon as the continue statement is executed in a loop, the control skips rest of the statements for that value and resumes for the next iteration.

Syntax

while (condition){
   Statement 1;
   _________________;
   _________________;
   if (another condition is true){
      continue;
   }
   Statement 2;
   _________________;
   _________________;
}

Given below are some examples where continue statement is used.

Example 1 − Continue Statement with Loop

public class ContinueExample1 { // class declaration public static void main(String[] args) { // main function declaration int i; // variable declaration for (i = 0; i <= 5; i++){ // for loop to increment value of i by 1 if ( i == 2) { // checking if value of i is equal to 2 continue; // continue statement will skip the remaining statements and take the control to the next iteration } System.out.println(i); // printing value of i in each iteration } } }

Output

0
1
3
4
5

In this code, the goal is to print from 0 to 5 without the number 2. The for loop is given so that the value of variable i is increased by 1 every loop, i.e. i = 0 in first loop, i = 1 in 2nd loop and so on. Now we want to avoid 2 from printing. For that an if statement is added which checks whether the value of variable i has reached 2 or not. After reaching, the continue statement is executed, which skips the remaining statements (here the System.out.println line) of the for loop and continues to the next loop. Now value of variable i is 3 and so the printing continues. So now the output becomes 0 1 3 4 5.

Example 2 − Continue Statement with Inner Loop

public class ContinueExample2 { // class declaration public static void main(String[] args) { // main function declaration int i,j; // variable declaration for (i = 0; i <= 5; i++) { // for loop to increment value of i by 1 for (j = 0; j <= 5; j++) { // for loop to increment value of j by 1 if ( i == 2 && j == 2) { // checking if value of i and j is 2 continue; // continue statement will skip the remaining statements and take the control to the next iteration } System.out.println(i + " " + j); // printing value of i and j on each loop } } } }

Output

0 1
0 2
0 3
0 4
0 5

1 0
1 2
1 3
1 4
1 5

2 0
2 1
2 3
2 4
2 5

3 0
3 1
3 2
3 4
3 5

4 0
4 1
4 2
4 3
4 5

5 0
5 1
5 2
5 3
5 4

In this code, the goal is to display the value of two for loop variables side by side excluding times when both variables are equal, i.e this code will display {1, 2}, {2, 3}, {2,4}, etc. but not print {1, 1}, {2, 2}, {3, 3}, etc. In order to achieve this, a continue statement has been added inside the inner loop. Why is it added to the inner loop, because for every single iteration of the outer loop, the inner loop iterates 6 times in this code. So if we had put the continue statement outside the inner loop, then only {5,5} would have been excluded. Now when both the for loop variables are same, like when both i and j are 2, the continue statement skips the print line and moves to the next inner loop iteration.

Example 3 − Continue Statement with Labelled For Loop

public class ContinueExample3 { // class declaration public static void main(String[] args) { // main function declaration Label1: // A label is a component for placing a text in a container (in here, a for loop block) for (int i = 0; i <= 5; i++) { // for loop to increment value of i by 1 Label2: for (int j = 0; j <= 5; j++) { // for loop to increment value of j by 1 if (i == 3 && j == 3) { // checking if value of i and j is 3 continue Label1; // if yes, then continue is applied which skips the remaining statements and take the control to the next iteration } System.out.println(i + " " + j); // printing value of i and j on each loop } System.out.println(); // printing next line } } }

Output

0 0
0 1
0 2
0 3
0 4
0 5

1 0
1 1
1 2
1 3
1 4
1 5

2 0
2 1
2 2
2 3
2 4
2 5

3 0
3 1
3 2

4 0
4 1
4 2
4 3
4 4
4 5

5 0
5 1
5 2
5 3
5 4
5 5

This code of a simple demonstration of what happens when there exists a huge number of nested for loops in a code, and you want to skip a particular for loop during some condition checking. For that, we add label to every for loop, i.e. naming them individually. Now if one needs to skip the statements of a particular for loop, just write continue <for loop label name>.

Example 4 − Continue Statement in While Loop

public class ContinueExample4 { // class declaration public static void main(String[] args) { // main function declaration int i = 0; // variable declaration while(i <= 10) { // while loop to increment value of i by 1 if (i == 5) { // checking if value of i is equal to 5 i++; continue; // if yes, then continue is applied which will skip the remaining statements and take the control to the next iteration } System.out.println(i); // printing value of i on each loop i++; // incrementing value of i by 1 } } }

Output

 0
 1
 2
 3
 4
 6
 7
 8
 9
10

In this code, we are using while loop instead of for loop to skip a certain number and print the remaining numbers.

Example 5 − Continue Statement in Do-while Loop

public class ContinueExample5 { // class declaration public static void main(String[] args) { // main function declaration int i = 0; // variable declaration do { // start of do-while block if (i == 5) { // checking if value of i is equal to 5 i++; continue; // if yes, then continue is applied which will skip the remaining statements and take the control to the next iteration } System.out.println(i); // printing value of i on each loop i++; // incrementing value of i by 1 } while(i <= 10); // while statement to increment value of i by 1 } }

Output

 0
 1
 2
 3
 4
 6
 7
 8
 9
10

In this code, we are using do-while loop instead of while loop to skip a certain number and print the remaining numbers.


Advertisements