Looping in Java enables the developers to repeat the code functionalities numerous times. The traditional “for” loop and the “enhanced for loop” are two fundamental looping mechanisms in Java that assist in iterating along the target containers.
In this article, we will compare the “for” loop and the “enhanced for” loop.
What is the “for” Loop?
The standard “for” loop in Java follows a specific syntax, consisting of three parts: initialization, condition, and increment. This loop is typically used when the iteration requires greater control over each step. It allows for explicit initialization and modification of loop variables, enabling fine-grained control over iteration.
Syntax
for(initialization; condition; increment/decrement)
Parameters
initialization: It establishes the loop control variable’s starting value.
condition: The loop will keep executing till the condition is true.
increment/decrement: After each iteration, the loop control variable is updated.
Example: Utilization of the “for” Loop
Let’s look at an example code applying the “for” loop:
package com.company;
public class Class1 {
public static void main(String[] args) {
for (int x = 1; x <= 7; x++) {
System.out.print(x + " ");
}
}
}
The above code runs a “for” loop on a variable “x”. The loop iterates 7 times, and the variable is incremented each time the value of “x” is printed on the screen.
Output

What is the Enhanced for Loop (for-Each)?
Conversely, the “enhanced for” loop, also referred to as the “for-Each” loop, provides a simplified syntax for iterating the elements in collections. This loop is particularly beneficial when the developer does not require direct access to individual array elements or their indices. The enhanced for loop automatically handles the iteration, abstracting away the complexities of managing loop variables and conditions.
Syntax
for(item : collection)
In this syntax, “item” represents the variable that holds each item/element in the collection, and “collection” is the iterated array or collection.
Example 1: Utilization of the Enhanced “for” Loop
Let’s consider an example where we want to find the sum of elements in an array using the enhanced for loop:
package com.company;
public class Class1 {
public static void main(String[] args){
int[] marks = {4, 54, 2, 5, 42};
int sum = 0;
for (int mark : marks) {
sum += mark;
}
System.out.println("Sum of the marks are: " + sum);
}
}
In the above lines of code:
- Assign a variable named sum to “0”.
- Then, iterates through each value of the “marks” array via a “for-each” loop.
- The current element’s value is added to the “sum” variable at the start of each iteration.
- The final result of “sum”, which appears after the loop is finished, represents the array’s total number of elements.
- The code ends by printing the given outcome and the calculated sum.
Output

Example 2: Applying the for-Each Loop on an Array
This demonstration applies the “for-Each” loop on an array:
package com.company;
public class Class1 {
public static void main(String[] args){
int arr1[]={3, 65, 76, 23};
int sum=0;
for(int a:arr1){
sum=sum+a;
}
System.out.println("Sum is: "+sum);
}
}
In this block of code:
- Initialize an integer array comprising the stated integers.
- Now, initialize the variable “sum” to zero.
- Each “a” in the “arr1” array element is iterated using the “for-each” loop.
- The “sum” variable is updated with the value of “a” after each iteration.
- Lastly, print the final sum, which reflects the sum of all entries in the array, after the loop is finished.
Output

Difference Between “for” Loop and “Enhanced for” Loop
While both loops have the same purpose of iterating over elements in a collection, they have some differences as well listed below:
Features | for Loop | Enhanced for Loop |
---|---|---|
Control over Iteration Variables | In the traditional for loop, developers have explicit control over the initialization, condition, and incrementation of the loop variables. This allows for granular customization, thereby enabling precise control over the iteration process. | The enhanced for loop automatically handles the variable declaration and iteration process, making it more suitable for situations where direct control over the loop variables is unnecessary. |
Use Cases | The for loop is often used when fine-grained control over the iteration process is required, such as performing mathematical computations on elements, sorting arrays, or filtering elements based on specific conditions. | The enhanced for loop is particularly useful for scenarios where the only requirement is to access each element sequentially, such as printing elements, calculating aggregates, or performing simple operations on each element. |
Data Types | The traditional for loop can be used with any index-based data structure, including arrays and string objects. | The enhanced for loop is specifically designed to work with collections, making it optimal for traversing lists, sets, and queues. |
Conclusion
The “for” loop and “enhanced for” loop are both powerful looping mechanisms in Java. The “for” loop allows fine-grained control and manipulation over loop variables, making it suitable for complex patterns and data structures. On the other hand, the “enhanced for” loop returns a more concise and readable syntax for simple iteration over arrays and collections.