<p>In this article, you will learn about the for-each loop and how to create for-each loop in Java programming.</p>
<p>Apart from the standard <strong><span style="color: #0000ff;">for</span></strong> loop, Java provides you another form of <strong><span style="color: #0000ff;">for</span></strong> loop to work with arrays or collection, known as the <strong><span style="color: #008000;">enhanced for loop</span></strong>.</p>
<p>To learn more about standard <strong><span style="color: #0000ff;">for</span></strong> loop in Java visit <strong><span style="color: #0000ff;"><a style="color: #0000ff;" href="https://c1ctech.com/java-for-loop/">here</a></span></strong>.</p>
<h3><span style="color: #000080;"><strong>Java for-each Loop</strong></span></h3>
<p class="p1">The Java <span style="color: #0000ff;"><strong>for-each</strong></span> loop or <strong><span style="color: #0000ff;">enhanced for loop</span></strong> provides an alternative approach to traverse through the items of arrays or collection.</p>
<p class="p1">It is known as the <strong><span style="color: #0000ff;">for-each</span></strong> loop because it traverses through each element (sequentially )of array/collection.</p>
<p class="p1">The purpose of for-each loop is to do something for every element rather than doing something n times.</p>
<p>The <span style="color: #0000ff;"><strong>for-each</strong></span> loop starts with the keyword <span style="color: #008000;"><b>for</b></span> like a normal <strong><span style="color: #0000ff;">for</span></strong> loop as shown below:</p>
<h4><span style="color: #0000ff;"><strong>Syntax</strong></span></h4>
<pre>for (data_type item : collection) {
 <strong><span style="color: #008000;">// Do something to item</span></strong>
}</pre>
<p class="p1">In the above syntax,</p>
<ul class="ul1">
<li class="li1"><strong><span style="color: #0000ff;">collection</span></strong> : refers collection (ArrayList) or array variable which you want to traverse through.</li>
<li class="li1"><strong><span style="color: #0000ff;">item</span></strong> : refers to a single item from the collection.</li>
</ul>
<h3></h3>
<h3><span style="color: #000080;"><strong>How for-each loop works?</strong></span></h3>
<p>When the Control falls into the <strong><span style="color: #0000ff;">for-each</span></strong> loop, for each iteration:</p>
<ul>
<li>traverse through each item in the given collection or array.</li>
<li>stores each item in a variable.</li>
<li>executes the body of the for-each loop</li>
</ul>
<h3></h3>
<h3><strong><span style="color: #000080;">Traversing the array using for Loop</span></strong></h3>
<p class="p1">Here&#8217;s an example to iterate through elements of an array using the standard for loop:</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>

<h4><strong><span style="color: #0000ff;">Example </span></strong></h4>
<pre>public class ForLoop {

 public static void main(String[] args) {
 
 <span style="color: #008000;"><strong>//declaring an array</strong></span>
 int[] numbers = {10, 20, 30, 40};

 <strong><span style="color: #008000;">//traversing the array with for loop</span></strong>
 for (int i = 0; i <; numbers.length; i++) {
 System.out.println(numbers[i]);
 }

 }</pre>
<h3><strong><span style="color: #000080;">Traversing the array u</span></strong><strong><span style="color: #000080;">sing for-each Loop</span></strong></h3>
<p>You can perform the same task using for-each loop as follows:</p>
<h4><strong><span style="color: #0000ff;">Example </span></strong></h4>
<pre>public class ForEachLoop {

 public static void main(String[] args) {

 <strong><span style="color: #008000;">//declaring an array</span></strong>
 int[] numbers = {10, 20, 30, 40};

 <strong><span style="color: #008000;">//traversing the array with for-each loop</span></strong>
 for (int number : numbers) {
 System.out.println(i);
 }
 }
}</pre>
<h4></h4>
<h4><strong><span style="color: #0000ff;">Output</span></strong></h4>
<p>The output of both programs will be the same :</p>
<pre>10
20
30
40</pre>
<h3></h3>
<h3><span style="color: #000080;"><strong>Example: for-each Loop</strong></span></h3>
<p>The below program will print the sum of elements of an array.</p>
<pre>public class ForEachLoop {

 public static void main(String[] args) {

 int[] numbers = {10, 20, 30, 40};
 int sum = 0;

 for (int number : numbers) {
 sum = sum + number;
 }

 System.out.println("Sum = " + sum);
 }
}</pre>
<h4></h4>
<h4><strong><span style="color: #0000ff;">Output</span></strong></h4>
<pre>Sum = 100</pre>
<h4></h4>
<h4><strong><span style="color: #0000ff;">Example explained</span></strong></h4>
<p class="p1">In the above example, during each iteration, the for-each loop:</p>
<ul class="ul1">
<li class="li1">traverse through each element in the <strong><span style="color: #008000;">numbers</span></strong> variable</li>
<li class="li1">stores it in the <strong><span style="color: #008000;">number</span></strong> variable</li>
<li class="li1">execute the body, i.e. add a number to sum</li>
</ul>


