Translate

Foreach loop in Java.

Foreach loop in Java

 
  • It is a special type of loop which is used mostly with array.
  • It stores each element of array in a variable and executes the body of loop.
  • It starts with for keyword like a normal for loop.
class Easy
{
 public static void main(String[] args) 
 {
  //array declaration
  int ar[]={10,50,60,80,90};
  for (int element:ar) 
   System.out.print(element+" ");       
 }
}
/*
### Output ###
10 50 60 80 90
*/
class Easy
{
 public static void main(String[] args) 
 {
  //array declaration
  int ar[]={10,50,60,80,90};
  for (int i = 0; i <ar.length; i++) 
   System.out.print(ar[i]+" ");       
 }
}
/*
### Output ###
10 50 60 80 90
*/
Next Post Previous Post
No Comment
Add Comment
comment url