for-each loop – JAVA

for-each loop provides the facility to trace the element of array starting from 0 and those up to size of array.

It always takes one step increment (i++/i=i+1).

e.g.

for(int i : x)

{

System.out.println(i);

}

For accessing the value stored at various indexes of x array simply use the variable specified in the for each loop as it implicitly perform the mapping of values in the specified variable.

program:

//PROGRAM TO SHOW WORKING OF for-each LOOP  – JAVA
class ForEachDemo
{
public static void main(String ar[])
{
int A[]={8,4,7,22,344};
for(int i:A)
{
System.out.print(” “+i);
}
}
}

in the above program the value of array (A) will copy into i and then it will show the value one by one by as per each increment

Leave a Reply

Your email address will not be published. Required fields are marked *