Continue Statement in Java

Continue statements are used to terminate the rest of the code in the loop and execute the next iteration. Otherwise, the continue statement jumps immediately to the next iteration of the loop.

As continue statement skips the rest of the code on condition, continue should be added in if condition.

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

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.

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



Related Articles

Leave a Reply

Your email address will not be published. Required fields are marked *