<p>In this article, you will learn about how to create for loop in Java programming with the help of examples.</p>
<p><strong><span style="color: #008000;">Loops</span> </strong>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 for Loop</strong></span></h3>
<p>A <strong><span style="color: #0000ff;">for</span></strong> loop is used to repeat a specific block of code a <span style="color: #008000;"><b>known</b></span> number of times.</p>
<p class="p1">It provides a concise way of writing the loop structure. Unlike a while loop, a for statement contains the initialization, test-expression and increment/decrement in a single line.</p>
<p>The for loop is mainly used when you know exactly how many times the loop is about to be executed i.e. with what values will it start and with what values it will end and what will be its test condition.</p>
<h4><span style="color: #0000ff;"><strong>Syntax</strong></span></h4>
<pre>for (initialization; test-expression; updation)
{
 <span style="color: #008000;"><strong> // body of the loop</strong></span>
<span style="color: #008000;"><strong> // statements to be executed</strong></span>
}</pre>
<p class="p1">The various <span style="color: #000000;">parts of the for loop</span> are:</p>
<p><b>1. <span style="color: #0000ff;">initialization</span> :</b> This expression allows you to declare and initialize any loop control variable and is executed first, and only once.<br />
<b></b></p>
<p><b><span style="color: #008000;">Example</span>: </b>int i = 1</p>
<p><b>2. <span style="color: #0000ff;">test-expression:</span></b> In this expression, we have to define the condition i.e. the number of times the body of the loop will execute.</p>
<p><b><span style="color: #008000;">Example</span>: </b>i <;= 10</p>
<p><b>3. <span style="color: #0000ff;">updation</span></b>: This expression allows you to update (increment/decrement) the loop control variable by some value and is executed (every time) after executing the loop body.</p>
<p><b><span style="color: #008000;">Example</span>: </b>i++</p>
<h3></h3>
<h3><span style="color: #000080;"><strong>How for loop works?</strong></span></h3>
<p class="p2">When the <strong><span style="color: #008000;">Control</span> </strong>falls into the for loop, it first executes the initialization expression only once. Then, 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>
<ol class="ul1">
<li>The statements inside the body of <strong><span style="color: #008000;">for</span></strong> loop get executed.</li>
<li class="li1">Then, <strong><span style="color: #008000;">Updation</span></strong> takes place.</li>
<li class="li1">Again, the <strong><span style="color: #008000;">test-expression</span></strong> is evaluated, if it is true, step 1 and step 2 get executed.</li>
<li class="li1">This process goes on until the test expression is evaluated to <span style="color: #008000;"><strong>false</strong></span>.</li>
</ol>
<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">for loop gets terminated and control goes out of the <strong><span style="color: #008000;">for</span></strong> loop.</li>
</ul>
<h3></h3>
<h3><span style="color: #000080;"><strong>Flowchart of for loop</strong></span></h3>
<p><img class=" wp-image-1690 aligncenter" src="https://c1ctech.com/wp-content/uploads/2020/03/Screenshot-2020-03-19-15.16.52-1.png" alt="Screenshot 2020-03-19 15.16.52" width="579" height="587" /></p>
<h4><span style="color: #0000ff;"><strong>Example 1</strong></span></h4>
<pre>public class ForLoop {

 public static void main(String[] args) {

 for (int i = 1; i <; 4; i++) {
 System.out.println("value of i: " + i);
 }
 }
}</pre>
<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><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;"><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><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 ForLoop {

 public static void main(String[] args) {
 
 int sum = 0;

 <strong><span style="color: #008000;">// Exit when i becomes greater than 10</span></strong>
 for (int i = 1; i <;= 10; i++) {

 sum = sum + i;
 
 }
 System.out.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>


