---
title: For-each Loop in Java - Scaler Topics
description: Learn about for each loop in java by Scaler Topics. The for each loop in Java, also called an ‘enhanced for loop’, was introduced in Java 5. Read to know more.
author: Sufiyan Khan
category: Java
---
:::section{.abstract}
The ``for each`` loop in Java, also named `enhanced for loop`, was introduced in **Java 5**, which is used specifically for traversing a collection in Java. It is one of the alternative approaches that is used for traversing arrays. In the `for-each` loop traversal technique, you can directly initialize a variable with the same type as the base type of the array. The variable can be used to access the collection elements without any indexing. It changes to successive elements in the Collection after each iteration.
### Advantages
1. **More convenient**: When we want to iterate through the whole iterable, a `for-each` loop is an easier and simpler way to do this, as we do not have to provide the index of the elements.
2. **It enhances the readability of code**: The syntax of the `for-each` loop is very simple and short, making the code in the loop's body more readable.
3. **Decreases the chances of errors**: The chances of getting errors also get reduced while using the `for-each` loop, as it is generally less prone to errors.
### Syntax
```java
// datatype is the same as that of the elements of the iterable
for(datatype element : iterable) {
// Code
}
```
In the above syntax of the `for-each` loop, you can see that we enter four things inside the parentheses after the `for` keyword.
Those four things are:
* **datatype**
* **variable's name** for storing all the iterable elements one by one
* **colon** symbol followed by
* The **iterable** name which we want to traverse through.
**Note**: The `for-each` loop runs for the whole iterable.
### Example
```java
public class Main {
public static void main(String args[]) {
int arr[] = { 1, 2, 3, 4, 5, 6 };
System.out.println("Printing the elements using the for-each loop:")
for (int elem : arr) {
System.out.print(elem + ", ");
}
}
}
```
**Output**
```plaintext
Printing the elements using the for-each loop:
1, 2, 3, 4, 5, 6,
```
**Explanation:** In the example given above, we have used a `for-each` loop to print all the array `arr` elements one by one without even passing the index of the array elements.
:::
:::section{.main}
## How Does It Work?
Using the following flow diagram, let's examine the code flow in a `for-each` loop.

The above-given flow diagram of a `for each` loop in Java is explained with the help of an example in the steps given below.
The working of the `for each` loop in Java is very interesting. We create a variable in the `for-each` loop's round brackets() that stores each element of the iterable during each iteration. The code written inside the `for-each` loop is executed for that particular element, which is stored in the `for-each` loop variable. In the same way, the code is executed for all the elements of the iterable one by one.
This means that the code written inside the body of a `for-each` loop runs for all the iterable elements sequentially.
:::
:::section{.main}
## Limitations of for-each Loop in Java
Knowing when we can not use a `for-each` loop is also important.
* Whenever we want to loop or iterate over a specific portion of the iterable instead of the whole iterable or modify the iterable, we use a [for loop](https://www.scaler.com/topics/for-loop-in-java/) instead of the `for-each` loop.
* Also note that the `for each` loop in Java can not be executed in a reverse order, unlike the for loop, which can execute the code in the reverse order.
```java
// cannot convert the below traversal using a for-each loop
for (int i=nums.length-1; i>=0; i--)
{
System.out.println(nums[i]);
}
```
* When we need to access the indices of the elements in the iterable, we cannot use for-each loop.
```java
for (int a : nums)
{
if (a == x)
{
return ?; // here, we not able to return the index of element a
}
}
```
* We cannot modify the array elements using the' for-each loop.
```java
for (int num : nums)
{
num = num/2;
//element of nums will not change
}
```
:::
:::section{.summary}
## Conclusion
* `for each` loop in java is also called 'enhanced for loop'.
* `for each` loop in java can be used to iterate over iterables such as arrays, hashmaps and lists in Java.
* It loop does not require the index of iterable elements.
* It can not be executed in reverse order.
* The `foreach` loop in java can not be used to modify the iterable or collection.
* We can use Java's `break` keyword to terminate(stop) the `for-each` loop for some conditions.
:::