⏱️ 7 min read • Beginner Level • Lesson 29
The enhanced for loop in Java, also called the for-each loop, is used to iterate through arrays and collections in a simple and readable way.
It is commonly used when you want to access every element of an array or collection without using an index. Before learning enhanced for loop, you should understand Java for loop, Java arrays, and Java collections.
The enhanced for loop is a simplified version of the normal for loop.
It is used to access each element of an array or collection one by one.
It is also known as the for-each loop.
It works on elements directly, not on index positions.
It makes code shorter, cleaner, and easier to read.
The syntax of enhanced for loop is:
for (dataType variableName : arrayOrCollection) {
// code to be executed
}
The following diagram shows how an enhanced for loop works. Java initializes the loop variable, checks the condition, executes the loop body, updates the variable, and then repeats.
The following example uses an enhanced for loop to print all elements of an integer array.
public class EnhancedForDemo {
public static void main(String[] args) {
int[] numbers = {1, 2, 3, 4, 5};
for (int item : numbers) {
System.out.println("Count is: " + item);
}
}
}
Output:
Count is: 1 Count is: 2 Count is: 3 Count is: 4 Count is: 5
numbers is created.item stores the current element during each iteration.item.
Enhanced for loop can also be used with a String array.
public class StringArrayForEach {
public static void main(String[] args) {
String[] languages = {"Java", "Python", "C++", "JavaScript"};
for (String language : languages) {
System.out.println(language);
}
}
}
Output:
Java Python C++ JavaScript
languages is created.language.
Enhanced for loop is very useful with collections such as ArrayList.
import java.util.ArrayList;
public class ArrayListForEachExample {
public static void main(String[] args) {
ArrayList<String> names = new ArrayList<>();
names.add("Ayan");
names.add("Sarah");
names.add("John");
for (String name : names) {
System.out.println(name);
}
}
}
Output:
Ayan Sarah John
Enhanced for loop is simple and readable, but it has some limitations.
int[] numbers = {10, 20, 30};
for(int item : numbers) {
// Cannot directly access index here
System.out.println(item);
}
Since enhanced for loop does not provide an index variable, use a normal
for loop when index access is required.
for loop when you need index control.
Use enhanced for loop when you only need to read elements one by one.
| Normal for Loop | Enhanced for Loop |
|---|---|
| Uses index or counter variable. | Works directly with elements. |
| Useful when index is required. | Useful when only element values are required. |
| Can iterate forward, backward, or skip elements. | Iterates from first element to last element automatically. |
| Better for modifying array elements by index. | Better for simple reading and printing elements. |
What is the biggest advantage of the enhanced for loop over a normal for loop?
The enhanced for loop makes code shorter and easier to read because it works directly with elements and does not require an index variable.
🧠 Test your understanding with a quick quiz
Topic: Enhanced-for-loop | Language: Java
String[] names = {"Ayan", "Sarah"};
for (String name : names) {
System.out.println(name);
}int[] numbers = {1, 2, 3};
for (int n : numbers) {
System.out.println(n);
}🎉 Great job! Continue learning Java step by step.