<p class="p1">In this article, you will learn about while loop and how to create while loop in Java programming.</p>
<p>Loops in Java are used when we want to execute a block of statements repeatedly until a specific condition is met(condition is <strong><span style="color: #008000;">false</span></strong>).</p>
<h3><span style="color: #000080;"><strong>Java 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><span style="color: #0000ff;"><strong>Syntax</strong></span></h4>
<pre><span style="color: #0000ff;"><strong>//while loop
</strong></span>
<span style="color: #008000;"><strong>//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 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><span style="color: #008000;"><span style="color: #000000;">(boolean expression)</span></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 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 <strong><span style="color: #008000;">false</span></strong>.</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><span style="color: #0000ff;"><strong>Flowchart</strong></span></h4>
<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="562" height="509" /></p>
<h4><span style="color: #0000ff;"><strong>Example 1</strong></span></h4>
<pre>public class WhileLoop {

 public static void main(String[] args) {
<strong><span style="color: #008000;"> 
 //initialization</span></strong>
 int i = 1;

<strong><span style="color: #008000;"> //exit when i becomes greater than 3</span></strong>
 while (i <; 4) {
 System.out.println("value of i: " +i);
 <strong><span style="color: #008000;">//updation</span></strong>
 i++;
 }
 }
}</pre>
<h4></h4>
<h4><span style="color: #0000ff;"><strong>Output</strong></span></h4>
<pre>value of i: 1
value of i: 2
value of i: 3</pre>
<h4></h4>
<h4><span style="color: #0000ff;"><strong>Example explained</strong></span></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 true.</p>
<ul class="ul1">
<li class="li1"><span style="color: #008000;"><b> &#8220;value of i: 1&#8221;</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 true.</p>
<ul class="ul1">
<li class="li1"><span style="color: #008000;"><b>&#8220;value of i: 2&#8221;</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 true.</p>
<ul class="ul1">
<li class="li1"><span style="color: #008000;"><b>&#8220;value of i: 3&#8221;</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 false.</p>
<p class="p1"><strong>6</strong>. exit from the while loop.</p>
<h4></h4>
<h4><span style="color: #0000ff;"><strong>Example 2</strong></span></h4>
<p class="p1">The below program will print the sum of natural numbers from 1 to 10.</p>
<pre>public class WhileLoop {

 public static void main(String[] args) {

 int i = 1, sum = 0;

 <strong><span style="color: #008000;">// exit when i becomes greater than 10</span></strong>
 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++;
 }
 System.out.println("Sum of numbers from 1 to 10: " + sum);
 }

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


