<p>In this article, you will learn about the do-while loop and how to create do-while loop in Kotlin programming.</p>
<p><span style="color: #0000ff;"><strong>Loops</strong></span> in Kotlin are used when we want to execute a block of statements repeatedly until a specific condition is met(condition is <span style="color: #008000;"><strong>false</strong></span>).</p>
<h3><span style="color: #000080;"><strong>Kotlin do-while Loop</strong></span></h3>
<p class="p1"><span class="s1">Kotlin&#8217;s</span> <span style="color: #008000;"><b>do-while loop</b></span> is an <span style="color: #008000;"><strong>e</strong></span><b><span style="color: #008000;">xit-control loop</span>, </b>which means unlike while loop, the do-while loop check for the condition after executing the statements or the loop body.</p>
<p class="p1">The do-while loop is similar to while loop with one main difference. The body of the do-while loop is executed at least once before the test-expression is checked.</p>
<p><span style="color: #0000ff;"><strong>Note:</strong></span> the do-while loop will execute the statements at least once before any condition is checked.</p>
<h4><span style="color: #0000ff;"><strong>Syntax</strong></span></h4>
<pre>do {
 <strong><span style="color: #008000;">//body of the loop</span></strong>
<strong><span style="color: #008000;"> //statements to be executed</span></strong>
 }
 <strong><span style="color: #008000;">//test expression is a boolean expression</span></strong>
 while (test_expression);</pre>
<h3></h3>
<h3><span style="color: #000080;"><strong>Flowchart of do-while loop</strong></span></h3>
<p><img class=" wp-image-1672 aligncenter" src="https://c1ctech.com/wp-content/uploads/2020/03/Screenshot-2020-03-18-14.19.30-3022851422-1584522057550.png" alt="Screenshot 2020-03-18 14.19.30" width="590" height="489" /></p>
<h3 class="p1"><span style="color: #000080;"><b>How </b><strong>do-while </strong><b>loop works?</b></span></h3>
<p class="p2">When the Control falls into the do-while loop, it first executes the body of the loop inside the <span style="color: #0000ff;"><strong>do</strong></span> block and then it checks the <span style="color: #008000;"><strong>test_expression</strong></span>.</p>
<p class="p2"><span style="color: #000080;"><strong>If the test expression is evaluated to true,</strong></span></p>
<ul class="ul1">
<li>The statements inside the do block(body of the loop) get executed.</li>
<li class="li2">Then, the test expression is evaluated again and this process goes on until the test expression is evaluated to <span style="color: #008000;"><strong>false</strong></span>.</li>
</ul>
<p class="p2"><span style="color: #000080;"><strong>If the test expression is evaluated to false,</strong></span></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 class="ul1">
<li class="li2">do-while loop gets terminated and control goes out of the do-while loop.</li>
</ul>
<h4></h4>
<h4><strong><span style="color: #0000ff;">Example 1</span></strong></h4>
<pre>fun main(args: Array<;String>;) {

 var i = 1

 do {
 System.out.println("value of i: $i")

 <span style="color: #008000;"><strong>//increment value of i by 1</strong></span>
 i++
 }
 <strong><span style="color: #008000;">//exit when i becomes greater than 3</span></strong>
 while (i <; 4)
}</pre>
<h4></h4>
<h4><strong><span style="color: #0000ff;">Output</span></strong></h4>
<pre>value of i: 1
value of i: 2
value of i: 3</pre>
<h4></h4>
<h4><strong><span style="color: #0000ff;">Example explained</span></strong></h4>
<p class="p1">In the above example,</p>
<p class="p1"><strong>1</strong>.<span style="color: #008000;"><strong> i </strong></span>is initialized with value 1.</p>
<p class="p1"><strong>2. </strong>Execute the loop body inside <span style="color: #0000ff;"><strong>do</strong></span> block.</p>
<ul>
<li class="p1"><span style="color: #008000;"><strong>“value of i: 1”</strong></span> gets printed.</li>
<li class="p1">Updation is done. Now <span style="color: #008000;"><b>i = 2</b></span>.</li>
</ul>
<p class="p1"><strong>3</strong>. Condition <span style="color: #0000ff;"><strong>2 <; 4</strong></span> is checked, yields true.</p>
<p class="p1"><strong>4</strong>. Execute the loop body inside <span style="color: #0000ff;"><strong>do</strong></span> block.</p>
<ul>
<li class="p1"><span style="color: #008000;"><b>“value of i: 2”</b></span> gets printed.</li>
<li class="p1">Updation is done. Now <span style="color: #008000;"><b>i = 3</b></span>.</li>
</ul>
<p><strong>5</strong>. Condition <span style="color: #0000ff;"><strong>3<; 4</strong></span> is checked, yields true.</p>
<p class="p1"><strong>6</strong>. Execute the loop body inside <span style="color: #0000ff;"><strong>do</strong></span> block.</p>
<ul>
<li class="p1"><span style="color: #008000;"><b>“value of i: 3”</b></span> gets printed.</li>
<li class="p1">Updation is done. Now <span style="color: #008000;"><b>i = 4</b></span>.</li>
</ul>
<p><strong>7</strong>. Condition <span style="color: #0000ff;"><strong>4<; 4</strong></span> is checked, yields false.</p>
<p class="p1"><strong>8</strong>. Exit from the do-while loop.</p>
<h4></h4>
<h4><span style="color: #0000ff;"><strong>Example 2</strong></span></h4>
<p>The below program will print the sum of natural numbers from 1 to 10.</p>
<pre>fun main(args: Array<;String>;) {

 var i = 1
 var sum = 0

 do {
 sum = sum + i

 <strong><span style="color: #008000;"> //Increment the value of i for</span></strong>
<strong><span style="color: #008000;"> //next iteration</span></strong>
 i++
 } while (i <;= 10)

 println("Sum of numbers from 1 to 10: $sum")

}</pre>
<h4><strong><span style="color: #0000ff;">Output</span></strong></h4>
<pre>Sum of numbers from 1 to 10: 55</pre>


