Java: Continue Statement

break and continue in java

Break and Continue Statements

Sometime the programmer wants to skip some conditions in the loop or might desire to terminate the loop without evaluating the test condition. 

Break and Continue statements fulfill this same purpose.

Continue statement in Java

The Continue statement in Java functions similarly to a break statement. It does not impose a forced termination. Instead, When a continue statement is encountered within a loop, the control jumps to the beginning of the loop for the next iteration, skipping the execution of statements inside the body of the loop for the current iteration. In other words, The continue statement skips some lines of code inside the loop of the current iteration and continues with the next iteration. It is mainly used for a condition where we want skip some code or a particular iteration for a particular condition.

Continue statement




//part of the code to be skipped

Now let us proceed and understand continue statement with the help of an example. We want to print a series of numbers but skip one of them in process.

The code for this purpose using for loop and continue would be as follows:

public class Main 
   public static void main(String args[])
	for (int num=0; num<=10; num++)
           if (num==5)

           System.out.print(num+" ");

In this code we initialize a loop which runs until num <= 10. We want the loop to skip the iteration num = 5.

Hence we use the continue statement. Let us see what will be the output of the code above.

The code will yield output as follows, skipping the number ‘ 5 ‘ 


1  2  3  4  6  7  8  9  10