Syntax:

Let us take the example using a String array that you want to iterate over without using any counters. Consider a String array arrData initialized as follows: Although you might know methods like finding the size of the array and then iterating through each element of the array using the traditional for loop (counter, condition, and increment), we need to find a more optimized approach that will not use any such counter. This is the conventional approach of the “for” loop: You can see the use of the counter and then use it as the index for the array. Java provides a way to use the “for” loop that will iterate through each element of the array. Here is the code for the array that we had declared earlier- You can see the difference between the loops. The code has reduced significantly. Also, there is no use of the index or rather the counter in the loop. Do ensure that, the data type declared in the foreach loop must match the data type of the array/list that you are iterating. Here we have the entire class showing the above explanation-

Output: