<p>In this article, you will learn about the do-while loop and how to create do-while loop in Java programming.</p>
<p><span style="color: #0000ff;"><strong>Loops</strong></span> in Java 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>Java do-while Loop</strong></span></h3>
<p class="p1"><span class="s1">Java</span> <span style="color: #008000;"><b>do-while loop</b></span> is an <span style="color: #0000ff;"><strong>e</strong></span><b><span style="color: #0000ff;">xit-control loop</span>, </b><span style="color: #000000;">which means </span>unlike while loop, a 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><strong><span style="color: #0000ff;">Syntax</span></strong></h4>
<pre>do {
 <strong><span style="color: #008000;">//body of the loop</span></strong>
 <span style="color: #008000;"><strong>//statements to be executed</strong></span>
 }
 <span style="color: #008000;"><strong>//test expression is a boolean expression</strong></span>
 while (test_expression);</pre>
<h3></h3>
<h3><span style="color: #000080;"><strong>Flowchart of do-while loop</strong></span></h3>
<p> ;</p>
<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="597" height="495" /></p>
<h3 class="p1"><span style="color: #000080;"><b>How </b></span><span style="color: #000080;"><strong>do-while </strong></span><span style="color: #000080;"><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 <strong><span style="color: #0000ff;">do</span></strong> block and then it 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 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><span style="color: #0000ff;"><strong>Example 1</strong></span></h4>
<pre>public class DoWhileLoopDemo {

 public static void main(String[] args) {

 int i = 1;

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

 <strong><span style="color: #008000;">//increment value of i by 1</span></strong>
 i++;
 }
 <span style="color: #008000;"><strong>//exit when i becomes greater than 3</strong></span>
 while (i <; 4);
 }
}</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>
<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<span style="color: #008000;">. </span></strong><span style="color: #008000;"><span style="color: #000000;">Execute the loop body inside <strong><span style="color: #0000ff;">do</span></strong> block.</span></span></p>
<p class="p1"><strong><span style="color: #008000;">“value of i: 1”</span></strong> gets printed.</p>
<ul>
<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>. <span style="color: #008000;"><span style="color: #000000;">Execute the loop body inside <strong><span style="color: #0000ff;">do</span></strong> block.</span></span></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 <strong><span style="color: #0000ff;">3<; 4</span></strong> is checked, yields true.</p>
<p><strong>6</strong>.<span style="color: #008000;"> <span style="color: #000000;">Execute the loop body inside <strong><span style="color: #0000ff;">do</span></strong> block.</span></span></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 <strong><span style="color: #0000ff;">4<; 4</span></strong> is checked, yields false.</p>
<p><strong>8</strong>. Exit from the do-while loop.</p>
<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>public class DoWhileLoopDemo {

 public static void main(String[] args) {

 int i = 1, 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++;
 }

 <span style="color: #008000;"><strong>// Exit when i becomes greater than 10</strong></span>
 while (i <;= 10);

 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>Sum of numbers from 1 to 10: 55</pre>


