Java for loop vs Enhanced for loop Last Updated : 03 Jan, 2025 Summarize Comments Improve Suggest changes Share Like Article Like Report 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 loopBelow comparison highlights the major differences between a traditional for loop and an enhanced for loop in Java.Parametersfor loopEnhanced for loopIntroducedIntroduced in early Java versions as a basic loop structure.Introduced in Java 5 as a simplified version for iterating over collections/arrays.UsageGeneral-purpose loop suitable for arrays, collections, and numerical ranges.Specifically designed for iterating over arrays and collections.Index-Based AccessAllows access to elements by their index using i.No index-based access; iterates directly over elements.Read-Only IterationSupports both read and modify operations on elements.Primarily for read-only operations. Modification may lead to errors.Control Over IterationOffers more control over iteration (start, end, and step).Limited control as it automatically iterates over all elements.Syntax ComplexitySlightly verbose; requires initialization, condition, and increment/decrement logic.Concise and simpler to write.Best Use CaseUseful when indices or custom iteration logic is required.Ideal for directly processing all elements of an array or collection.PerformanceMay perform slightly better when accessing array elements directly by index.Performance depends on the internal iteration mechanism of the collection.for loop in JavaThe 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 Comment More infoAdvertise with us Next Article For Loop in Java | Important points M mroshanmishra0072 Follow Improve Article Tags : Java Technical Scripter Difference Between Technical Scripter 2020 Java-Loops +1 More Practice Tags : Java Similar Reads For-Each Loop in Java The for-each loop in Java (also called the enhanced for loop) was introduced in Java 5 to simplify iteration over arrays and collections. It is cleaner and more readable than the traditional for loop and is commonly used when the exact index of an element is not required.Example: Using a for-each lo 8 min read String Array with Enhanced For Loop in Java Enhanced for loop(for-each loop) was introduced in java version 1.5 and it is also a control flow statement that iterates a part of the program multiple times. This for-loop provides another way for traversing the array or collections and hence it is mainly used for traversing arrays or collections. 1 min read For Loop in Java | Important points Looping in programming languages is a feature that facilitates the execution of a set of instructions/functions repeatedly while some condition evaluates to true. In Java, just unlikely any programming language provides four ways for executing the loops namely while loop, for loop, for-each loop, do 6 min read Java - Loop Through an Array In Java, looping through an array or Iterating over arrays means accessing the elements of the array one by one. We have multiple ways to loop through an array in Java. Example 1: Here, we are using the most simple method i.e. using for loop to loop through an array.Java// Java program to loop throu 3 min read Java For Loop Java for loop is a control flow statement that allows code to be executed repeatedly based on a given condition. The for loop in Java provides an efficient way to iterate over a range of values, execute code multiple times, or traverse arrays and collections.Now let's go through a simple Java for lo 4 min read Output of Java Programs | Set 40 (for loop) Prerequisite : Loops in Java 1. what will be the output of the following program? JAVA public class Test { public static void main(String[] args) { for (int i = 0; i < 10; i++) int x = 10; } } Options: 1. No Output 2. 10 3. Compile time error 4. 10 (10 times) The answer is option (3) Explanation: 3 min read Like