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

Compile C/C++ program (Dev-C/C++) what is Dev-C/C++? Dev-C/C++, developed by Bloodshed Software, is a fully featured graphical IDE(Integrated Development Environment),which ...
Break Statement in JAVA Break Statement in JAVA The break statement is usually used in following two scenarios: The break statement terminates the loop immediately, and...
Firebase Push notification | Swift tutorial Push Notification by firebase in IOS.For Firebase Push notification we need to have paid apple developer account. You need to Enroll into the Apple D...
First Java Program First Java Program NOTE: You need to have java installed on your system. Step 1: Open a text editor, like Notepad on windows and TextEdit on Mac. W...
E-Mail Format Formal Email / Letter Format A formal email is in general sent to someone you don’t know well or to someone who’s in authority. Examples of someone w...
Dart Introduction | Dart programming language Dart Introduction Dart is a scalable language that you can use to write simple scripts or full featured apps. Whether you’re creating a mobile app, w...

Leave a Reply

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