- ✕This summary was generated using AI based on multiple online sources. To view the original source information, use the "Learn more" links.
The for-each loop (also called the enhanced for loop) in Java provides a concise way to iterate over arrays or collections without using an index variable. It was introduced in Java 5 and is ideal for read-only traversal.
Example – Iterating Over an Array
public class Example {public static void main(String[] args) {int[] numbers = {1, 2, 3, 4, 5};// For-each loopfor (int num : numbers) {System.out.println(num);}}}Copied!✕CopyOutput:
12345Copied!✕CopyHere, num takes the value of each element in numbers sequentially, eliminating the need for manual indexing.
Syntax
for (datatype variable : arrayOrCollection) {// use variable}Copied!✕CopyWorks with arrays and any object implementing Iterable (e.g., List, Set).
Internally uses an iterator for collections and an index counter for arrays.
Example – Iterating Over a List
import java.util.*;public class Example {public static void main(String[] args) {List<String> fruits = Arrays.asList("Apple", "Banana", "Cherry");for (String fruit : fruits) {System.out.println(fruit);}}}Copied!✕Copy Java For Loop - W3Schools
In this example, the loop starts with i = 10. The condition i < 5 is already false, so the loop body is skipped, and nothing is printed.
See results only from w3schools.comTry It Yourself
The W3Schools online code editor allows you to edit code and view the result in …
Java User Input
Java User Input The Scanner class is used to get user input, and it is found in the …
Java Data Types
Note: This rule makes Java safer, because the compiler will stop you if you try to mix …
Java Variables
Java Variables Variables are containers for storing data values. In Java, there are …
Java Methods
Create a Method A method must be declared within a class. It is defined with …
W3Schools Tryit Editor
The W3Schools online code editor allows you to edit code and view the result in …
Real-Life Examples
Real-Life Examples To demonstrate a practical example of the for loop, let's …
W3schools Exercise
I completed a JAVA exercise on w3schools.com You completed the JAVA …
Nested Loops
Nested Loops It is also possible to place a loop inside another loop. This is called a …
Java Operators
Java Operators Operators are used to perform operations on variables and …
Java For Loop - GeeksforGeeks
Mar 11, 2026 · The for loop in Java is a control flow statement used to execute a block of code repeatedly based on a condition. It is especially useful when the …
The for Statement (The Java™ Tutorials > Learning the Java ... - Oracle
The for Statement 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 loops until a …
Java for Loop (With Examples) - Programiz
In this tutorial, we will learn how to use for loop in Java with the help of examples and we will also learn about the working of Loop in computer programming.
Check if a variable is between two numbers with Java
And, like others have said, Java doesn't have an elegant syntax for checking if a …
- Reviews: 6
Printing Even Numbers with Java Using Loops - Medium
Jul 14, 2025 · Looping over a range of numbers and filtering for even values is one of the simplest but most effective tasks for learning control flow in Java. It lets …
- People also ask
Java For Loop - Baeldung
Feb 25, 2026 · A for loop is a control structure that allows us to repeat certain operations by incrementing and evaluating a loop counter. Before the first …
Mastering the `for` Loop in Java - javaspring.net
Jan 16, 2026 · The for loop is a powerful and versatile construct in Java that allows you to iterate over a sequence of values. By understanding its fundamental concepts, usage methods, common practices, …
How to Write a Java Statement to Display Integers Between Two Given ...
Learn how to create a Java statement that prints all integers between two specified numbers with a step-by-step guide and code example.
Loops in java - For, While, Do-While Loop in Java
This Java program generates a triangle pattern with numbers increasing sequentially from 1 to the row number. The outer loop is a for loop, while the …
Deep dive into For Loop in Java Between Numbers