<p>In this article, you will learn about while loop and how to create 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 While Loop</strong></span></h3>
<p class="p1">The <span style="color: #0000ff;"><strong>while</strong></span> loop executes the block of code as long as a specified condition is <span style="color: #008000;"><strong>true</strong></span>.</p>
<h4></h4>
<h4><strong><span style="color: #0000ff;">Syntax</span></strong></h4>
<pre><span style="color: #008000;"><strong><span style="color: #0000ff;">//while loop</span>

//test-expression is a boolean expression</strong></span>
while (test-expression) {
 
 <strong><span style="color: #008000;">// body of the loop</span></strong>
<strong><span style="color: #008000;"> //statements to be executed</span></strong>
}</pre>
<h3></h3>
<h3><span style="color: #000080;"><strong>Flowchart of while Loop</strong></span></h3>
<p><img class=" wp-image-1670 aligncenter" src="https://c1ctech.com/wp-content/uploads/2020/03/Screenshot-2020-03-18-14.21.18.png" alt="Screenshot 2020-03-18 14.21.18" width="542" height="490" /></p>
<h3 class="p1"><span style="color: #000080;"><b>How while loop works?</b></span></h3>
<p class="p2">When the Control falls into the while loop, it first checks the <strong><span style="color: #008000;">test_expression</span></strong>.</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 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>
<ul class="ul1">
<li class="li2">while loop gets terminated and control goes out of the 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

 <strong><span style="color: #008000;">//exit when i becomes greater than 3</span></strong>
 while (i <; 4) {
 println("value of i: $i")

 <strong><span style="color: #008000;">//increment value of i by 1</span></strong>
 i++
 }
}</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>
<!-- 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>

<p class="p1"><strong>1</strong>. <span style="color: #008000;"><b>i</b></span> is initialized with value 1.</p>
<p class="p1"><strong>2</strong>. condition <span style="color: #0000ff;"><b>1 <; 4</b></span> is checked, yields <strong><span style="color: #008000;">true</span></strong>.</p>
<ul class="ul1">
<li class="li1"><span style="color: #008000;"><b>“value of i: 1”</b></span> gets printed.</li>
<li class="li1"><span class="Apple-converted-space"> </span>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;"><b>2 <; 4</b></span> is checked, yields <strong><span style="color: #008000;">true</span></strong>.</p>
<ul class="ul1">
<li class="li1"><span style="color: #008000;"><b>“value of i: 2”</b></span> gets printed.</li>
<li class="li1"><span class="Apple-converted-space"> </span>Updation is done. Now <span style="color: #008000;"><b>i = 3</b></span>.</li>
</ul>
<p class="p1"><strong>4</strong>. condition <span style="color: #0000ff;"><b>3 <; 4</b></span> is checked, yields <strong><span style="color: #008000;">true</span></strong>.</p>
<ul class="ul1">
<li class="li1"><span style="color: #008000;"><b>“value of i: 3”</b></span> gets printed.</li>
<li class="li1"><span class="Apple-converted-space"> </span>Updation is done. Now <span style="color: #008000;"><b>i = 4</b></span>.</li>
</ul>
<p class="p1"><strong>5</strong>. condition <span style="color: #0000ff;"><b>4 <; 4</b></span> is checked, yields <strong><span style="color: #008000;">false</span></strong>.</p>
<p class="p1"><strong>6</strong>. exit from the while loop.</p>
<h4></h4>
<h4><strong><span style="color: #0000ff;">Example 2</span></strong></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

 <span style="color: #008000;"><strong> // Exit when i becomes greater than 10</strong></span>
 while (i <;= 10) {
 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++
 }
 println("Sum of numbers from 1 to 10: $sum")
}</pre>
<h4></h4>
<h4><strong><span style="color: #0000ff;">Output</span></strong></h4>
<pre>Sum of numbers from 1 to 10: 55</pre>


