springjava.com
  • Core Java
  • Spring Core
  • Spring Boot
  • Spring MVC
  • Angular Tutorial

Related Posts

  • Java for each loop | Enhanced for loop How to iterate Map in Java How to iterate Set in Java How to iterate List in Java Java 9 Stream API Improvements Examples Factory method to create Immutable Collection in Java 9 Java 9 Diamond Operator With Example Java 9 @SafeVarargs Annotation Enhancement Use Java Try with Resources Create Private Methods in Java 9
Core Java

Java for each loop | Enhanced for loop

Last Updated: 08-05-2023 Springjava

In this topic, we will learn about the for-each loop in java. A for-each loop was introduced in Java 1.5. This loop is also known as an enhanced for loop. This loop is used to traverse Collection or Array in java. It helps us avoid programming errors. It makes the code precise and readable. It's easier to implement. This loop avoids the chance of an infinite loop.

Table of content

1. Syntax of for-each loop
2. How does the for-each loop work?
3. Example of traversing the array elements
4. Example of the sum of array elements
5. Example of traversing the collection elements
6. Conclusion

1. Syntax of for-each loop

The syntax of Java for-each loop is containing a data type with the variable name followed by a colon (:)  symbol and then the data source is an array or collection.

for(data_type variable : array){  
//body of for-each loop  
}

for(data_type variable :collection){  
//body of for-each loop  
}

2. How does the for-each loop work?

The Java for-each loop is traversing the array or collection until the last element. The for each element, it stores the element in the variable and then executes the body of the for-each loop.

3. Example of traversing the array elements

public class Test{  
  public static void main(String args[]){  
   //declaring an array  
   int arr[]={10,11,12,13,14};  
   //traversing the array with for-each loop  
   for(int i:arr){  
     System.out.println(i);  
   }  
 }   
}  

Output

10
11
12
13
14

4. Example of the sum of array elements

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

Output

Total: 89

5. Example of traversing the collection elements

import java.util.ArrayList;  
public class Test{  
  public static void main(String args[]){  
   //Creating a list of elements  
   ArrayList<String> list=new ArrayList<String>();  
   list.add("Peter");  
   list.add("Robert");  
   list.add("William");  
   //traversing the ArrayList of elements using for-each loop  
   for(String s:list){  
     System.out.println(s);  
   }  
  }   
}    

Output

Peter
Robert
William

6. Conclusion

In this topic, we learnt about the for-each loop in java and its example.

What is a Java Bean Class? Java 8 Features Write Less Lines Of Code Lambda Expression In Java 8 Features

Leave your thought here

Your email address will not be published. Required fields are marked *

springjava_com
  • springjavateam@gmail.com
  • springjava.com
Learn
    • Core Java
    • Spring Core
    • Spring Boot
    • Spring MVC
    • Angular Tutorial
Quick links
  • About Us
  • Contact Us
  • Privacy Policy
Subscribe to Blog via Email
Subscribe
© 2021 Spring Java. All Rights Reserved.
springjava.com
  • Core Java
  • Spring Core
  • Spring Boot
  • Spring MVC
  • Angular Tutorial
  • About Us
  • Contact Us