<p>In this article, you will learn how to use continue construct and continue labels in Java.</p>
<p>Sometimes it is useful to force an early iteration of a loop. That is, you might want to continue running the loop but stop processing the remaining code in its body for this particular iteration. In such case we can use <span style="color: #008000;"><strong>continue</strong> </span>statement.</p>
<h3><span style="color: #000080;"><strong>Java continue example</strong></span></h3>
<p>The continue statement is used to skip the iteration of the nearest enclosing loop (for, while, and do…while loop).</p>
<p>Basically, continue is used to repeat the loop for a specific condition. It skips the remaining statements and continues with the next iteration of the loop.</p>
<h4><span style="color: #0000ff;"><strong>Syntax</strong></span></h4>
<p>Syntax of <span style="color: #008000;"><strong>continue</strong> </span>in for loop:</p>
<pre>for (...) {
 <strong><span style="color: #008000;">//codes</span></strong>
 if (test-expression) {
 continue;
 }
 <strong><span style="color: #008000;">//codes</span></strong>
}</pre>
<p>If<span style="color: #0000ff;"><strong> test-expression </strong></span>is evaluated to <span style="color: #008000;"><strong>true</strong></span>, <strong><span style="color: #008000;">continue</span> </strong>statement is executed which skips the remaining statements inside the loop and continues with the next iteration of the loop.</p>
<h4><span style="color: #0000ff;"><strong>Example</strong></span></h4>
<p>The below program will print even numbers between 1 to 10.</p>
<pre>public class ContinueDemo {

 public static void main(String[] args) {

 for (int n = 1; n <;= 10; n++) {

 if (n % 2 != 0)
 continue;

 System.out.print(" " + n + " ");

 }
 }
}</pre>
<h4><strong><span style="color: #0000ff;">Output</span></strong></h4>
<pre> 2 4 6 8 10</pre>
<h4><span style="color: #0000ff;"><strong>Example explained:</strong></span></h4>
<p>In the above example, when n = { 1, 3, 5, 7, 9 } :</p>
<ul>
<li><span style="color: #008000;"><strong>If</strong></span> condition<strong><span style="color: #0000ff;"> ( n % 2 ! = 0 )</span></strong> is checked<strong>, </strong>yields <strong><span style="color: #008000;">true</span>.</strong>Then<strong>, </strong>continue statement is executed, which skips the execution of <strong><span style="text-align: left; color: #008000; text-transform: none; line-height: 1.2em; text-indent: 0px; letter-spacing: normal; font-family: 'Noto Serif',Georgia,'Times New Roman',Times,serif; font-size: 16px; font-style: normal; font-variant: normal; text-decoration: none; word-spacing: 0px; display: inline !important; white-space: normal; cursor: text; orphans: 2; float: none; -webkit-text-stroke-width: 0px; background-color: #ffffff;">System.out.print(&#8221; &#8221; + n + &#8221; &#8220;)</span></strong> statement.</li>
</ul>
<h3></h3>
<h3><span style="color: #000080;"><strong>Java continue example in nested loop</strong></span></h3>
<div>When <strong><span style="color: #008000;">continue</span> </strong>is used in the nested loop (loop inside another loop), it skips the iteration of the nearest enclosing loop (inner loop) when it is encountered and continues with the next iteration of the loop.</div>
<div>
<h4></h4>
<h4><span style="color: #0000ff;"><strong>Example</strong></span></h4>
<pre>public class ContinueDemo {

 public static void main(String[] args) {

 for (int i = 1; i <;= 3; i++) {
 for (int j = 1; j <;= 2; j++) {

 System.out.println("Before continue,i = " + i + "; j = " +j);
 if (i == 2) {
 continue;
 }
 System.out.println("i = " + i + "; j = " +j);
 }
 }
 
 }
}</pre>
<h4><span style="color: #0000ff;"><strong>Output</strong></span></h4>
<pre>Before continue,i = 1; j = 1
i = 1; j = 1
Before continue,i = 1; j = 2
i = 1; j = 2
Before continue,i = 2; j = 1
Before continue,i = 2; j = 2
Before continue,i = 3; j = 1
i = 3; j = 1
Before continue,i = 3; j = 2
i = 3; j = 2</pre>
<div>
<h4><span style="color: #0000ff;"><strong>Example explained:</strong></span></h4>
</div>
<p>In the above example, when i = 2 :</p>
<!-- WP QUADS Content Ad Plugin v. 2.0.98.1 -->
<div class="quads-location quads-ad2" id="quads-ad2" style="float:none;margin:0px;">

</div>

<ul>
<li><span style="color: #008000;"><strong>If</strong></span> condition<strong><span style="color: #0000ff;"> ( i == 2 )</span></strong> is checked<strong>, </strong>yields <strong><span style="color: #008000;">true</span>.</strong>Then<strong>, </strong>continue statement is executed, which skips the execution of <span style="display: inline !important; float: none; background-color: #ffffff; color: #2c3338; cursor: text; font-family: 'Noto Serif',Georgia,'Times New Roman',Times,serif; font-size: 16px; font-style: normal; font-variant: normal; font-weight: 400; letter-spacing: normal; line-height: 1.2em; orphans: 2; text-align: left; text-decoration: none; text-indent: 0px; text-transform: none; -webkit-text-stroke-width: 0px; white-space: normal; word-spacing: 0px;"><strong><span style="color: #008000;">System.out.println(&#8220;i = &#8221; + i + &#8220;; j = &#8221; +j)</span></strong></span> statement and passes the control to inner for-loop for further iteration.</li>
</ul>
<h3></h3>
<h3><span style="color: #000080;"><strong>Java labeled continue example</strong></span></h3>
<p>In Kotlin, <span style="color: #008000;"><strong>labeled</strong> </span>continue is used to skip the iteration of the desired loop when certain condition is satisfied. It can be done with the help of <span style="color: #008000;"><strong>labels</strong></span>.</p>
<p><strong><span style="color: #008000;">Label</span> </strong>starts with an identifier which is followed by <strong>‘ : ‘</strong>. e.g.-<span style="color: #008000;"><strong> inner: , outer:</strong></span> etc.</p>
<h4><span style="color: #0000ff;"><strong>Syntax</strong></span></h4>
<p>Syntax of labeled <span style="color: #008000;"><strong>continue</strong> </span>in for loop:</p>
<pre>outer:
for (...){
 <span style="color: #008000;"><strong> // code</strong></span>
 inner:
 for (...){
 <strong><span style="color: #008000;">// code</span></strong>
 if (condition for continue){
 continue outer;
 }
 }
}</pre>
<h4><strong><span style="color: #0000ff;">Example</span></strong></h4>
<pre>public class ContinueDemo {

 public static void main(String[] args) {

 label:
 for (int i = 1; i <;= 5; i++) {
 for (int j = 1; j <;= 2; j++) {
 if (i >; 2) {
 continue label;
 }
 System.out.println("i = " + i + "; j = " +j);
 }
 }

 }
}</pre>
<h4><strong><span style="color: #0000ff;">Output</span></strong></h4>
<pre>i = 1; j = 1
i = 1; j = 2
i = 2; j = 1
i = 2; j = 2</pre>
<h4><span style="color: #0000ff;"><strong>Example explained:</strong></span></h4>
<p>In the above example, when i = { 3, 4, 5 } :</p>
<ul>
<li><strong><span style="color: #008000;">If</span></strong> condition <strong><span style="color: #0000ff;">( i >; 2 )</span></strong> is checked, yields <strong><span style="color: #008000;">true</span></strong>.Then,<strong><span style="color: #0000ff;"> continue label</span></strong> is executed, which skips the execution of<strong><span style="color: #008000;"> System.out.println(&#8220;i = &#8221; + i + &#8220;; j = &#8221; +j)</span></strong> statement and passes the control to the desired loop marked with label <strong><span style="color: #0000ff;">label:</span></strong> for further iteration.</li>
</ul>
<p> ;</p>
</div>
<p> ;</p>


