i

JAVA Complete Course

Foreach

We have already covered loop control operations but let’s touch another alternative of for loop. If we are working with collections or arrays, we can use alternative syntax of for loop in order to iterate through the elements of arrays/collections. It is also called as for-each loop because the loop iterates over each element of array/collection.

Below is an example how to iterate over elements of an array using standard for loop:

The same task can be performed with for-each loop as follows:

Let's first look at the syntax of for each loop: