For each Loop in Java

For-each is another array traversing technique like for loop, while loop, do-while loop introduced in Java5. 
 

  • It starts with the keyword for like a normal for-loop.
  • Instead of declaring and initializing a loop counter variable, you declare a variable that is the same type as the base type of the array, followed by a colon, which is then followed by the array name.
  • In the loop body, you can use the loop variable you created rather than using an indexed array element. 
     
  • It’s commonly used to iterate over an array or a Collections class (eg, ArrayList)

 

Syntax:

 for (type var : array)

{

statements using var;

}

is equivalent to

for (int i=0; i<arr.length; i++) 
{ 
    type var = arr[i];
   statements using var;
}
 




No comments:

Post a Comment