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 OverflowW3Schools
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 ...
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
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
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
Can someone explain for loops like I'm 5?
Try writing it all out on paper, erasing and updating each variable. The inner loop is going to be repeated on each iteration of the outer loop. More on reddit.com
What is the Use of For Loop in Java?
1) Used to repeat a block of code many times
2) Helps in iterating over arrays, lists, and collections
3) Improves efficiency in performing repetitive tasks
theknowledgeacademy.com
theknowledgeacademy.com › blog › java-for-loop
Java For Loop: Syntax , Examples, and Types
What is the Best Practice For Loop in Java?
1) Use a for loop when the number of iterations is predetermined
2) Keep the condition simple and clear to prevent infinite loops
3) Prefer enhanced for loops for arrays and collections to improve readability
theknowledgeacademy.com
theknowledgeacademy.com › blog › java-for-loop
Java For Loop: Syntax , Examples, and Types
What are the Related Courses and Blogs Provided by The Knowledge Academy?
The Knowledge Academy offers various Java Courses, including the Java Programming Course, JavaScript for Beginners Course, and the Java Engineer Training. These courses cater to different skill levels, providing comprehensive insights into Constructor Overloading in Java.
Our Programming & Devops Blogs cover a range of topics related to Java, offering valuable resources, best practices, and industry insights. Whether you are a beginner or looking to advance your Programming skills, The Knowledge Academy's diverse courses and informative blogs have got you covered.
theknowledgeacademy.com
theknowledgeacademy.com › blog › java-for-loop
Java For Loop: Syntax , Examples, and Types
Videos
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.
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.
GeeksforGeeks
geeksforgeeks.org › java › loops-in-java
Java Loops - GeeksforGeeks
The for statement includes the initialization, condition, and increment/decrement in one line. Example: The below Java program demonstrates a for loop that prints numbers from 0 to 10 in a single line.
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:
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.
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
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.
Top answer 1 of 4
56
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
2 of 4
10
An enhanced for loop is just limiting the number of parameters inside the parenthesis.
for (int i = 0; i < myArray.length; i++) {
System.out.println(myArray[i]);
}
Can be written as:
for (int myValue : myArray) {
System.out.println(myValue);
}
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]); } } }