Java continue Statement
In this tutorial, we shall see the use of continue
statement in java.
The continue
statement aborts the current iteration of the loop and moves on to execute the next iteration.
Syntax
continue;
Let us see a program with continue
statement to print all the odd numbers in an array.
Example
class Main {
public static void main(String[] args) {
// create an array with initial values.
int[] numbers = {1,3,4,5,7,8};
for (int number: numbers) {
if(number%2==0){
continue;
}
System.out.println(number);
}
System.out.println("End of program.");
}
}
Output
1
3
5
7
End of program.
In the above program, we have initialize an integer array and loop through it using for-each
loop.
Inside the for-each
block, we check whether the value is divisible by 2
. If the value is divisible by 2
, then we use the continue
statement to skip further executing in the block of code and move to the end of the loop. Then it starts the new iteration.