Java- for each loop / Chapter -6 / Flow Control (1.5v)

javabychetan.blogspot.com
for each loop came into 1.5 version.It is specially designed loop to retrieve elements of arrays and collections.
example 1 :-
Q. print elements of the one-dimensional array?
 int [ ] a={1,2,3,4,5};
 To prints the elements of the array, we have 2 approaches.

1.old approach (old for loop) :-

for(int i=0 ; i<a.lenght ;i++ )              
{                                                      
System.out.ptintln(a[i]);
  }                                                   Output is  :
                                                                1            
                                                                2
                                                                3
                                                                4
                                                                5

2. new approach (for each loop):-

for(int a1 :a )
 {
System.out.ptintln(a1);                              
}      

Output is :  
 
1
2
3
4
5

example 2 :-
Q. print elements of 2-dimensional array?
 int [ ] [ ]a={1,2,3},{4,5}};
 To prints the elements of the 2-d  array we have 2 approaches.

1.old approach (old for loop) :-

for(int i=0 ; i<a.lenght ;i++ )              
{
   for(int j=0 ; j<a.[ i ].length ;j++ )              
   {                                                      
         System.out.ptintln(a[i][j]);                                  
 }                                                                  
                       
Output is :      
1
2
3
4
5                                            

2. new approach (for each loop):-

for(int [ ]a1 :a )
 {
     for(int a2 :a1)
     {
        System.out.ptintln(a2);                
       }
  }
Output is :        
1
2
3
4


Limitations:-

1.for -each loop is the best choice to retrieve elements of array and collections.
   But it is applicable only for array and collection & we can't use this is for general purposes.

example:-
for(int i=0 ; i<3;i++ )              
{                                                           Output is  :
         System.out.ptintln("Hello");                  Hello            
 }                                                                   Hello
      ↑                                                              Hello
 we can't write an equivalent for each loop for above code.              



2.By using normal for ( ) loop we can print array elements in both either in original order or in reverse order . 
But by using for each loop we can print array element only in original order.

example:-
int [ ] x={10,20,30,40};
for(int i=x.length ; i>=0 ; i++ )              
{                                                    
         System.out.ptintln(x[i]);            
 }                                                            

Output is :
40
30
20
10

we can't write an equivalent for each loop, for printing array elements in reverse order in the above code.            


Next:Chapter -7/Flow Control/ break keyword 
Back:Chapter -5/Flow Control/ for() loop

No comments:

Post a Comment

Be the first to comment!

Don't just read and walk away, Your Feedback Is Always Appreciated. I will try to reply to your queries as soon as time allows.

Note:
1. If your question is unrelated to this article, please use our Facebook Page.
2. Please always make use of your name in the comment box instead of anonymous so that i can respond to you through your name and don't make use of Names such as "Admin" or "ADMIN" if you want your Comment to be published.

Regards,
JavaByChetan
Back To Home

Flipkart End Of Season Sale






© Copyright 2017 Javabychetan.blogspot.com