##### Keyword "continue"

The Keyword “continue”

The continue keyword can be used in any of the loop control structures. It causes the loop to immediately jump to the next iteration of the loop.

• In a for loop, the continue keyword causes control to immediately jump to the update statement.

• In a while loop or do/while loop, control immediately jumps to the Boolean expression.

continue;

The continue statement passes control to the next iteration of the enclosing while, do, for, or foreach statement in which it appears.

It stops the current iteration of a loop, and starts a new iteration.

Flow Diagram

You can use the continue statement only inside a while, do...while, or for loop. Executing the continue statement stops the current iteration of the loop and continues program flow with the beginning of the loop. This has the following effects on the different types of loops:
while and do...while loops test their condition, and if true, execute the loop again. for loops execute their increment expression, and if the test expression is true, execute the loop again.
The following example illustrates the use of the continue statement:

Example 1

int sum = 0;
for(int i = 1; i <= 100 ; i++){
    if(i % 2 == 0)
         continue;
    sum += i;
}

This would get the sum of only odd numbers from 1 to 100

Example 2

public class Test {

public static void main(String args[]) {
int [] numbers = {10, 20, 30, 40, 50};

for(int x : numbers ) {
if( x == 30 ) {
continue;
}
System.out.print( x );
System.out.print("\n");
}
}
}

This will produce the following result −

Output

10
20
40
50

Example 3

int maxLoopIter = 7;

for (int i = 0; i < maxLoopIter; i++ ) {
if (i == 5) {
continue;  // -- 5 iteration is skipped --
}
System.println("Iteration = " + i);

}
results in

0
1
2
3
4
6
7

Created: