![]() |
![]() |
Java if statement | |
Java if-else statement | |
Java nested if-else | |
Java else-if ladder | |
Java Switch case statement | |
Java Nested Switch Case | |
Java Ternary/Conditional operator |
Java while loop | |
Java do-while loop | |
Java for loop | |
Java Enhanced loop/for each loop | |
Java Labelled for loop | |
Java Nesting of loops | |
Java Break and Continue |
Java Programs | |
Java Keywords Dictionary | |
Java Interview Questions |
Java has implemented a new type loop for iterating on all elements called for each loop.
For each loop or enhanced for loop is one of the most simpler loop than others because it has very few statements in the loop.
Note : If you don't know about for loop then please learn for loop statement first to understand this topic.
for ( type variableName : arrayVariable ) { // body of for each loop }
public class ForEachTest { public static void main(String[] args) { int array [] = {2,44,54,64,532}; for (int copyval : array ) { System.out.println("value : "+copyval); } } }
Note : In above example array concept is used. Learn java array to understand this example easily.
You can also use other type values but, remember the copyval and arrayvariable type must be matched.
public class Test { public static void main(String[] args) { String name[]={"Harry","Micky","Jacob","Larry"}; for (String copyval : name ) { System.out.println("Name : "+copyval); } } }
NOTE : Array must contain atleast 1 element to traverse. |