Coding/Learn/java/for each loop
Java Foundational Course
01.Introduction to Java
02.Your First Java Program
03.Syntax & Code Structure
04.Comments in Java
05.Variables & Declaration
06.Primitive Data Types
07.Numbers & Arithmetic Operators
08.Strings in Java
09.Booleans & Relational Operators
10.Type Casting & Conversion
11.Scanner & Interactive Input
12.Logical Operators (&&, ||, !)
13.If Statements
14.If-Else Statements
15.If-Else-If Ladders
16.Switch Statements
17.While Loops
18.Do-While Loops
19.Standard For Loops
20.Break & Continue Statements
21.Arrays in Java
22.Array Indexing & Traversal
23.Enhanced For-Each Loop
24.Essential String Methods
25.Methods in Java
26.Method Parameters & Arguments
27.Method Overloading
28.Classes & Objects in Java
29.Constructors in Java
30.Exception Handling (try-catch-finally)
31.Capstone Mini Projects
Course Certificate0%

Complete all lessons to earn

LearnEnhanced For-Each Loop
23 / 31 0%
Lesson 23 of 31
5 mins read

Enhanced For-Each Loop

PreviousNext Lesson
In Plain English

The Enhanced `for-each` loop provides a clean, readable syntax to iterate sequentially through every element in an array or collection. It eliminates index math and completely prevents out-of-bounds bugs.

The Mental Model

"A for-each loop is like a conveyor belt passing items to you one by one. You don't care what number slot it was in; you just pick up each item and process it."

Deep Dive: How It Works

Syntax: `for (type item : collectionOrArray) { /* use item */ }`

Read-only element access: Modifying `item` inside the loop does NOT change the value in the underlying array.

Use standard `for` loop if you need the numerical index `i` or need to modify array elements in-place.

Syntax Blueprint

for (String name : namesArray) {
    System.out.println(name);
}

Reads naturally: "For each String name in namesArray, do something".

PointCore Rules to Remember

Point star
Zero Index Boilerplate: No int i = 0 or arr.length required.
Point star
Readability: Clean, modern, and self-documenting.
Point star
Safety: Mathematically impossible to produce out-of-bounds exceptions.

Common Beginner Traps & How to Fix Them

Mistake:Trying to modify array values through the iteration variable: for (int x : arr) { x = 0; }

Why it happens: x is a copy of the value; modifying x leaves the array unchanged.

How to fix: Use a standard index-based loop when modifying array elements: arr[i] = 0;.

Pro Tip: Use for-each as your default loop for reading arrays unless you explicitly need the index number.

Live Interactive Example

Hit Run Code to see it live
For-Each Array Traversal
Java 13
1
2
3
4
5
6
7
8
9
10
Output Console
Click "Run Code" to view the rendered output.
How it works: The for-each loop cleanly iterates over each element in the tools array.

Your Turn: Micro Challenge

No pressure! Edit the starter code below and test your solution with instant feedback.

Micro Exercise

Print prices with for-each loop

  • PointGiven double[] prices = {9.99, 19.99, 4.99};
  • PointUse a for-each loop to print "Price: $" + p for each price.
1
2
3
4
5
6
7
8
Sandbox Output
Click "Run & Check" to test your solution.

Finished reading and practicing?

Mark this lesson as completed to update your course progress.

PreviousNext Lesson
Overview100 SheetLearnCertificates