Java for loop vs Enhanced for loop Last Updated : 03 Jan, 2025 Comments Improve Suggest changes 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 Java for loop vs Enhanced for loop 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 Java Do While Loop Java do-while loop is an Exit control loop. Unlike for or while loop, a do-while check for the condition after executing the statements of the loop body.Example:Java// Java program to show the use of do while loop public class GFG { public static void main(String[] args) { int c = 1; // Using do-whi 4 min read JavaScript for...of Loop The JavaScript for...of loop is a modern, iteration statement introduced in ECMAScript 2015 (ES6). Works for iterable objects such as arrays, strings, maps, sets, and more. It is better choice for traversing items of iterables compared to traditional for and for in loops, especially when we have bre 3 min read Difference between for and while loop in C, C++, Java In C, C++, and Java, both for loop and while loop is used to repetitively execute a set of statements a specific number of times. However, there are differences in their declaration and control flow. Let's understand the basic differences between a for loop and a while loop. for Loop A for loop prov 5 min read Difference between for and do-while loop in C, C++, Java for loop: for loop provides a concise way of writing the loop structure. Unlike a while loop, a for statement consumes the initialization, condition and increment/decrement in one line thereby providing a shorter, easy to debug structure of looping. Syntax: for (initialization condition; testing con 2 min read Like