When looping through collections, you can use enhanced loops:

int[] numbers = 
 {1,2,3,4,5,6,7,8,9,10};

for (int item : numbers) {
   System.out.println(item);
}
Answer from Abdulgood89 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:
🌐
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 ...
Discussions

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
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
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
What makes for-loops difficult for beginners?
The syntax of for loops in C-like languages is not exactly intuitive. Even the name "for" seems like a rather non-descriptive, meaningless word. I don't know how for loops are commonly taught, but perhaps it is common to see them introduced with examples like this: for (int i = 0; i < 10; ++i) print i; Now, it is true that the most common use of for loops is to repeat something a certain number of times like this, but if this is how for loops are introduced, then I can understand how they seem mysterious. What in the world is all that garbage syntax doing there for a simple counting loop? Why are there all of those semicolons, and why do you have to say ++i? Etc. It would be better, I think, to teach that the loop for (initialization; condition; update) statement; is (almost) equivalent to initialization; while (condition) { statement; update; } and then show several examples of the same code written in both of these ways to demonstrate and reinforce this equivalence. These examples should include not only the common case of repeating a statement a certain number of times, but also other uses of the for loop, such as finding the length of a C string (where the condition is like *p != '\0'). More on reddit.com
🌐 r/learnprogramming
7
2
March 27, 2013
People also ask

When should I use a for loop versus a while loop in Java?
Use a for loop when the number of iterations is known or when iterating over a range of values. On the other hand, use a while loop when the number of iterations is uncertain or when looping based on a condition that may change during execution. Choose the loop type that best fits your program's specific requirements.
🌐
simplilearn.com
simplilearn.com › home › resources › software development › understanding for loop in java with examples and syntax
Understanding For Loop in Java With Examples and Syntax
What is the difference between a while loop and a do-while loop in Java?
The main difference between a while loop and a do-while loop is that a while loop tests the loop condition before executing the loop body, while a do-while loop executes the loop body at least once and then tests the loop condition.
🌐
simplilearn.com
simplilearn.com › home › resources › software development › understanding for loop in java with examples and syntax
Understanding For Loop in Java With Examples and Syntax
How many types of loops are there in Java?
Java provides three main types of loops: the for loop, the while loop, and the do-while loop. Each type has its syntax and use cases, allowing developers to choose the most appropriate loop for a given scenario.
🌐
simplilearn.com
simplilearn.com › home › resources › software development › understanding for loop in java with examples and syntax
Understanding For Loop in Java With Examples and Syntax
🌐
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.
🌐
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.
🌐
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
🌐
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.
Find elsewhere
🌐
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:
🌐
IONOS
ionos.com › digital guide › websites › web development › for-loop java
Java For-Loop - How to - IONOS
July 31, 2023 - The starting point and the step size can also be specified with the for-loop, since it has an ad­di­tion­al run variable in contrast to the while-loop. The syntax of a for-loop in Java is always the same.
🌐
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
🌐
CodeGym
codegym.cc › java blog › core java › java for loop
For loop Java
In Java indefinite loop or while loop is continuously executed if the boolean condition comes true. The syntax of while loop: while (boolean condition) { loop statements... } Pretty often you may choose which one loop you want to use. Sometimes they are very close and you can use both of them. For example, here is the code for the same task (writing ten times "Hello!" with a number of the line) wrote with while loop:
Published   July 23, 2024
🌐
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 ...
🌐
The Knowledge Academy
theknowledgeacademy.com › blog › java-for-loop
Java For Loop: Syntax , Examples, and Types
February 3, 2026 - The loop runs five times, as i starts from 1 and increments until it reaches 5, printing the statement in each iteration. ... The Java for loop is a powerful tool that allows you to execute repetitive tasks efficiently without writing the same code multiple times. By automating iterations, it simplifies programming and enhances efficiency. Understanding its syntax, components, and types will enable you to tackle various problems in Java with ease.
🌐
ScholarHat
scholarhat.com › home
Understanding for loop in Java (Different Types and Examples)
September 5, 2025 - It is nothing but a repetition control structure that allows us to efficiently write a loop that needs to be executed a specific number of times. It contains the initialization, condition, and updating statements as part of its syntax.
🌐
BeginnersBook
beginnersbook.com › 2015 › 03 › for-loop-in-java-with-example
For loop in Java with example
In the above program: int i=1 is initialization expression i>1 is condition(Boolean expression) i– Decrement operation · A for loop inside another for loop is called nested for loop. Let’s take an example to understand the concept of nested for loop.
🌐
Jenkov
jenkov.com › tutorials › java › for.html
Java for Loops
April 18, 2024 - The third statement increments the value of i. This statement is also executed once per iteration of the for loop, after the body of the for loop is executed. The result of the for loop shown above is that the body of the loop is executed 10 times. Once for each of the values of i that are less than 10 (0 to 9). You don't actually need the curly braces around the for loop body. If you omit the curly braces, then only the first Java statement after the for loop statement is executed.
🌐
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]); } } }
🌐
DigitalOcean
digitalocean.com › community › tutorials › how-to-use-loops-in-java
How To Use Loops in Java | DigitalOcean
January 11, 2023 - There are two main types of loops: while and for loops. What type it is depends on the loop’s syntax and logic. The while loops depend on a Boolean condition. This condition could be general and while it is true, the code block is repeatedly executed. The for loops also repeatedly execute a code block, but the loop condition typically depends on the increment or decrement of an integer variable.
🌐
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.