Java For Each Loop
For each loop is an enhanced version of for loop.
It travels each element of the data structure one by one.
Note that you can not skip any element in for loop and it is also not possible to traverse elements in reverse order with the help of for each loop.
It increases the readability of the code.
If you just want to simply traverse an array from start to end then it is recommended to use for each loop.
Syntax :
/* for (int element:Arr) {
Sout(element); //Prints all the elements
} */
Example :
class CWH_forEachLoop{
public static void main(String args[]){
//declaring an array
int arr[]={1,2,3,3,4,5};
//traversing the array with for-each loop
for(int i:arr){
System.out.println(i);
}
}
}
Output
1
2
3
4
5
2
3
4
5
Example
package com.company;
public class cwh_27_arrays {
public static void main(String[] args) {
int [] marks = {98, 45, 79, 99, 80};
// System.out.println(marks.length);
// Displaying the Array (Naive way)
System.out.println("Printing using Naive way");
System.out.println(marks[0]);
System.out.println(marks[1]);
System.out.println(marks[2]);
System.out.println(marks[3]);
System.out.println(marks[4]);
// Displaying the Array (for loop)
System.out.println("Printing using for loop");
for(int i=0;i<marks.length;i++){
System.out.println(marks[i]);
}
}
}
Output
Printing using Naive way
98
45
79
99
80
Printing using for loop
98
45
79
99
80
98
45
79
99
80
Printing using for loop
98
45
79
99
80