Enhanced for Loop in Java


The enhanced for loop (also known as the “for-each” loop) is used to iterate through elements in arrays or collections more succinctly than the traditional for loop.

Source Code

int[] numbers = {1, 2, 3, 4, 5};
for (int number : numbers) {
    System.out.println(number);
}

Use the enhanced for loop for cleaner code when iterating through arrays or collections when you don’t need the index value.

Subscribe
Notify of
guest
0 Comments
Inline Feedbacks
View all comments