{"id":3775,"date":"2018-07-29T08:47:03","date_gmt":"2018-07-29T08:47:03","guid":{"rendered":"https:\/\/www.testingdocs.com\/questions\/?p=3775"},"modified":"2024-08-09T08:44:51","modified_gmt":"2024-08-09T08:44:51","slug":"enhanced-for-loop-in-java","status":"publish","type":"post","link":"https:\/\/www.testingdocs.com\/questions\/enhanced-for-loop-in-java\/","title":{"rendered":"Enhanced For Loop in Java"},"content":{"rendered":"<h2>Enhanced For Loop<\/h2>\n<p>The <strong>Enhanced For Loop<\/strong> in Java provides an alternative approach to traverse the array or collection in java and it eliminates the possibility of errors and makes it more readable. Its main purpose is to traverse the array or collection of elements. This loop is also called a <strong>For-Each<\/strong> loop.<\/p>\n<p>It is also known as the for-each loop because it traverses each element one after another sequence and it makes the code readable. Enhanced for loops are simple to use. The <strong>break\/continue<\/strong> statements can also be used to control the behavior of enhanced for loops.<\/p>\n<p>Ex:<\/p>\n<p>The usual way to go through all the elements of an array in order is by using the standard for loop<\/p>\n<p><strong>for(int i=0; i&lt;myArray.length; i++)<\/strong><\/p>\n<p><strong>{<\/strong><\/p>\n<p><strong>System.out.println(myArray[i]);<\/strong><\/p>\n<p><strong>}<\/strong><\/p>\n<p>But by using enhanced for loop is another way similar to the above<\/p>\n<p><strong>for(int arrayElement : array)<\/strong><strong>{<\/strong><\/p>\n<p><strong>System.out.println(arrayElement);<\/strong><\/p>\n<p><strong>}<\/strong><\/p>\n<h3>Syntax<\/h3>\n<p><strong>for(data_type variable : array | collection) <\/strong><strong>{<\/strong><\/p>\n<p><strong>\/\/ body of the for-each loop<\/strong><\/p>\n<p><strong>}<\/strong><\/p>\n<h3>Java Program<\/h3>\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"generic\">public class ForEachLoopDemo {\r\n  public static void main(String[] args) {\r\n    \/\/declaring an array\r\n    int arr[]= {10,20,30,40};\r\n\r\n    \/\/traversing the array with for-each loop\r\n    for(int i:arr) {\r\n      System.out.println(i);\r\n    }\t\t\r\n  }\r\n}\r\n<\/pre>\n<h3>Screenshot<\/h3>\n<p><img loading=\"lazy\" decoding=\"async\" class=\"alignnone size-full wp-image-3786\" src=\"https:\/\/www.testingdocs.com\/questions\/wp-content\/uploads\/enhanced-for-loop.png\" alt=\"enhanced for loop\" width=\"973\" height=\"587\" title=\"\" srcset=\"https:\/\/www.testingdocs.com\/questions\/wp-content\/uploads\/enhanced-for-loop.png 973w, https:\/\/www.testingdocs.com\/questions\/wp-content\/uploads\/enhanced-for-loop-300x181.png 300w, https:\/\/www.testingdocs.com\/questions\/wp-content\/uploads\/enhanced-for-loop-768x463.png 768w\" sizes=\"auto, (max-width: 973px) 100vw, 973px\" \/><\/p>\n<p>&nbsp;<\/p>\n<h3>Program Output<\/h3>\n<p>10<br \/>\n20<br \/>\n30<br \/>\n40<\/p>\n<p>&nbsp;<\/p>\n<p>The disadvantage of the <strong>Enhanced For loop<\/strong> is that it cannot traverse the elements in reverse order because here we do not have any option to skip any element, as it is not dependent on an index.<\/p>\n<p>&#8212;<\/p>\n<p>Java Tutorial on this website:<\/p>\n<p><a href=\"https:\/\/www.testingdocs.com\/java-tutorial\/\">https:\/\/www.testingdocs.com\/java-tutorial\/<\/a><\/p>\n<p>For more information on Java, visit the official website :<\/p>\n<p><a href=\"https:\/\/www.oracle.com\/in\/java\/\" rel=\"noopener\">https:\/\/www.oracle.com\/in\/java\/<\/a><\/p>\n","protected":false},"excerpt":{"rendered":"<p>Enhanced For Loop The Enhanced For Loop in Java provides an alternative approach to traverse the array or collection in java and it eliminates the possibility of errors and makes it more readable. Its main purpose is to traverse the array or collection of elements. This loop is also called a For-Each loop. It is [&hellip;]<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[40],"tags":[],"class_list":["post-3775","post","type-post","status-publish","format-standard","hentry","category-java","has-post-title","has-post-date","has-post-category","has-post-tag","has-post-comment","has-post-author",""],"_links":{"self":[{"href":"https:\/\/www.testingdocs.com\/questions\/wp-json\/wp\/v2\/posts\/3775","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/www.testingdocs.com\/questions\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/www.testingdocs.com\/questions\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/www.testingdocs.com\/questions\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/www.testingdocs.com\/questions\/wp-json\/wp\/v2\/comments?post=3775"}],"version-history":[{"count":9,"href":"https:\/\/www.testingdocs.com\/questions\/wp-json\/wp\/v2\/posts\/3775\/revisions"}],"predecessor-version":[{"id":23797,"href":"https:\/\/www.testingdocs.com\/questions\/wp-json\/wp\/v2\/posts\/3775\/revisions\/23797"}],"wp:attachment":[{"href":"https:\/\/www.testingdocs.com\/questions\/wp-json\/wp\/v2\/media?parent=3775"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.testingdocs.com\/questions\/wp-json\/wp\/v2\/categories?post=3775"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.testingdocs.com\/questions\/wp-json\/wp\/v2\/tags?post=3775"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}