Continue Statement in JAVA

Continue Statement in JAVA

Suppose you are working with loops. Sometime it is desirable terminate the loop or skip some statement inside the loop without checking the test expression.
In such case “continue” and “break” statement are used.

  • “Continue” statement is mostly used inside the loops (for, while, do-while).
  • It skips the current iteration of the loop.
  • Whenever “Continue” statment is executed, control of the program directly jumps to the beginning of the loop for next iteration, skipping the execution of statements inside loop’s body for the current iteration.
  • It is almost always used with decision making statements (if…else Statement).
Syntax:

The syntax of this is continue word followed by semicolon i.e.

continue;

Flow Diagram of Continue Statement:

How continue statement works?:

 

Example: Continue Statement inside FOR Loop:
public class ContinueExample1{
   public static void main(String[] args){
      for (int i = 1; i <= 10; ++i){      
         if (i > 4 && i < 9){
            continue;
         }      
         System.out.println(i);
      }   
   }
}
Output:
1
2
3
4
9
10

As you may have noticed, the values 5, 6, 7, 8 are missing in the output, why?
Because when the value of i becomes more than 4 and less than 9, continue statement is executed, which skips the execution of System.out.println(i); statement.

 

Example: Continue Statement inside WHILE Loop:

 

public class ContinueExample2 {
   public static void main(String args[]){
    int counter=10;
    while (counter >=0)
    {
          if (counter==7)
           {
           counter--;
           continue;
           }
           System.out.print(counter+" ");
           counter--;
    }
   }
}
Output:
10 9 8 6 5 4 3 2 1 0

This loop is iterating from 10 to 0 for counter value and when the counter value is 7 the loop skipped the print statement and started next iteration of the while loop.

Example: Continue Statement inside DO-WHILE Loop:
public class ContinueExample3{
   public static void main(String args[]){
    int j=0;
        do
    {
       if (j==7)
       {
         j++;
         continue;
       }
       System.out.print(j+ " ");
       j++;
       }
       while(j<10);
   }
}
Output
0 1 2 3 4 5 6 8 9 
In case of nested loops, continue skips the current iteration of innermost loop.

Labeled Continue Statement:

  • Till now we have discussed the unlabeled form of “Continue” statement, which skips the execution of remaining statement(s) of innermost for, while and do-while loop.
  • There is another form of continue statement i.e. “LABELED” form, that can be used to skip the execution of statement(s) that lies inside the outer loop.

How labeled continue statement works?:

In this, “LABEL” is an identifier.

Example: labeled Continue Statement:
class LabeledContinue{
   public static void main(String[] args) {
      label:
      for (int i = 1; i < 6; ++i) {
         for (int j = 1; j < 5; ++j) {
            if (i == 3 || j == 2)
               continue label;
            System.out.println("i = " + i + "; j = " + j); 
         }
      } 
   }
}
Output:
i = 1; j = 1
i = 2; j = 1
i = 4; j = 1
i = 5; j = 1
NOTE: The use of labeled continue is often discouraged as it makes your code hard to understand.

Java continue important points:

Some important points about java continue statement are;

  • For simple cases, continue statement can be easily replaced with if-else conditions but when we have multiple if-else conditions then using continue statement makes our code more readable.
  • Continue statement comes handy incase of nested loops and to skip a particular record from processing.

Leave a Reply

Your email address will not be published. Required fields are marked *

Get more stuff like this
in your inbox

Subscribe to our mailing list and get interesting stuff and updates to your email inbox.