for-each loop

description of image


for-each Loop


In the last lesson, we used a for loop to access array elements.
In Java, there is another version of for loop that makes it really easy to work with arrays. It is known as the for-each loop. We will see how for-each loop works in this lesson.

For-each Syntax


The syntax of the for-each loop is:


For(dataType item : array) {


// code inside loop


}


Here,


Array – an array e item – each element of the array is assigned to this variable one by One


dataType – the data type of the array.


Next, we will see a working example of it.



Example: for-each Loop


● This program prints the numbers from 35 to 67



Quiz


Question Text