Java Continue

Java continue statement is used to skip the execution of subsequent statements in the loop block, and continue with the next iteration.

If continue statement is used in nested loops, only the immediate loop is considered.

Continue statement can be used inside a For loop, While loop and Do-while loop statements.

Syntax

The syntax of continue statement is

continue
ADVERTISEMENT

Examples

In the following examples, we will cover scenarios where we use continue statement in different kinds of loops.

Continue For Loop

In the following example, we write a For loop with condition to execute until i is less than 10. But, inside the For loop, we execute continue statement when i equals 5.

Java Program

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

Output

0
1
2
3
4
6
7
8
9

Execution for I=5 is skipped.

In the following example, we will use continue statement inside a For-each loop.

Java Program

public class Example {
	public static void main(String[] args) {
		int[] arr = {1, 2, 4, 8, 16};
		
		for (int x: arr) {
			if (x == 8) {
				continue;
			}
			System.out.println(x);
		}
	}
}

Output

1
2
4
16

Continue While Loop

In the following example, we write a While loop with condition to execute until i is less than 10. But, inside the While loop, we continue with next iteration when i equals 5.

Java Program

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

Output

0
1
2
3
4
6
7
8
9

Continue Do-while Loop

In the following example, we use continue statement inside a do-while loop.

Java Program

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

Output

0
1
2
3
4
6
7
8
9

Conclusion

In this Java Tutorial, we have learnt Java continue statement, and how to use this continue statement inside looping statements.