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

Emailing Phrases Most useful emailing phrases There are some standard phrases that are used in business or formal letters. These are essential to give a professional ...
Decision Control Statement C has three major decision making instructions— the if statement, the if-else statement, and the switch statement. The if Statement It is used to ex...
How to install flutter | Flutter tutorial Flutter is the google’s mobile app SDK for develop the high-quality native app for iOS and android. We can develop the app by use of DART and javascri...
Loop Control Statement Loop control statements are used when a section of code may either be executed a fixed number of times. C gives you a choice of three types of loop st...
Custom View by use of xib in swift Custom View by use of xib in swift We can create the custom and common view by the use of xib in swift 3.1 and XCode 8.3.2 . Custom views are common ...
C Language – Introduction Why C? There are three important aspects of any language so as C has : The way it stores data, The way it operates on this data and How th...
Author

Write A Comment