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
🌐
W3Schools
w3schools.com › java › java_for_loop.asp
Java For Loop
Java Examples Java Videos Java Compiler Java Exercises Java Quiz Java Code Challenges Java Server Java Syllabus Java Study Plan Java Interview Q&A Java Certificate ... When you know exactly how many times you want to loop through a block of code, use the for loop instead of a while loop:
🌐
GeeksforGeeks
geeksforgeeks.org › java › java-for-loop-with-examples
Java For Loop - GeeksforGeeks
2 weeks ago - Note: A "Time Limit Exceeded" error occurs when a program runs longer than the time allocated for execution, due to infinite loops. This is ideal for iterating over a known range or collection. Efficient syntax for initialization, condition checking, and incrementing.
Discussions

foreach - What is the syntax of the enhanced for loop in Java? - Stack Overflow
I have been asked to use the enhanced for loop in my coding. I have only been taught how to use traditional for loops, and as such don't know about the differences between it and the enhanced for... More on stackoverflow.com
🌐 stackoverflow.com
Is there a shorter way to write a for loop in Java? - Stack Overflow
In many cases max value is unknown (data from files, databases etc.). For me the best is loading everything to List (ArrayList etc.) and use for-each loop. Looping through collections is the best way to short loop syntax in my opinion. More on stackoverflow.com
🌐 stackoverflow.com
ELI5: Understanding For Loops in Java
we have 3 loop types: whie (condition) { // do this code } do { // run this code at least once } while (condition); // determines whether the loop should be ran more than once for (declaration; condition; iteration_step) { // do this code } people will often use i for a short lived loop variable - think of it as meaning iteration or something like that for (int i = 0; i < 5; i++) { // some code here } in this for loop we are declaring an int called i, saying to run the loop whilst i is less than 5 and the i++ is done once the body of the loop has been ran More on reddit.com
🌐 r/learnprogramming
5
0
April 14, 2021
Java is fast, code might not be
In practice though, for most enterprise web services, a lot of real world performance comes down to how efficiently you are calling external services (including the database). Just converting a loop of queries into bulk ones can help loads (and then tweaking the query to make good use of indexes, ... More on news.ycombinator.com
🌐 news.ycombinator.com
249
226
3 days ago
🌐
Oracle
docs.oracle.com › javase › tutorial › java › nutsandbolts › for.html
The for Statement (The Java™ Tutorials > Learning the Java Language > Language Basics)
See JDK Release Notes for information about new features, enhancements, and removed or deprecated options for all JDK releases. The for statement provides a compact way to iterate over a range of values. Programmers often refer to it as the "for loop" because of the way in which it repeatedly ...
🌐
Programiz
programiz.com › java-programming › for-loop
Java for Loop (With Examples)
If the condition is true, the body of the for loop is executed. The updateExpression updates the value of initialExpression. The condition is evaluated again. The process continues until the condition is false. To learn more about the conditions, visit Java relational and logical operators.
🌐
YouTube
youtube.com › watch
Java for loops are easy! 🔂 - YouTube
#java #javatutorial #javacourse A for loop in Java is used to repeat a block of code a specific number of times, with control over initialization, condition,...
Published   December 5, 2024
Find elsewhere
🌐
Baeldung
baeldung.com › home › java › core java › java for loop
Java For Loop | Baeldung
1 month ago - Before the first iteration, the loop counter gets initialized, then the condition evaluation is performed followed by the step definition (usually a simple incrementation). ... The initialization, Boolean-expression, and step used in for statements ...
🌐
DataCamp
datacamp.com › doc › java › java-for-loop
Java For Loop
In this example, a nested for loop is used to create a grid-like output. The outer loop iterates over i, and for each iteration of i, the inner loop iterates over j.
🌐
Medium
medium.com › javarevisited › understanding-the-java-for-loop-a-beginners-guide-20df0f83dde5
Understanding the Java for Loop: A Beginner’s Guide | by Mouad Oumous | Javarevisited | Medium
September 26, 2024 - The Java for loop, its structure, uses, and tips for writing efficient loops with increment, decrement, and nested loop examples.
🌐
Reddit
reddit.com › r/learnprogramming › eli5: understanding for loops in java
r/learnprogramming on Reddit: ELI5: Understanding For Loops in Java
April 14, 2021 -

As the title said, can you guys help me understand the for loops in java like I'm five. I've been trying to learn java but when it comes to for loop, I'm having a hard time understanding it. Especially when my professor is using single letters like for int i, x, y, and such something like that. Whenever I'm coding, I always use proper names so I don't get confused with the variables. But I still get confused using for loops;-; so I mostly prefer using the do.. while or while loops. But I want to learn to understand it because I noticed my prof always uses it and as well as ytber programmers.

🌐
Simplilearn
simplilearn.com › home › resources › software development › understanding for loop in java with examples and syntax
Understanding For Loop in Java With Examples and Syntax
July 31, 2025 - Java provides three types of loops, i.e., ✓ for loop ✓ while loop ✓ do-while loop. In this tutorial, you will learn all about for loop in Java. Start now!
Address   5851 Legacy Circle, 6th Floor, Plano, TX 75024 United States
🌐
GeeksforGeeks
geeksforgeeks.org › java › loops-in-java
Java Loops - GeeksforGeeks
Increment/ Decrement: It is used for updating the variable for next iteration. Loop termination:When the condition becomes false, the loop terminates marking the end of its life cycle. Note: There is another form of the for loop known as Enhanced for loop or (for each loop). This loop is used to iterate over arrays or collections. Example: The below Java program demonstrates an Enhanced for loop (for each loop) to iterate through an array and print names.
Published   August 10, 2025
🌐
Tutorialspoint
tutorialspoint.com › java › java_for_loop.htm
Java - for Loop
Modifies the loop counter at the end of each iteration. // Update example i++; // Increments i by 1 after each iteration · The following is the complete syntax of a for loop with all four components:
🌐
freeCodeCamp
freecodecamp.org › news › java-for-loop-example
Java For Loop Example
February 7, 2023 - You can use loops in programming to carry out a set of instructions repeatedly until a certain condition is met. There are three types of loops in Java: for loop. while loop. do...while loop. In this article, we'll focus on the for loop, its synt...
🌐
Hacker News
news.ycombinator.com › item
Java is fast, code might not be | Hacker News
3 days ago - In practice though, for most enterprise web services, a lot of real world performance comes down to how efficiently you are calling external services (including the database). Just converting a loop of queries into bulk ones can help loads (and then tweaking the query to make good use of indexes, ...
🌐
CodeWithHarry
codewithharry.com › tutorial › java-for-loop
for Loop | Java Tutorial | CodeWithHarry
There are three types of loops in Java: for loop · while loop · do...while loop · Whenever a loop needs to be run a specific number of times, we use a for loop. Syntax: for (initializeVariable; testCondition; increment/decrement) { // block of code } initializeVariable: Initialize a new ...
🌐
Tutorial Gateway
tutorialgateway.org › java-for-loop
Java For loop
March 25, 2025 - In the Java for loop, there are three expressions separated by the semi-colons (;) and the execution of these expressions is as follows: Initialization: It starts with the initialization statement.
🌐
Reddit
reddit.com › r/javahelp › for loops
r/javahelp on Reddit: For Loops
October 15, 2022 -

hey I need help really understanding for loops in university. it looked quite simple at first but now it looks all forms of confusing. can someone please explain for loops in a simple fashion maybe in food terms or car terms something that a regular teenager can understand

Top answer
1 of 5
8
https://docs.oracle.com/javase/tutorial/java/nutsandbolts/for.html There are 2 types of for loops the traditional for loop: for (initialization; termination; increment) { statement(s) } as an example: for(int i=1; i<11; i++) { System.out.println("Count is: " + i); } which will output a count of 1 through 10 this is because the value of i will increase by one on the i++ each pass of the for loop. until it hits the termination condition of 11 which will exit the loop. When the termination condition is false the loop exits. As long as it is true the loop will continue and will perform the increment. The second type of loop is known as a for-each loop or enhanced-for loop and is more like python style: int[] numbers = {1,2,3,4,5,6,7,8,9,10}; for(int item : numbers) { System.out.println("Count is: " + item); } I like to read it as a for-in loop in the above scenario I would read it as: for int item in numbers for(int item : numbers) this loop will go through the int[] one at a time and give you each value as the variable. The type declaration must be the same as the array or List that is provided. the major difference between the two loops is that in a for-each loop you do not have the index tracked for example if we did the traditional for loop: int[] numbers = {1,2,3,4,5,6,7,8,9,10}; for(int index=0; index
2 of 5
3
Let's say you have a Hershey's chocolate bar. Let's pretend you are bad at counting so you don't know how many pieces a Hershey bar has (It's 12 btw). You are also a jerk (jk) so you're going to lick each piece of the bar one by one until you have licked the entire thing before begrudgingly sharing it with your friends. Let's also say you have a click counter you swiped from your gym teacher set to zero. So you break up your chocolate bar and set the pieces in a pile, and you have your click counter. To ensure you lick the entire bar, you lick a piece, set it aside, then click the counter. You will continue to do this until you run out of pieces of chocolate in your original pile. That's a for loop. Here's some pseudo code to demonstrate: for (clicker = 0; clicker < pile.size; clicker++) { lick(); movePiece(); } So let's break each part of this down: clicker = 0 clicker is a variable that holds the current number displayed on your click counter. It starts at zero before you begin. clicker < pile.size Next, As long as the number on the clicker is less than the number of pieces of chocolate in your original pile, the loop will continue. clicker++ Once each iteration of the loop completes, add one to the value of clicker. lick(); movePiece(); Each iteration, lick a piece, then move it. That's it. Now all your friends are eating licked pieces of chocolate, and hopefully you understand for loops a little better!
🌐
DataFlair
data-flair.training › blogs › java-for-loop
Java For Loop Syntax and Example - DataFlair
May 8, 2024 - We will be learning about for loops Currently in Loop. value of i = 0 Currently in Loop. value of i = 1 Currently in Loop. value of i = 2 Currently in Loop. value of i = 3 Currently in Loop. value of i = 4 Currently outside Loop. value of i = 5 · package com.dataflair.javaforloop; import java.io.*; public class ArrayTraverse{ public static void main(String []args) { int arrp[] = {1,2,3,4,5,6}; for(int i=0;i<arrp.length;i++) { System.out.println("The "+(i+1)+" element is "+arrp[i]); } } }