W3Schools Java For Each Loop Tutorial Guide

Robert Jul 13, 2026

Enhance your Java proficiency by mastering the "for each" loop, a powerful tool for easy and efficient iteration. W3Schools, a renowned online learning platform, provides an excellent guide to understand and implement this construct.

the quick and easy way to learn how to use loops
the quick and easy way to learn how to use loops

The "for each" loop simplifies the process of traversing arrays or collections in Java. It offers a sleek alternative to traditional loop structures like "for" and "while". Let's dive into the essential aspects of the "for each" loop using W3Schools as our trusted guide.

an image of a computer screen with text that reads 1 for loop used when the number of
an image of a computer screen with text that reads 1 for loop used when the number of

The Foundation: Syntax and Declaration

The "for each" loop's syntax is concise and easy to understand. It takes away the need to manage loop counters and indices. Instead, it directly works with the elements of the array or collection.

a poster with different types of text and symbols on the back ground, including an image of
a poster with different types of text and symbols on the back ground, including an image of

The basic syntax is: for (type variable : arrayOrCollection) { // code to be executed } Replace type with your chosen data type, and variable with the name you want to use for the elements. arrayOrCollection is the array or collection you want to iterate over.

Iterating through Arrays

Loops In Java – Best And Complete Guide For Beginners
Loops In Java – Best And Complete Guide For Beginners

Arrays are the most common collection types in Java. Using the "for each" loop to traverse them couldn't be simpler: int[] numbers = {10, 20, 30}; for (int i : numbers) { System.out.println(i); } This code will print every integer in the numbers array.

What if you need the index as well? Java 5 and later versions support the Arrays.parallelPrefix() method, which can help you maintain the original index while using a "for each" loop.

Iterating through Collection Classes

Java for Loop (With Examples)
Java for Loop (With Examples)

The "for each" loop works perfectly with all collection classes like List, Set, and Map. It saves you the trouble of creating an iterator and manually managing the traversal process: ArrayList<String> cars = new ArrayList<String>(); cars.add("Volvo"); cars.add("BMW"); cars.add("Ford"); for (String i : cars) { System.out.println(i); } Here, string variables are used to iterate over the ArrayList<String> of cars.

The output of this code snippet is each car model in the List followed by a newline character.

"For Each" Loop with Multiple Variables

Java While Loop Explained 🔁 | Easy Guide for Beginners
Java While Loop Explained 🔁 | Easy Guide for Beginners

W3Schools also demonstrates how to use the "for each" loop with multiple variables simultaneously, mimicking the functionality of the zip() function in other languages. This can be done using index-based loops, but the "for each" loop offers a more elegant solution.

Combining Two Arrays or Lists

For Loop vs While Loop vs Do-While Loop | Java Loop Comparison
For Loop vs While Loop vs Do-While Loop | Java Loop Comparison
the text 4 for each loop used to treat over arrays and collections without dealing with indexes
the text 4 for each loop used to treat over arrays and collections without dealing with indexes
Loops in Java
Loops in Java
Coding with Parvez
Coding with Parvez
🚀 Master JavaScript Loops: For, While, Do-While, For-In & For-Of Loops Explained! 💻✨
🚀 Master JavaScript Loops: For, While, Do-While, For-In & For-Of Loops Explained! 💻✨
Loops in java
Loops in java
While Loop in Java Explained | Java Programming Basics for Beginners
While Loop in Java Explained | Java Programming Basics for Beginners
Java Loops - An Ultimate Guide to Master the Concept
Java Loops - An Ultimate Guide to Master the Concept
an image of a computer screen with the text 6 enhanced control with break & continue
an image of a computer screen with the text 6 enhanced control with break & continue
the text 3 do - while loop is displayed above an image of a computer screen
the text 3 do - while loop is displayed above an image of a computer screen
an image of a computer screen with the text 5 nested loops loops inside other loops, often used for multi - dimensional data
an image of a computer screen with the text 5 nested loops loops inside other loops, often used for multi - dimensional data
ap Computer Science java programming notes
ap Computer Science java programming notes
two while loop executes a block of code as long as the condition remains true
two while loop executes a block of code as long as the condition remains true
For loop in Java programming language - Codeforcoding
For loop in Java programming language - Codeforcoding
For Loop in Java Explained | Java Programming Basics for Beginners
For Loop in Java Explained | Java Programming Basics for Beginners
Java  Loops
Java Loops
🟠 UNDERSTANDING JAVA LOOPS 🔄
🟠 UNDERSTANDING JAVA LOOPS 🔄
Complete Java Roadmap for Beginners in 2026
Complete Java Roadmap for Beginners in 2026
Java Basics in 15 Minutes ☕ | Beginner-Friendly Java Crash Course
Java Basics in 15 Minutes ☕ | Beginner-Friendly Java Crash Course

To combine two arrays or lists with the "for each" loop, use two separate loop variables: int[] numbers = {1, 2, 3}; String[] words = { "a", "b", "c"}; for (int i : numbers) { System.out.println(i + " " + words[i]); } In this example, both integer and string variables are used to iterate over their respective arrays simultaneously.

The output of this code snippet is the concatenation of the elements from the numbers and words arrays on each iteration.

Combining an Array and a List

You can also combine an array with a list using the "for each" loop. The code is almost identical to the previous example: swap the array and list variables, and adapt the iterator accordingly: int[] numbers = {1, 2, 3}; ArrayList<String> words = new ArrayList<String>(); words.add("a"); words.add("b"); words.add("c"); for (int i = 0; i < numbers.length; i++) { System.out.println(numbers[i] + " " + words.get(i)); } This example also uses two loop variables to traverse an integer array and a string list simultaneously.

The output and principles of this code snippet are the same as in the previous example.

With this comprehensive understanding of the "for each" loop from W3Schools, you're now ready to improve your Java skills and streamline your coding workflow.