Enhanced for Loop for Arrays

Learn about 'for each' loop and how it works in Java.

Java provides a special kind of for loop that can be used with arrays. This loop is much easier to write because it doesn’t involve an index variable or the use of []. Let’s get started.

The for each loop


To set up a for each loop, look at the snippet below:

for (datatype variable: arrayname)

The datatype specifies the type of values in the array called arrayname. The variable refers to the enhanced for loop variable. It reads: for each variable value in arrayname.


Let’s look at an example.

Get hands-on with 1200+ tech skills courses.