Enhanced for loop:

for (String element : array) {

    // rest of code handling current element
}

Traditional for loop equivalent:

for (int i=0; i < array.length; i++) {
    String element = array[i]; 

    // rest of code handling current element
}

Take a look at these forums: https://blogs.oracle.com/CoreJavaTechTips/entry/using_enhanced_for_loops_with

http://www.java-tips.org/java-se-tips/java.lang/the-enhanced-for-loop.html

Answer from user1920811 on Stack Overflow
🌐
GeeksforGeeks
geeksforgeeks.org › java › difference-between-for-loop-and-enhanced-for-loop-in-java
Java for loop vs Enhanced for loop - GeeksforGeeks
July 23, 2025 - 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 ·
Discussions

Can someone explain the enhanced for-loop ? as simple as possible ?
Sure. Say you have an array list of strings (ArrayList list), and you wanted to print out each of the elements in the list. You could do it one of two ways: for (int i = 0; i < list.size(); i++) { System.out.println(list.get(i)); } or method 2: for (String s : list) { System.out.println(s); } This second function is the enhanced for loop. So basically if you're iterating over a list or set, you don't have to write out the full old for loop syntax, you can just write the much more readable enhanced for-loop syntax. Its much more readable because the bounds of the for-loop are very clear (you're iterating over each element of the for loop from start to end) and you immediately have access to each element without calling .get(index). The downsides of the enhanced for-loop is that you don't have access to the index you're at without explicitly keeping track of it, and that it only works on objects that extend the Collections interface, so really only lists and sets. So no enhanced for loops on hashmaps or iterators. More on reddit.com
🌐 r/learnjava
10
5
July 20, 2015
Does Javascript have an enhanced for loop syntax similar to Java's - Stack Overflow
I am wondering if JavaScript has an enhanced for loop syntax that allows you to iterate over arrays. For example, in Java, you can simply do the following: String[] array = "hello there my friend". More on stackoverflow.com
🌐 stackoverflow.com
Why does the "for" loop work so different from other languages like Java or C++ .
Language designer decision. More of a for each than a traditional for but still fundamentally a while loop with some of the work done for you. More on reddit.com
🌐 r/learnpython
70
169
February 16, 2022
What's the purpose of using a for-each/enhanced for-loop?
It makes the code easier to read, allows the use of lambdas (in the case of stream()) and eliminates the need for an index variable and a get() operation required by the classic for loop. If, say, you wanted to print the contents of an ArrayList list using a classic for loop, it would look like this: for (int i = 0; i < list.size(); i++) { System.out.println(list.get(i)); } Using an enhanced for loop, it would look like this: for(String s: list) { System.out.println(s); } There is now no need to use an index variable or a get() operation. More on reddit.com
🌐 r/learnjava
9
14
April 26, 2019
People also ask

How does the enhanced for loop in Java differ from the traditional for loop?
The enhanced for loop simplifies iteration over collections and arrays by abstracting the iterator or index management, focusing solely on the elements. Unlike the traditional for loop which requires initialization, condition, and increment expressions, the enhanced for loop automatically iterates over each element with a cleaner syntax.
🌐
studysmarter.co.uk
studysmarter.co.uk › java enhanced for loop
Java Enhanced For Loop: Definition & Syntax | StudySmarter
What are the limitations of using the Java enhanced for loop?
The Java enhanced for loop cannot be used for modifying elements as it doesn't provide an index or iterator. It doesn't support removing elements safely during iteration. It is not suitable for iterating over multiple collections simultaneously or when you need access to the index of the elements.
🌐
studysmarter.co.uk
studysmarter.co.uk › java enhanced for loop
Java Enhanced For Loop: Definition & Syntax | StudySmarter
How do you iterate over a two-dimensional array using the Java enhanced for loop?
To iterate over a two-dimensional array using the Java enhanced for loop, nest two for-each loops: the outer loop iterates over each array (row) within the main array, and the inner loop iterates over each element within these arrays. Example: `for (int[] row : array) { for (int element : row) { // use element } }`.
🌐
studysmarter.co.uk
studysmarter.co.uk › java enhanced for loop
Java Enhanced For Loop: Definition & Syntax | StudySmarter
🌐
Zero To Mastery
zerotomastery.io › blog › enhanced-for-loop-java
How To Use Enhanced For Loops In Java (aka 'foreach') | Zero To Mastery
January 26, 2024 - The enhanced foreach loop is a way of iterating through elements in arrays and collections in Java. It simplifies the traditional for loop syntax and eliminates the need for manual index management, making your code more readable and less prone ...
🌐
Programiz
programiz.com › java-programming › enhanced-for-loop
Java for-each Loop (With Examples)
In Java, the for-each loop is used to iterate through elements of arrays and collections (like ArrayList). It is also known as the enhanced for loop.
🌐
freeCodeCamp
freecodecamp.org › news › enhanced-for-loops-in-java-how-to-use-foreach-loops-on-arrays
Enhanced For Loops in Java – How to Use ForEach Loops on Arrays
February 17, 2023 - Enhanced loops simplify the way you create for loops. They are mostly used to iterate through an array or collection of variables. In this tutorial, you'll learn the syntax and how to use the for-each loop (enhanced loop) in Java.
Find elsewhere
🌐
CodeGym
codegym.cc › java blog › java collections › enhanced for loop in java
Enhanced for loop in Java
February 13, 2025 - for (elementType element : collection) { // code block to execute } In this syntax, elementType is the data type of the elements in the collection, and element is a variable that represents each element in the collection.
🌐
StudySmarter
studysmarter.co.uk › java enhanced for loop
Java Enhanced For Loop: Definition & Syntax | StudySmarter
The enhanced for loop automates the process of stepping through each element in an array or collection. The syntax simplifies iterating over data types like arrays and all classes that implement the Iterable interface.
🌐
W3Schools Blog
w3schools.blog › home › enhanced for loop java
Enhanced For Loop Java - W3schools.blog
May 19, 2016 - * @author W3spoint */ public class EnhancedForLoopExample { static void enhancedForLoopTest(List<String> arrayList){ //Enhanced For loop test for (String name : arrayList) { System.out.println(name); } } public static void main(String args[]){ //Create ArrayList object.
🌐
JetBrains
jetbrains.com › help › inspectopedia › ForLoopReplaceableByForEach.html
'for' loop can be replaced with enhanced for loop | Inspectopedia Documentation
1 month ago - Reports for loops that iterate over collections or arrays, and can be automatically replaced with an enhanced for loop (foreach iteration syntax).
🌐
Deep Java
digibeatrix.com › home › data handling & collections (java standard library) › java enhanced for loop (for-each): complete guide with examples, differences, and best practices
Java Enhanced for Loop (for-each): Complete Guide with Examples, Differences, and Best Practices - Deep Java
December 27, 2025 - Learn how to use the Java enhanced for loop (for-each statement). This complete guide explains syntax, differences from the standard for loop, performance, common mistakes, and practical examples for arrays, lists, and maps.
🌐
TutorialsPoint
tutorialspoint.com › what-is-enhanced-for-loop-in-Java
what is enhanced for loop in Java?
June 15, 2020 - Expression − This evaluates to the array you need to loop through. The expression can be an array variable or method call that returns an array. In this example, we're showing the use of a foreach loop to print contents of an List of Integers.
🌐
Javatpoint
javatpoint.com › for-each-loop
Java For-each Loop | Enhanced For Loop - javatpoint
Java For-each loop | Java Enhanced For Loop: The for-each loop introduced in Java5. It is mainly used to traverse array or collection elements. The advantage of for-each loop is that it eliminates the possibility of bugs and makes the code more readable.
🌐
Cs226fa21
cs226fa21.github.io › notes › 06-iterators › step01.html
Enhanced For Loop - CS226 Homepage
There is an alternative form, the so-called enhanced for loop: // The colon in the syntax can be read as "in" for (int element : myArray) { System.out.println(element); } The enhanced for loop was introduced in Java 5 as a simpler way to iterate through all the elements of a Collection (not ...
🌐
Mozilla
developer.mozilla.org › en-US › docs › Web › JavaScript › Guide › Loops_and_iteration
Loops and iteration - JavaScript - MDN Web Docs - Mozilla
A for loop repeats until a specified condition evaluates to false. The JavaScript for loop is similar to the Java and C for loop. ... The initializing expression initialization, if any, is executed. This expression usually initializes one or more loop counters, but the syntax allows an expression ...
🌐
How to do in Java
howtodoinjava.com › home › java flow control › enhanced for loop
Java For-each Loop | Enhanced For Loop - HowToDoInJava
January 2, 2023 - The for-each loop does not use the index variable. It uses a variable of the base type of the Collection or the array we will iterate, followed by a colon and finally the array/collection itself.
🌐
Java Community Process
jcp.org › aboutJava › communityprocess › jsr › tiger › enhanced-for.html
An enhanced for loop for the Java™ Programming Language
Note that the cast may generate a warning or error: if Expression has a raw type, then an unchecked assignment warning will be generated, correctly implying that the implicit cast may fail. If Expression has a parameterized type, the statement will generate a compilation error if the type parameter ...
🌐
BeginnersBook
beginnersbook.com › 2024 › 06 › java-for-each-loop-enhanced-for-loop
Java For-each Loop (Enhanced for loop)
It is easier to use than traditional for loop, this is why it is also known as enhanced for loop. Syntax of for-each loop: for (Type element : collection) { // Use element } Here, Type is the type of the elements in the collection, for example, int, String etc.
🌐
Runestone Academy
runestone.academy › ns › books › published › csawesome › Unit6-Arrays › topic-6-3-arrays-with-foreach.html
6.3. Enhanced For-Loop (For-Each) for Arrays — CSAwesome v1
An enhanced for loop header includes a variable, referred to as the enhanced for loop variable, that holds each value in the array. For each iteration of the enhanced for loop, the enhanced for loop variable is assigned a copy of an element without using its index.