Continue statements in java is used to terminate the rest of the code in the loop and execute the next iteration. Java Continue statement is used for any type of loop, but is usually used for, while, do-while loops and foreach. The continue statement jumps immediately to the next iteration of the loop. Continue statement is mostly used inside loops.This is especially helpful when you wish to continue the loop, but do not want the rest of the loop statements to be executed for that particular iteration.

As continue statement skips the rest of the code on condition, continue should be added in if condition. Inside the loop, when a continue statement is found, the control jumps immediately to the beginning of the loop for the next iteration instead of performing the current iteration statements.

When the continue statement is executed, the program control skips the current iteration, and moves to end of the loop. Then, the loop condition is evaluated. If the condition is true, the next iteration continues, otherwise moves out of the loop.

Syntax

continue;
    or
continue labelname;


Continue Statement in Java

In the example below, the for loop is executed to print from 0 to 9. the continue statement should be executed if the value is reached to 4. If you execute the code below, the values from 0 to 9 will be printed except the value 4.

package com.yawintutor;

public class ContinueStatement {
	public static void main(String[] args) {
		
		for (int i = 0; i < 10; i++) {
			if(i==4) {
				continue;
			}
			System.out.println(i);
		}
	}
}

Output

Number 4 is missing in the below output

0
1
2
3
5
6
7
8
9


Continue statement in Nested For loop in Java

If the continue statement is added in a nested for loop, it skips current iteration of the inner for loop and continues to execute from next iteration. Below is an example of how the continue works in the nested loop.

The continue statement will skip the statements in the immediate loop. The outer loops will be executed normally. The inner loop will be skipped the rest of the code if the continue statement is executed.

package com.yawintutor;

public class NestedContinue {
	public static void main(String[] args) {
		
		for (int i = 1; i <= 3; i++) {
			System.out.println("\nSTART OUTER FOR i = " + i);

			for (int j = 1; j <= 3; j++) {
				if(j==2) {
					continue;
				}
				System.out.println("i = " + i + " j = " + j);
			}
			System.out.println("END   OUTER FOR i = " + i);
		}
	}
}

Output

The for loop with value j=2 is missed.


START OUTER FOR i = 1
i = 1 j = 1
i = 1 j = 3
END   OUTER FOR i = 1

START OUTER FOR i = 2
i = 2 j = 1
i = 2 j = 3
END   OUTER FOR i = 2

START OUTER FOR i = 3
i = 3 j = 1
i = 3 j = 3
END   OUTER FOR i = 3


Labeled continue statement in Java

The labeled continue statement skips both inner and outer for loop current iteration and starts with the next outer for loop iteration.

package com.yawintutor;

public class LabeledContinue {
	public static void main(String[] args) {
		
		outer:
		for (int i = 1; i <= 3; i++) {
			System.out.println("\nSTART OUTER FOR i = " + i);

			for (int j = 1; j <= 5; j++) {
				if(i==2 && j==3) {
					continue outer;
				}
				System.out.println("i = " + i + " j = " + j);
			}
			System.out.println("END OUTER FOR i = " + i);
		}
	}
}

Output


START OUTER FOR i = 1
i = 1 j = 1
i = 1 j = 2
i = 1 j = 3
i = 1 j = 4
i = 1 j = 5
END OUTER FOR i = 1

START OUTER FOR i = 2
i = 2 j = 1
i = 2 j = 2

START OUTER FOR i = 3
i = 3 j = 1
i = 3 j = 2
i = 3 j = 3
i = 3 j = 4
i = 3 j = 5
END OUTER FOR i = 3


Continue Statement with while loop in Java

The example below shows how to use the continue statement in while loop.

package com.yawintutor;

public class ContinueWithWhileLoop {
	public static void main(String[] args) {
		int i = 0;
		while (i < 10) {
			i++;
			if(i==5) {
				continue;
			}
			System.out.println(i);
		}
	}
}

Output

1
2
3
4
6
7
8
9
10


Continue Statement with do while loop in Java

The following example shows how to use the continue statement in do while loop.

package com.yawintutor;

public class ContinueWithDoWhileLoop {
	public static void main(String[] args) {
		int i = 0;
		do {
			i++;
			if(i==5) {
				continue;
			}
			System.out.println(i);
		} while (i < 10);
	}
}

Output

1
2
3
4
6
7
8
9
10



Leave a Reply