For-each loop in Java

In Java, a for-each loop is essentially a for loop with a modified syntax. It is designed to cycle through a collection of objects such as arrays in a sequential order from start to end, one at a time.


Syntax

The general form of the for-each loop is:

Here

  • type : It is the data type of each element of the collection. For example, in case the collection is an array of integers, type would be int.
  • var : any variable name. Each element of the collection is assigned to this variable, one by one.
  • collection : As the name suggests it is grouping of similar type of data, like an array of integers.

It is also called the enhanced for loop.


An Example

An example would be right to understand the inner workings of for-each loop. The example below prints the sum of all the elements of an integer array:

The output is:

With each iteration of the loop, the next element in the array is retrieved and its value is stored in the temp variable. This value is added to sum variable on each iteration. Here type of temp is int because each element of the array is of type integer.


for-each loop is read only

Inside the loop body, if you do any assignments to the iteration variable it won’t be reflected inside the collection. Let’s see this example:

The output is:

We try to double the value of x, but that doesn’t happen.


Nested for each loop

LIke regular for loop, for-each loop can be nested one inside the other. The example below is of two dimensional array (a matrix). A 2D array is an array of 1D arrays. So if you access any element of the 2D array, you get a 1D array. Now if you access any element of this 1D array, you get a single value.

The output is:

Type of u is an array of integers( int[] ) because each element of 2D array(which is 1D array) is accessed and assigned to u. Type of v is int because each element of u is of type int and this value is assigned to v one by one.


Use of break in for-each loop

For-each loop iterates until all the elements in the collection have been accessed. But sometimes you need to break out of the loop as soon as certain conditions are met, like the value being searched was found in the array, so there is no point in iterating over rest of the array:

The output is:

There are two ways this loop could terminate:

  • On Exhaustion: The key may not be present in the array or present at the last index. So all elements will have to be accessed.
  • Breaking prematurely: As soon as the key is found, work of the loop is done and loop terminates prematurely using break.

Use of continue in for-each loop

Sometimes you want to skip rest of the instructions in a loop but you still want to continue running the loop, continue is a statement made specifically for this purpose .

The output is:

In case when array element is divisible by 2, the continue statement sends the control to the start oof the loop then next element is retrieved and so on.


Labelling for-each loop

You can label a for-each loop using any valid Java token followed by a colon. Like in:

Labelling can be used for many uses. For example, break with a label can be used to break out of a series of nested for-each loops to the end of the label.

The output is :