How to use continue in java / Everything about continue statement in java

continue is a keyword in java which is used inside loops(for, while and do-while). Please note that continue can only be used inside loops. Using continue outside a loop will result in a compiler error as continue cannot be used outside a loop.

How continue works

continue is used to skip the current loop iteration and start with the next one. This means that as soon as continue is encountered inside the loop, all statements after continue are skipped and loop execution starts from the beginning of the loop. Remember that loop counter is incremented. Thus when continue is executed, it behaves as if the end of loop has reached for the current iteration and next iteration is started.

Look at the following example. It contains a loop which executes from 1 to 10 and prints the loop counter value. Inside the loop, we check for counter value equal to 5. If it is equal to 5 then continue. Check the output for better understanding.

public class ContinueExample {
   public static void main(String args[]) {
	for (int counter = 1; counter <= 10; counter++) {
		if (counter == 5) {
			System.out.println("Skipping current iteration");
                        //print statement after this statement will not be executed
			continue;
		}
		System.out.println("Loop iteration: " + counter);
	}
   }
}

Output

Loop iteration: 1
Loop iteration: 2
Loop iteration: 3
Loop iteration: 4
Skipping current iteration
Loop iteration: 6
Loop iteration: 7
Loop iteration: 8
Loop iteration: 9
Loop iteration: 10

See that when if condition is met and continue is executed, loop statements after continue are skipped and loop counter is incremented.

How continue behaves in case of nested loops

Nested loop means a loop inside another loop. What happens when continue is placed inside a nested loop? Answer is that the loop in which continue statement is written is skipped when continue is executed. Suppose there is Loop1 which contains another loop, Loop2. Now following two conditions may exist :

         Scenario 1 : continue is placed inside Loop2. When continue is executed, then current iteration of Loop 2 is skipped and Loop1 executes normally.
         Scenario 2 : continue is placed inside Loop1. When continue is executed, then current iteration of Loop 1 is skipped and hence execution of Loop2 for this iteration of Loop1 is also skipped.

Look at the following code example for Scenario 1. It contains two loops where one loop is nested. Both loops iterate from 1 to 3 and print the value of their loop counters. Inner loop skips when inner loop counter becomes equal to 2 which means that the value of inner loop counter is not printed when it is equal to 2.

public class NestedContinueExample {
   public static void main(String args[]) {
	for (int outerLoopCounter = 1; outerLoopCounter <= 3; outerLoopCounter++) {
		System.out.println("Outer Loop iteration: " + outerLoopCounter);
		for (int innerLoopCounter = 1; innerLoopCounter <= 3; innerLoopCounter++) {
                        // check if outer loop counter is 2
			if (innerLoopCounter == 2) {
                                // print message and skip inner loop
				System.out.println("Skipping inner loop");
				continue;
			}
			System.out.println("Inner Loop iteration: " + innerLoopCounter);
		}
	}
   }
}

Output

Outer Loop iteration: 1
Inner Loop iteration: 1
Skipping inner loop
Inner Loop iteration: 3
Outer Loop iteration: 2
Inner Loop iteration: 1
Skipping inner loop
Inner Loop iteration: 3
Outer Loop iteration: 3
Inner Loop iteration: 1
Skipping inner loop
Inner Loop iteration: 3

Note that when inner loop counter is 2 its current iteration is skipped and the print statement after continue is not executed.

Have a look at the following code example for Scenario 2. It contains two loops where one loop is nested. Both loops iterate from 1 to 3 and print the value of their loop counters.
Outer loop continues when its loop counter becomes equal to 2. This means that when outer loop counter is equal to 2, it should skip its current iteration and begin with its loop counter value as 3. Inner loop is not executed when the outer loop counter is 2.
Output given after the program further explains the concept.

public classNestedBreakExample {
   public static void main(String args[]) {
	for (int outerLoopCounter = 1; outerLoopCounter <= 3; outerLoopCounter++) {
		System.out.println("Outer Loop iteration: " + outerLoopCounter);
                // check if outer loop counter is 2
		if (outerLoopCounter == 2) {
                        // print message and terminate outer loop
			System.out.println("Skipping outer loop");
			continue;
		}
		for (int innerLoopCounter = 1; innerLoopCounter <= 3; innerLoopCounter++) {
			System.out.println("Inner Loop iteration: " + innerLoopCounter);
		}
	}
   }
}

Output

Outer Loop iteration: 1
Inner Loop iteration: 1
Inner Loop iteration: 2
Inner Loop iteration: 3
Outer Loop iteration: 2
Skipping outer loop
Outer Loop iteration: 3
Inner Loop iteration: 1
Inner Loop iteration: 2
Inner Loop iteration: 3

Note that when outer loop counter is 2, it returns from there only and begins next iteration with loop counter as 3. Note that Inner loop does not not execute when outer loop counter becomes 2.

Let’s tweak in

  1. continue statement can only be used along with an if statement. Using continue without an if will result in a compiler error. This is because a continue placed without any condition will cause the loop to skip the statements after continue and they will never be executed. Compiler detects this and it flags an error.
  2. There is also a labelled version of continue statement in which a label is placed after continue. Label may be any user defined string as continue codippa;. Similar label is placed somewhere else in the program along with a colon(:) such as codippa:. Now whenever continue is executed, the control jumps to the line where label matching continue is written. This is similar to goto statement.
  3. Difference between break and continue is that as soon as break is executed, it terminates the loop while when continue is executed, loop execution starts again but with loop counter incremented. All statements after continue are skipped.