{"id":1166,"date":"2022-02-24T19:23:29","date_gmt":"2022-02-24T19:23:29","guid":{"rendered":"https:\/\/mdr.foobrdigital.com\/?p=1166"},"modified":"2022-02-24T19:23:29","modified_gmt":"2022-02-24T19:23:29","slug":"for-each-loop","status":"publish","type":"post","link":"https:\/\/mudassirbackup.infinitycodestudio.com\/index.php\/2022\/02\/24\/for-each-loop\/","title":{"rendered":"For-each Loop"},"content":{"rendered":"\n<p>In this tutorial, we will learn about the Java for-each loop and its difference with for loop with the help of examples.<\/p>\n\n\n\n<p id=\"introduction\">In Java, the\u00a0<strong>for-each<\/strong>\u00a0loop is used to iterate through elements of\u00a0arrays\u00a0and collections (like\u00a0ArrayList). It is also known as the enhanced for loop.<\/p>\n\n\n\n<hr class=\"wp-block-separator\"\/>\n\n\n\n<h2 class=\"wp-block-heading\">for-each Loop Sytnax<\/h2>\n\n\n\n<p>The syntax of the Java&nbsp;<strong>for-each<\/strong>&nbsp;loop is:<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>for(dataType item : array) {\n    ...\n}<\/code><\/pre>\n\n\n\n<p>Here,<\/p>\n\n\n\n<ul><li><strong>array<\/strong>&nbsp;&#8211; an array or a collection<\/li><li><strong>item<\/strong>&nbsp;&#8211; each item of array\/collection is assigned to this variable<\/li><li><strong>dataType<\/strong>&nbsp;&#8211; the data type of the array\/collection<\/li><\/ul>\n\n\n\n<hr class=\"wp-block-separator\"\/>\n\n\n\n<h2 class=\"wp-block-heading\">Example 1: Print Array Elements<\/h2>\n\n\n\n<pre class=\"wp-block-code\"><code>\/\/ print array elements \n\nclass Main {\n  public static void main(String&#91;] args) {\n      \n    \/\/ create an array\n    int&#91;] numbers = {3, 9, 5, -5};\n    \n    \/\/ for each loop \n    for (int number: numbers) {\n      System.out.println(number);\n    }\n  }\n}<\/code><\/pre>\n\n\n\n<p><strong>Output<\/strong><\/p>\n\n\n\n<pre class=\"wp-block-code\"><code><samp>3\n9\n5\n-5\n<\/samp><\/code><\/pre>\n\n\n\n<p>Here, we have used the&nbsp;<strong>for-each loop<\/strong>&nbsp;to print each element of the&nbsp;<var>numbers<\/var>&nbsp;array one by one.<\/p>\n\n\n\n<ul><li>In the first iteration,&nbsp;<var>item<\/var>&nbsp;will be 3.<\/li><li>In the second iteration,&nbsp;<var>item<\/var>&nbsp;will be 9.<\/li><li>In the third iteration,&nbsp;<var>item<\/var>&nbsp;will be 5.<\/li><li>In the fourth iteration,&nbsp;<var>item<\/var>&nbsp;will be -5.<\/li><\/ul>\n\n\n\n<hr class=\"wp-block-separator\"\/>\n\n\n\n<h2 class=\"wp-block-heading\" id=\"example\">Example 2: Sum of Array Elements<\/h2>\n\n\n\n<pre class=\"wp-block-code\"><code>\/\/ Calculate the sum of all elements of an array\n\nclass Main {\n public static void main(String&#91;] args) {\n  \n   \/\/ an array of numbers\n   int&#91;] numbers = {3, 4, 5, -5, 0, 12};\n   int sum = 0;\n\n   \/\/ iterating through each element of the array \n   for (int number: numbers) {\n     sum += number;\n   }\n  \n   System.out.println(\"Sum = \" + sum);\n }\n}<\/code><\/pre>\n\n\n\n<p><strong>Output<\/strong>:<\/p>\n\n\n\n<pre class=\"wp-block-preformatted\"><samp>Sum = 19<\/samp><\/pre>\n\n\n\n<p>In the above program, the execution of the&nbsp;<code>for each<\/code>&nbsp;loop looks as:<\/p>\n\n\n\n<figure class=\"wp-block-table is-style-stripes\"><table><tbody><tr><th>Iteration<\/th><th>Variables<\/th><\/tr><tr><td>1<\/td><td><var>number<\/var> = 3<br><var>sum<\/var> = 0 + 3 = 3<\/td><\/tr><tr><td>2<\/td><td><var>number<\/var> = 4<br><var>sum<\/var> = 3 + 4 = 7<\/td><\/tr><tr><td>3<\/td><td><var>number<\/var> = 5<br><var>sum<\/var> = 7 + 5 = 12<\/td><\/tr><tr><td>4<\/td><td><var>number<\/var> = -5<br><var>sum<\/var> = 12 + (-5) = 7<\/td><\/tr><tr><td>5<\/td><td><var>number<\/var> = 0<br><var>sum<\/var> = 7 + 0 = 7<\/td><\/tr><tr><td>6<\/td><td><var>number<\/var> = 12<br><var>sum<\/var> = 7 + 12 = 19<\/td><\/tr><\/tbody><\/table><\/figure>\n\n\n\n<p>As we can see, we have added each element of the&nbsp;<var>numbers<\/var>&nbsp;array to the&nbsp;<var>sum<\/var>&nbsp;variable in each iteration of the loop.<\/p>\n\n\n\n<hr class=\"wp-block-separator\"\/>\n\n\n\n<h2 class=\"wp-block-heading\" id=\"difference\">for loop Vs for-each loop<\/h2>\n\n\n\n<p>Let&#8217;s see how a\u00a0<code>for-each<\/code>\u00a0loop is different from a regular\u00a0Java for loop.<\/p>\n\n\n\n<h3 class=\"wp-block-heading\">1. Using for loop<\/h3>\n\n\n\n<pre class=\"wp-block-code\"><code>class Main {\n public static void main(String&#91;] args) {\n    \n   char&#91;] vowels = {'a', 'e', 'i', 'o', 'u'};\n\n   \/\/ iterating through an array using a for loop\n   for (int i = 0; i &lt; vowels.length; ++ i) {\n     System.out.println(vowels&#91;i]);\n   }\n }\n}<\/code><\/pre>\n\n\n\n<p><strong>Output<\/strong>:<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code><samp>a\ne\ni\no\nu<\/samp><\/code><\/pre>\n\n\n\n<h3 class=\"wp-block-heading\">2. Using for-each Loop<\/h3>\n\n\n\n<pre class=\"wp-block-code\"><code>class Main {\n public static void main(String&#91;] args) {\n\n   char&#91;] vowels = {'a', 'e', 'i', 'o', 'u'};\n  \n   \/\/ iterating through an array using the for-each loop\n   for (char item: vowels) {\n     System.out.println(item);\n   }\n }\n}<\/code><\/pre>\n\n\n\n<p><strong>Output<\/strong>:<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code><samp>a\ne\ni\no\nu<\/samp><\/code><\/pre>\n\n\n\n<p>Here, the output of both programs is the same. However, the&nbsp;<strong>for-each<\/strong>&nbsp;loop is easier to write and understand.<\/p>\n\n\n\n<p>This is why the\u00a0<strong>for-each<\/strong>\u00a0loop is preferred over the\u00a0<strong>for<\/strong>\u00a0loop when working with arrays and collections.<a href=\"https:\/\/www.programiz.com\/java-programming\/enhanced-for-loop#example\"><\/a><\/p>\n","protected":false},"excerpt":{"rendered":"<p>In this tutorial, we will learn about the Java for-each loop and its difference with for loop with the help of examples. In Java, the\u00a0for-each\u00a0loop is used to iterate through elements of\u00a0arrays\u00a0and collections (like\u00a0ArrayList). It is also known as the enhanced for loop. for-each Loop Sytnax The syntax of the Java&nbsp;for-each&nbsp;loop is: Here, array&nbsp;&#8211; an [&hellip;]<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[363],"tags":[],"_links":{"self":[{"href":"https:\/\/mudassirbackup.infinitycodestudio.com\/index.php\/wp-json\/wp\/v2\/posts\/1166"}],"collection":[{"href":"https:\/\/mudassirbackup.infinitycodestudio.com\/index.php\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/mudassirbackup.infinitycodestudio.com\/index.php\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/mudassirbackup.infinitycodestudio.com\/index.php\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/mudassirbackup.infinitycodestudio.com\/index.php\/wp-json\/wp\/v2\/comments?post=1166"}],"version-history":[{"count":0,"href":"https:\/\/mudassirbackup.infinitycodestudio.com\/index.php\/wp-json\/wp\/v2\/posts\/1166\/revisions"}],"wp:attachment":[{"href":"https:\/\/mudassirbackup.infinitycodestudio.com\/index.php\/wp-json\/wp\/v2\/media?parent=1166"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/mudassirbackup.infinitycodestudio.com\/index.php\/wp-json\/wp\/v2\/categories?post=1166"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/mudassirbackup.infinitycodestudio.com\/index.php\/wp-json\/wp\/v2\/tags?post=1166"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}