Java for loop vs Enhanced for loop
Last Updated :
03 Jan, 2025
In Java, loops are fundamental constructs for iterating over data structures or repeating blocks of code. Two commonly used loops are the for loop and the enhanced for loop. While they serve similar purposes, their applications and usage vary based on the scenario.
for loop vs Enhanced for loop
Below comparison highlights the major differences between a traditional for loop and an enhanced for loop in Java.
Parameters | for loop | Enhanced for loop |
---|
Introduced | Introduced in early Java versions as a basic loop structure. | Introduced in Java 5 as a simplified version for iterating over collections/arrays. |
Usage | General-purpose loop suitable for arrays, collections, and numerical ranges. | Specifically designed for iterating over arrays and collections. |
Index-Based Access | Allows access to elements by their index using i . | No index-based access; iterates directly over elements. |
Read-Only Iteration | Supports both read and modify operations on elements. | Primarily for read-only operations. Modification may lead to errors. |
Control Over Iteration | Offers more control over iteration (start, end, and step). | Limited control as it automatically iterates over all elements. |
Syntax Complexity | Slightly verbose; requires initialization, condition, and increment/decrement logic. | Concise and simpler to write. |
Best Use Case | Useful when indices or custom iteration logic is required. | Ideal for directly processing all elements of an array or collection. |
Performance | May perform slightly better when accessing array elements directly by index. | Performance depends on the internal iteration mechanism of the collection. |
for loop in Java
The traditional for loop is a versatile structure used to iterate over a range of values, arrays, or other data structures. It provides complete control over the iteration process for any tasks which need to do multiple times.
Syntax:
for( initialization; conditional_check ; increment_or_decrement_section)
{
// Code to be executed which defined in the conditions
}
Example:
Java
public class Geeks {
public static void main(String[] args) {
int[] n = {1, 2, 3, 4, 5, 6, 7, 8, 9, 10};
// Iterate through the array using a for loop
for (int i = 0; i < n.length; i++) {
System.out.println("Index " + i + ": " + n[i]);
}
}
}
OutputIndex 0: 1
Index 1: 2
Index 2: 3
Index 3: 4
Index 4: 5
Index 5: 6
Index 6: 7
Index 7: 8
Index 8: 9
Index 9: 10
Enhanced for loop (for-each loop)
The enhanced for loop, also known as the for-each loop, is a streamlined way to iterate over collections and arrays. It eliminates the need for managing the loop's counter or bounds manually.
Syntax:
for (data_Type_element : array_Or_Collection) {
// Code to be executed which defined in the conditions
}
Example:
Java
import java.io.*;
import java.util.*;
class Geeks {
public static void main(String[] args) {
int[] arr = { 1, 2, 3, 4, 5, 6, 7, 8, 9, 10 };
// Iterate through the array
// using an enhanced for loop
for (int a : arr) {
System.out.println(a);
}
}
}
Output1
2
3
4
5
6
7
8
9
10