<p>In this article, you will learn how to use continue construct and continue labels in Kotlin.</p>
<p><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;">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: #0000ff;"><strong>continue</strong> </span>statement.</span></p>
<h3><span style="color: #000080;"><strong>Kotlin continue example</strong></span></h3>
<p><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;">The continue statement is used to skip the iteration of the nearest enclosing loop (for, while, and do&#8230;while loop).</span></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 (iteration through iterator) {
 <strong><span style="color: #008000;">//codes</span></strong>
 if (test-expression) {
 continue
 }
 <strong><span style="color: #008000;">//codes</span></strong>
}</pre>
<p>If<strong><span style="color: #0000ff;"> test-expression </span></strong>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>fun main(args: Array<;String>;) {

 for (n in 1..10) {

 if (n % 2 != 0)
 <span style="color: #008000;"><strong>continue</strong></span>

 print(" $n ")

 }
}</pre>
<h4><span style="color: #0000ff;"><strong>Output</strong></span></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="color: #008000;">print(&#8221; $n &#8220;)</span></strong> statement.</li>
</ul>
<h3></h3>
<h3><span style="color: #000080;"><strong>Kotlin 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>fun main(args: Array<;String>;) {

 for (i in 1..3) {
 for (j in 1..2) {
 println("Before continue,i = $i; j = $j")
 if (i == 2) {
 continue
 }
 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>
<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><strong><span style="color: #008000;">If</span></strong> condition <span style="color: #0000ff;"><strong>( i == 2 )</strong></span> 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="color: #008000;"><strong><span style="text-align: left; 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;">println(&#8220;i = $i; j = $j&#8221;)</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>Kotlin 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><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;">Syntax of labeled </span><span style="color: #008000;"><strong>continue</strong> </span><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;">in for loop:</span></p>
<pre><strong><span style="color: #0000ff;">outer@</span></strong> for(iteration through iterator) {
 <strong><span style="color: #008000;">// code</span>
 <span style="color: #0000ff;">inner@</span></strong> for(iteration through iterator) {
 <span style="color: #008000;"><strong>// code</strong></span>
 if(condition for continue) {
 <strong><span style="color: #0000ff;">continue@outer</span></strong>
 }
 }
}</pre>
<h4><span style="color: #0000ff;"><strong>Example</strong></span></h4>
<pre>fun main(args: Array<;String>;) {

 <strong><span style="color: #0000ff;">label@</span> </strong>for (i in 1..5) {
 for (j in 1..2) {
 if (i >; 2) {
 <strong><span style="color: #0000ff;">continue@label</span></strong>
 }
 println("i = $i; j = $j")
 }
 }
}</pre>
<h4><span style="color: #0000ff;"><strong>Output</strong></span></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<strong>, </strong>yields <strong><span style="color: #008000;">true</span>.</strong>Then<strong>, <span style="color: #008000;">continue@label</span></strong> is executed, which skips the execution of <span style="color: #008000;"><strong>println(&#8220;i = $i; j = $j&#8221;)</strong></span> statement and passes the control to the desired loop <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;">marked with label </span><strong><span style="color: #008000;">label@</span></strong> for further iteration.</li>
</ul>
<p> ;</p>
<p> ;</p>
<p> ;</p>


