For-Each Loop in Java

Last Updated : 16 Jan, 2026

The for-each loop in Java (introduced in Java 5) provides a simple, readable way to iterate over arrays and collections without using indexes.

Example: Iterating Over an Array

Java
class Geeks {

    public static void main(String[] args) {

        int[] arr = {1, 2, 3, 4, 5};

        // Using for-each loop
        for (int e : arr) {
            System.out.print(e + " ");
        }
    }
}

Output
1 2 3 4 5 

Explanation:

  • The loop variable e represents each element of the array.
  • The loop automatically moves from the first element to the last.
  • No index variable is required, making the code concise and readable.

Syntax

for (datatype variable : arrayOrCollection) {
// statements using variable
}

Parameters:

  • datatype: The data type of the elements in the array or collection.
  • variable: The variable that holds the current element during each iteration.
  • array: The array or collection being iterated over.

When to Use For-Each Loop?

Use for-each loop when:

  • We do not need the index of elements
  • We only need to read or process elements
  • Code readability and simplicity are important

When to Avoid For-Each Loop?

Avoid using for-each loop when:

  • Index-based access is required
  • Reverse iteration is needed
  • You need to modify elements of a primitive array
  • Complex conditions depend on index positions

Examples of for-each loop

Example 1: Finding Maximum in an Array using for-each Loop

Java
class Geeks {

    public static void main(String[] args) {

        int[] marks = {125, 132, 95, 116, 110};
        int max = findMax(marks);

        System.out.println(max);
    }
    static int findMax(int[] arr) {
        int maximum = arr[0];

        for (int value : arr) {
            if (value > maximum) {
                maximum = value;
            }
        }
        return maximum;
    }
}

Output
132

Explanation:

  • The first element is assumed to be the maximum initially.
  • Each element is compared with the current maximum.
  • If a larger value is found, it replaces the previous maximum.

Example 2: Iterating in a List using for-each loop

Java
import java.util.*;

class Geeks {

    public static void main(String[] args) {

        List<Integer> list = new ArrayList<>();
        list.add(3);
        list.add(5);
        list.add(7);
        list.add(9);

        int max = Integer.MIN_VALUE;

        for (int num : list) {
            if (num > max) {
                max = num;
            }
        }

        System.out.println("List of Integers: " + list);
        System.out.println("Maximum element: " + max);
    }
}

Output
List of Integers [3, 5, 7, 9]
Maximum elemen in a list is: 9

Explanation: In the above example, we use the for-each loop to iterate the list of integer to find the largest or maximum value in the list. Here, we use the Integer.MIN_VALUE which is the minimum value of integer and compare it the element of list and check if the element is greater than the max then update the max value.

Limitations of the For-each Loop

While the for-each loop is convenient, there are some important limitations to consider:

1. Cannot Modify Primitive Array Elements

ffor (int num : marks) {
num = num * 2; // Does not modify array
}

Explanation:The loop variable holds a copy of the value, not the actual array element.

2. No Access to Index

for (int num : numbers) {
if (num == target) {
// do not know the index of 'num' here
return ???; // Index is unavailable in for-each loop
}
}

Explanation: The for-each loop does not provide access to the index of the current element. If we need the index for any reason (e.g., in a search operation), a traditional loop would be more appropriate.

3. Single-direction Iteration Only

// Traditional reverse iteration
for (int i = numbers.length - 1; i >= 0; i--) {
System.out.println(numbers[i]); // Reverse iteration not possible with for-each
}

Explanation: The for-each loop only iterates over the elements in a forward direction. If we need to iterate in reverse, we have to use a traditional for loop with a manually managed index.

Note:

  • The for-each loop works with arrays and all classes that implement Iterable
  • It improves code readability and reduces boilerplate
  • Internally, it uses an iterator for collections
  • Best suited for simple traversal and read-only operations
Comment
Article Tags: