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.

Related Post

New Tools for 64-bit Support in Xcode 9.3   New Tools for 64-bit Support in Xcode 9.3 The last macOS release to support 32-bit apps without compromise is macOS High Sierra. Make sure fu...
Java If-else Statement Java If-else Statement The Java if statement is used to test the condition. It checks boolean condition: true or false. For example, if a number i...
Strings String can be represented as a single-dimensional character type array. Strings in C are group of characters, digits, and symbols enclosed in quotatio...
Getting started with C Getting started with C : If we want to communicate with our computer it is very much needed that our computer understands what we want to say to it. ...
Invitation Letter for Visa Invitation Letter Samples These letter are necessary for taking visa of another country. so these samples are the most important document. Sample 1 ...
Data Type And Storage And Keywords To store data inside the computer we need to first identify the type of data elements we need in our program. There are several different types of dat...

Leave a Reply

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