AI Features

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

Introduction

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 ...