Site icon C1CTech

Java for-each Loop

<p>In this article&comma; you will learn about the for-each loop and how to create for-each loop in Java programming&period;<&sol;p>&NewLine;<p>Apart from the standard <strong><span style&equals;"color&colon; &num;0000ff&semi;">for<&sol;span><&sol;strong> loop&comma; Java provides you another form of <strong><span style&equals;"color&colon; &num;0000ff&semi;">for<&sol;span><&sol;strong> loop to work with arrays or collection&comma; known as the <strong><span style&equals;"color&colon; &num;008000&semi;">enhanced for loop<&sol;span><&sol;strong>&period;<&sol;p>&NewLine;<p>To learn more about standard <strong><span style&equals;"color&colon; &num;0000ff&semi;">for<&sol;span><&sol;strong> loop in Java visit <strong><span style&equals;"color&colon; &num;0000ff&semi;"><a style&equals;"color&colon; &num;0000ff&semi;" href&equals;"https&colon;&sol;&sol;c1ctech&period;com&sol;java-for-loop&sol;">here<&sol;a><&sol;span><&sol;strong>&period;<&sol;p>&NewLine;<h3><span style&equals;"color&colon; &num;000080&semi;"><strong>Java for-each Loop<&sol;strong><&sol;span><&sol;h3>&NewLine;<p class&equals;"p1">The Java <span style&equals;"color&colon; &num;0000ff&semi;"><strong>for-each<&sol;strong><&sol;span> loop or <strong><span style&equals;"color&colon; &num;0000ff&semi;">enhanced for loop<&sol;span><&sol;strong> provides an alternative approach to traverse through the items of arrays or collection&period;<&sol;p>&NewLine;<p class&equals;"p1">It is known as the <strong><span style&equals;"color&colon; &num;0000ff&semi;">for-each<&sol;span><&sol;strong> loop because it traverses through each element &lpar;sequentially &rpar;of array&sol;collection&period;<&sol;p>&NewLine;<p class&equals;"p1">The purpose of for-each loop is to do something for every element rather than doing something n times&period;<&sol;p>&NewLine;<p>The <span style&equals;"color&colon; &num;0000ff&semi;"><strong>for-each<&sol;strong><&sol;span> loop starts with the keyword <span style&equals;"color&colon; &num;008000&semi;"><b>for<&sol;b><&sol;span> like a normal <strong><span style&equals;"color&colon; &num;0000ff&semi;">for<&sol;span><&sol;strong> loop as shown below&colon;<&sol;p>&NewLine;<h4><span style&equals;"color&colon; &num;0000ff&semi;"><strong>Syntax<&sol;strong><&sol;span><&sol;h4>&NewLine;<pre>for &lpar;data&lowbar;type item &colon; collection&rpar; &lbrace;&NewLine; <strong><span style&equals;"color&colon; &num;008000&semi;">&sol;&sol; Do something to item<&sol;span><&sol;strong>&NewLine;&rcub;<&sol;pre>&NewLine;<p class&equals;"p1">In the above syntax&comma;<&sol;p>&NewLine;<ul class&equals;"ul1">&NewLine;<li class&equals;"li1"><strong><span style&equals;"color&colon; &num;0000ff&semi;">collection<&sol;span><&sol;strong> &colon; refers collection &lpar;ArrayList&rpar; or array variable which you want to traverse through&period;<&sol;li>&NewLine;<li class&equals;"li1"><strong><span style&equals;"color&colon; &num;0000ff&semi;">item<&sol;span><&sol;strong> &colon; refers to a single item from the collection&period;<&sol;li>&NewLine;<&sol;ul>&NewLine;<h3><&sol;h3>&NewLine;<h3><span style&equals;"color&colon; &num;000080&semi;"><strong>How for-each loop works&quest;<&sol;strong><&sol;span><&sol;h3>&NewLine;<p>When the Control falls into the <strong><span style&equals;"color&colon; &num;0000ff&semi;">for-each<&sol;span><&sol;strong> loop&comma; for each iteration&colon;<&sol;p>&NewLine;<ul>&NewLine;<li>traverse through each item in the given collection or array&period;<&sol;li>&NewLine;<li>stores each item in a variable&period;<&sol;li>&NewLine;<li>executes the body of the for-each loop<&sol;li>&NewLine;<&sol;ul>&NewLine;<h3><&sol;h3>&NewLine;<h3><strong><span style&equals;"color&colon; &num;000080&semi;">Traversing the array using for Loop<&sol;span><&sol;strong><&sol;h3>&NewLine;<p class&equals;"p1">Here&&num;8217&semi;s an example to iterate through elements of an array using the standard for loop&colon;<&sol;p>&NewLine;<&excl;-- WP QUADS Content Ad Plugin v&period; 2&period;0&period;98&period;1 -->&NewLine;<div class&equals;"quads-location quads-ad2" id&equals;"quads-ad2" style&equals;"float&colon;none&semi;margin&colon;0px&semi;">&NewLine;&NewLine;<&sol;div>&NewLine;&NewLine;<h4><strong><span style&equals;"color&colon; &num;0000ff&semi;">Example <&sol;span><&sol;strong><&sol;h4>&NewLine;<pre>public class ForLoop &lbrace;&NewLine;&NewLine; public static void main&lpar;String&lbrack;&rsqb; args&rpar; &lbrace;&NewLine; &NewLine; <span style&equals;"color&colon; &num;008000&semi;"><strong>&sol;&sol;declaring an array<&sol;strong><&sol;span>&NewLine; int&lbrack;&rsqb; numbers &equals; &lbrace;10&comma; 20&comma; 30&comma; 40&rcub;&semi;&NewLine;&NewLine; <strong><span style&equals;"color&colon; &num;008000&semi;">&sol;&sol;traversing the array with for loop<&sol;span><&sol;strong>&NewLine; for &lpar;int i &equals; 0&semi; i &lt&semi; numbers&period;length&semi; i&plus;&plus;&rpar; &lbrace;&NewLine; System&period;out&period;println&lpar;numbers&lbrack;i&rsqb;&rpar;&semi;&NewLine; &rcub;&NewLine;&NewLine; &rcub;<&sol;pre>&NewLine;<h3><strong><span style&equals;"color&colon; &num;000080&semi;">Traversing the array u<&sol;span><&sol;strong><strong><span style&equals;"color&colon; &num;000080&semi;">sing for-each Loop<&sol;span><&sol;strong><&sol;h3>&NewLine;<p>You can perform the same task using for-each loop as follows&colon;<&sol;p>&NewLine;<h4><strong><span style&equals;"color&colon; &num;0000ff&semi;">Example <&sol;span><&sol;strong><&sol;h4>&NewLine;<pre>public class ForEachLoop &lbrace;&NewLine;&NewLine; public static void main&lpar;String&lbrack;&rsqb; args&rpar; &lbrace;&NewLine;&NewLine; <strong><span style&equals;"color&colon; &num;008000&semi;">&sol;&sol;declaring an array<&sol;span><&sol;strong>&NewLine; int&lbrack;&rsqb; numbers &equals; &lbrace;10&comma; 20&comma; 30&comma; 40&rcub;&semi;&NewLine;&NewLine; <strong><span style&equals;"color&colon; &num;008000&semi;">&sol;&sol;traversing the array with for-each loop<&sol;span><&sol;strong>&NewLine; for &lpar;int number &colon; numbers&rpar; &lbrace;&NewLine; System&period;out&period;println&lpar;i&rpar;&semi;&NewLine; &rcub;&NewLine; &rcub;&NewLine;&rcub;<&sol;pre>&NewLine;<h4><&sol;h4>&NewLine;<h4><strong><span style&equals;"color&colon; &num;0000ff&semi;">Output<&sol;span><&sol;strong><&sol;h4>&NewLine;<p>The output of both programs will be the same &colon;<&sol;p>&NewLine;<pre>10&NewLine;20&NewLine;30&NewLine;40<&sol;pre>&NewLine;<h3><&sol;h3>&NewLine;<h3><span style&equals;"color&colon; &num;000080&semi;"><strong>Example&colon; for-each Loop<&sol;strong><&sol;span><&sol;h3>&NewLine;<p>The below program will print the sum of elements of an array&period;<&sol;p>&NewLine;<pre>public class ForEachLoop &lbrace;&NewLine;&NewLine; public static void main&lpar;String&lbrack;&rsqb; args&rpar; &lbrace;&NewLine;&NewLine; int&lbrack;&rsqb; numbers &equals; &lbrace;10&comma; 20&comma; 30&comma; 40&rcub;&semi;&NewLine; int sum &equals; 0&semi;&NewLine;&NewLine; for &lpar;int number &colon; numbers&rpar; &lbrace;&NewLine; sum &equals; sum &plus; number&semi;&NewLine; &rcub;&NewLine;&NewLine; System&period;out&period;println&lpar;"Sum &equals; " &plus; sum&rpar;&semi;&NewLine; &rcub;&NewLine;&rcub;<&sol;pre>&NewLine;<h4><&sol;h4>&NewLine;<h4><strong><span style&equals;"color&colon; &num;0000ff&semi;">Output<&sol;span><&sol;strong><&sol;h4>&NewLine;<pre>Sum &equals; 100<&sol;pre>&NewLine;<h4><&sol;h4>&NewLine;<h4><strong><span style&equals;"color&colon; &num;0000ff&semi;">Example explained<&sol;span><&sol;strong><&sol;h4>&NewLine;<p class&equals;"p1">In the above example&comma; during each iteration&comma; the for-each loop&colon;<&sol;p>&NewLine;<ul class&equals;"ul1">&NewLine;<li class&equals;"li1">traverse through each element in the <strong><span style&equals;"color&colon; &num;008000&semi;">numbers<&sol;span><&sol;strong> variable<&sol;li>&NewLine;<li class&equals;"li1">stores it in the <strong><span style&equals;"color&colon; &num;008000&semi;">number<&sol;span><&sol;strong> variable<&sol;li>&NewLine;<li class&equals;"li1">execute the body&comma; i&period;e&period; add a number to sum<&sol;li>&NewLine;<&sol;ul>&NewLine;&NewLine;

Exit mobile version