What is Enhanced For Loop in Java?
With the release of Java version 1.5, Java introduced a new kind of for loop known as enhanced for loop.
Syntax of Enhanced for Loop
for( type loopVar : arrayName ){
statement(s);
}
type
should be the data type of elements in the given array, for example- int, String..etc
loopVar
is the variable name that is used to access each element of an array while iteration. The scope of this variable is just inside the loop body.
arrayName
is the name of the given array.
This enhanced for loop is also referred to as for-each loop.
The main reason to introduce the enhanced for loop in Java was to simplify the loop that processes elements of an Array or a Collection.
Let’s understand For-each Loop with an example-
// how to use enhanced for loop with program example
public class EnhancedForLoop{
public static void main(String[] args){
int [] num={1,2,3,4,5,6};
System.out.println("Display elements of an array using enhanced for loop in java:");
for(int i : num){
System.out.print(i+ " ");
}
}
}
Output
Display elements of an Array using enhanced for loop:
1 2 3 4 5 6
From line number 10 to 12 in the above program, you can see the enhanced for loop in action.
The loop iterates through the array named num
i.e at line number 6 and its type being int
How the Enhanced For Loop works?
The working of the loop is very simple, the header of the loop specifies that for each iteration, assign the next element in the array to int
type variable i
and then execute the statement.

working of enhanced for loop
The first iteration, num[0]
is assigned to i
where num[0] is 1.
Then statement System.out.println()
executed inside for loop. Which prints the first element 1 in the output.
After, the first loop is completed the next iteration happens, in the next iteration num[1]
is assigned to i
where num[1] is 2. Again the statement inside the loop is executed. This time it prints 2 in the output.
The loop continues until the last element of the array is processed. Thus, every element is printed.
As you can see that the new way of iterating is simple and easy to use.
Things to avoid while using Enhanced For Loop in Java
Case 1: Avoid declaring the iteration variable outside the loop.
int i;
for(i : num){
............}
Case 2: The type of the iteration variable and the Array doest match
long [] num={ 76L, 12L, 90L};
for( int i : num){
............}
Conclusion
Now, you have learned the use of a for-each loop to iterate through an array of elements. Moreover, we can also use for-each loop or enhanced for loop to iterate through the content of a collection in Java. If you don’t know what is collection framework in Java then you can read more on it.
Hope you liked this article on enhanced for loop Java, Follow us on Facebook and Instagram. You can also subscribe to our newsletter.
Hi,
I like all your posts and follow them to learn java. I am very thankful to your guys for publishing these blogs everyday its adding a lot to my knowledge.
Thank you, Claire, we will continue to make more such java tutorial in a simplified way 🙂