You are currently viewing For Each Loop in Java with Example

For Each Loop in Java with Example

For-each Loop in Java

The Java for-each loop, also known as the enhanced for loop.  It provides a different way to traverse an array or collection in Java. It is primarily used to traverse the elements of an array or collection. The for-each loop is so named because it goes through each element one by one.

The enhanced for loop has the disadvantage of not being able to traverse the elements in reverse order. Because it does not work on an index basis, you do not have the option to skip any element. Furthermore, you cannot only traverse the odd or even elements.

However, for traversing the elements of an array or collection, it is recommended to use Java for each loop because it makes the code more readable.

  • It begins with the keyword for, just like a regular for-loop.
  • Instead of declaring and initializing a loop counter variable, you declare a variable of the same type as the array’s base type, followed by a colon, and then the array name.
  • Instead of an indexed array element, you can use the loop variable you created in the loop body.
  • It is commonly used to iterate through an array or a Collections class (eg, ArrayList)
for(data_type variable : array){    
//…code to be executed    
}    
AdvantagesDis- Advantages
1: There is no possibility of a programming error.
2: It makes the readability of the code.
3: This loop makes no use of an index or a counter.
1: Not being able to traverse the elements in reverse order .
2: Because there is no concept of index, you cannot skip any element.
3: You cannot select whether to traverse to odd or even indexed elements.
class Main {
  public static void main(String[] args) {
      
    // create an array
    int[] values = {2, 4, 6, -8};
    
    // iterating through the array 
    for (int value: values) {
       System.out.println(value);
    }
  }
}

Output:

2
4
6
-8

Another Example of: for each loop

class Sample{  
  public static void main(String args[]){  
   int arr[]={10,20,30,40};  
   int total=0;  
   for(int i:arr){  
     total=total+i;  
   }  
  System.out.println("Total is : "+total);  
 }   
}  

Output:

Total is : 100

For-each loop Example: Traversing the item

class Sample{  
  public static void main(String args[]){  
   ArrayList<String> list=new ArrayList<String>();  
   list.add("Sagar");  
   list.add("Avdhesh");  
   list.add("Hitesh");  
   list.add("Sambhav");  
   //traversing the list of elements using for-each loop  
   for(String d:list){  
     System.out.println(d);  
   }  
  
 }   
}  

output

Sagar
Avdhesh
Hitesh
Sambhav

You May like:

Java switch Statement with Example | Java Tutorial

We hope that this article will assist you in understanding all about Java For Each Loop. We have concentrated on making a basic, meaningful, and easy-to-learn guide to the concepts. Still, if you have any problems regarding this, please post them in the comment section, we will be glad to assist you.

for each loop java.for each loop java.for each loop java.for each loop java.for each loop javafor each loop

This Post Has One Comment

Leave a Reply