Tuesday 28 May 2013

USING ENHANCED FOR LOOP IN JAVA

The enhanced for loop, is particularly applicable for the case of arrays. Its general syntax is as follows:

for(int value : array)
{
     //statements within the for loop
}

Here value is a simple integer variable, that holds subsequent members of the array, in subsequent iteration. The number of iterations is depended on the maximum number of elements in the array. In other words if the array consists of n elements, than the loop will iterate n times.
An example will help you to have a better understanding. Suppose you have an array of 10 elements, so the loop will iterate 10 times and during the first iteration the value will store the array[0], which is the first element of the array, similarly during the second iteration, value will store array[1], and so on.

Consider here a simple example that uses advanced for loop to print the data of array.
Source Code :-
/********************************************************************************/

public class for_example {

public static void main(String[] args) {
int array[]={10,20,30,40,50,60,70,80,90,100,110};

for(int value:array)
{
System.out.println(value);

}
}
}
/*******************************************************************************/

output :-

10
20
30
40
50
60
70
80
90
100
110


Sometimes there may arise situations when we uses the advanced for loop, and it becomes a very tedious task to predict the output of the program.
Source Code:-
/******************************************************************************/
/*PROGRAM SHOWING THE CONCEPT OF 
 * ADVANCED FOR LOOP IN JAVA
 * a test showing the use of advanced for loop
 */

public class test_for {
public static void main(String[]args)
{
int array[]={1,2,3,4};
for(int value:array)
{
array[value]=value;
}
for(int i=0;i<4;i++)
{
System.out.println("array["+i+"] = "+array[i]);
}
}
}
/******************************************************************************/
Output:-
array[0] = 1
array[1] = 1
array[2] = 3
array[3] = 3

Explanation:-
Iteration # 1
During the first iteration the, value is equal to 1, as array[0] is equal to 1.
Now the statement inside the loop becomes arrray[1]=1.
So the second element of array now becomes 1.

Iteration # 2
During the second iteration value is equal to 1, as array[1]=1 (This value was actually modified during the first iteration).
Now the statement inside the loop becomes array[1]=1.
so the second element is again set to 1.

Iteration # 3
During the third iteration, value is equal to 3, as array[2]=3.
The statement inside the loop becomes, array[3]=3.
So the fourth element of array is now set to 3.

Iteration # 4
During the fourth iteration, value becomes 3, as array[3]=3, this was modified in the third iteration.
So the statement inside the loop becomes array[3]=3.
so the fourth element of array is again set to 3.

Note that during these iterations, array[0] and array[2], are never disturbed, by this I mean that, their value is never changed inside the loop, and hence the specified output is obtained.


You can also get an unexpected result by the following program.
/******************************************************************************/ 
/*PROGRAM SHOWING THE CONCEPT OF 
 * ADVANCED FOR LOOP IN JAVA
 * a test showing the use of advanced for loop
 */

public class test2_for {
public static void main(String[]args)
{
int array[]={1,2,3,4};
for(int value:array)
{
array[value]=0;
}
for(int i=0;i<4;i++)
{
System.out.println("array["+i+"] = "+array[i]);
}
}
}
/*****************************************************************************/
Output:-


array[0] = 0
array[1] = 0
array[2] = 3
array[3] = 0


Explanation :-

Iteration # 1
During the first iteration the, value is equal to 1, as array[0] is equal to 1.
Now the statement inside the loop becomes arrray[1]=0.
So the second element of array now becomes 0.

Iteration # 2
During the second iteration value is equal to 0, as array[1]=0 (This value was actually modified during the first iteration).
Now the statement inside the loop becomes array[0]=0.
so the first element is set to 0.

Iteration # 3
During the third iteration, value is equal to 3, as array[2]=3.
The statement inside the loop becomes, array[3]=0.
So the fourth element of array is now set to 0.

Iteration # 4
During the fourth iteration, value becomes 3, as array[3]=0.
So the statement inside the loop becomes array[0]=0.
so the first element of array is again set to 0.

Note that during these iterations, array[2], is never disturbed, it means, it's value is never changed inside the loop, and hence the specified output is obtained.


No comments:

Post a Comment