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.


1
<span class="hljs-keyword">continue</span>;

Flow Diagram of Continue Statement:

How continue statement works?:

 

Example: Continue Statement inside FOR Loop:

1
2
3
4
5
6
7
8
9
10
<span class="hljs-keyword">public</span> <span class="hljs-class"><span class="hljs-keyword">class</span> <span class="hljs-title">ContinueExample1</span></span>{
   <span class="hljs-function"><span class="hljs-keyword">public</span> <span class="hljs-keyword">static</span> <span class="hljs-keyword">void</span> <span class="hljs-title">main</span><span class="hljs-params">(String[] args)</span></span>{
      <span class="hljs-keyword">for</span> (<span class="hljs-keyword">int</span> i = <span class="hljs-number">1</span>; i &lt;= <span class="hljs-number">10</span>; ++i){      
         <span class="hljs-keyword">if</span> (i &gt; <span class="hljs-number">4</span> &amp;&amp; i &lt; <span class="hljs-number">9</span>){
            <span class="hljs-keyword">continue</span>;
         }      
         System.out.println(i);
      }  
   }
}
Output:

1
2
3
4
5
6
<span class="hljs-number">1</span>
<span class="hljs-number">2</span>
<span class="hljs-number">3</span>
<span class="hljs-number">4</span>
<span class="hljs-number">9</span>
<span class="hljs-number">10</span>

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:

 


1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
<span class="hljs-keyword">public</span> <span class="hljs-class"><span class="hljs-keyword">class</span> <span class="hljs-title">ContinueExample2</span> </span>{
   <span class="hljs-function"><span class="hljs-keyword">public</span> <span class="hljs-keyword">static</span> <span class="hljs-keyword">void</span> <span class="hljs-title">main</span><span class="hljs-params">(String args[])</span></span>{
    <span class="hljs-keyword">int</span> counter=<span class="hljs-number">10</span>;
    <span class="hljs-keyword">while</span> (counter &gt;=<span class="hljs-number">0</span>)
    {
          <span class="hljs-keyword">if</span> (counter==<span class="hljs-number">7</span>)
           {
           counter--;
           <span class="hljs-keyword">continue</span>;
           }
           System.out.print(counter+<span class="hljs-string">" "</span>);
           counter--;
    }
   }
}
Output:

1
<span class="hljs-number">10</span> <span class="hljs-number">9</span> <span class="hljs-number">8</span> <span class="hljs-number">6</span> <span class="hljs-number">5</span> <span class="hljs-number">4</span> <span class="hljs-number">3</span> <span class="hljs-number">2</span> <span class="hljs-number">1</span> <span class="hljs-number">0</span>

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:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
<span class="hljs-keyword">public</span> <span class="hljs-class"><span class="hljs-keyword">class</span> <span class="hljs-title">ContinueExample3</span></span>{
   <span class="hljs-function"><span class="hljs-keyword">public</span> <span class="hljs-keyword">static</span> <span class="hljs-keyword">void</span> <span class="hljs-title">main</span><span class="hljs-params">(String args[])</span></span>{
    <span class="hljs-keyword">int</span> j=<span class="hljs-number">0</span>;
        do
    {
       <span class="hljs-keyword">if</span> (j==<span class="hljs-number">7</span>)
       {
         j++;
         <span class="hljs-keyword">continue</span>;
       }
       System.out.print(j+ <span class="hljs-string">" "</span>);
       j++;
       }
       <span class="hljs-keyword">while</span>(j&lt;<span class="hljs-number">10</span>);
   }
}
Output

1
<span class="hljs-number">0</span> <span class="hljs-number">1</span> <span class="hljs-number">2</span> <span class="hljs-number">3</span> <span class="hljs-number">4</span> <span class="hljs-number">5</span> <span class="hljs-number">6</span> <span class="hljs-number">8</span> <span class="hljs-number">9</span>
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:

1
2
3
4
5
6
7
8
9
10
11
12
<span class="hljs-class"><span class="hljs-keyword">class</span> <span class="hljs-title">LabeledContinue</span></span>{
   <span class="hljs-function"><span class="hljs-keyword">public</span> <span class="hljs-keyword">static</span> <span class="hljs-keyword">void</span> <span class="hljs-title">main</span><span class="hljs-params">(String[] args)</span> </span>{
      label:
      <span class="hljs-keyword">for</span> (<span class="hljs-keyword">int</span> i = <span class="hljs-number">1</span>; i &lt; <span class="hljs-number">6</span>; ++i) {
         <span class="hljs-keyword">for</span> (<span class="hljs-keyword">int</span> j = <span class="hljs-number">1</span>; j &lt; <span class="hljs-number">5</span>; ++j) {
            <span class="hljs-keyword">if</span> (i == <span class="hljs-number">3</span> || j == <span class="hljs-number">2</span>)
               <span class="hljs-keyword">continue</span> label;
            System.out.println(<span class="hljs-string">"i = "</span> + i + <span class="hljs-string">"; j = "</span> + j);
         }
      }
   }
}
Output:

1
2
3
4
i = <span class="hljs-number">1</span>; j = <span class="hljs-number">1</span>
i = <span class="hljs-number">2</span>; j = <span class="hljs-number">1</span>
i = <span class="hljs-number">4</span>; j = <span class="hljs-number">1</span>
i = <span class="hljs-number">5</span>; j = <span class="hljs-number">1</span>
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

Control Flow Statements | Swift tutorial Control Flow : Swift tutorial Swift Provides diffrent type of controll flow statements like: Loops Statements : Loops are used for perform the t...
Create UITableView (Table View) By the use of stor... How to use UITableView in Above IOS 8 and Swift (UITableView) In this tutorial we are study how to use the TableView in Swift. One of the most common...
Dart Libraries | Dart Tutorial Dart Libraries Dart has the following libraries, which are included in all Dart platforms: dart:core This library is automatically imported into ev...
Switch Case Statement Switch Case Statement The switch statement is a multi-way branch statement. It provides an easy way to dispatch execution to different parts of c...
Use of CollectionViewDataSource method moveItemAt ... Use of CollectionViewDataSource method moveItemAt for Reordering CollectionView cell In IOS (Swift 4) In this tutorial we are study about how we can ...
Introduction to Java Programming Language JAVA TUTORIALS Introduction to Java Programming Language What is JAVA Java is programming language that produces software for multiple platforms, w...

Leave a Reply

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