Saturday 10 June 2017

Java(for loop)

                                                    For loop             

Execute a sequence of statements multiple times and abbreviates the code that manages the loop variable.
 The Java for loop is used to iterate a part of the program several times. If the number of iteration is fixed, it is recommended to use for loop.

There are many type of for loop

  1. Simple For Loop
  2. For-each or Enhanced For Loop
  3. Labeled For Loop

            
Java simple for loop

The simple for loop is same as C/C++. We can initialize variable, check condition and increment/decrement value.

Syntax:
           for(initialization;condition;incr/decr){  
                          //code to be executed  
                   }  



     
Example:
                       public class ForExample {  
                       public static void main(String[] args) {  
                            for(int i=1;i<=10;i++){  
                         System.out.println(i);  
                            }  
                         }  
                     }  

Output:1
                 2
                 3
                 4
                 5
                 6
                 7
                 8
                 9
                10

Java for-each loop

The for-each loop is used to traverse array or collection in java. It is easier to use than simple for loop because we don't need to increment value and use subscript notation.
It works on elements basis not index. It returns element one by one in the defined variable.

Syntax:
           for(Type var:array){  
                        //code to be executed  
                      }  

Example:
              public class ForEachExample {  
                     public static void main(String[] args) {  
                         int arr[]={12,23,44,56,78};  
                            for(int i:arr){  

                                   System.out.println(i);  
                            }  
                         }  
                      }  

Output:12
                 23
                 44
                 56
                 78

Java labeled for loop

We can have name of each for loop. To do so, we use label before the for loop. It is useful if we have nested for loop so that we can break/continue specific for loop.
Normally, break and continue keywords breaks/continues the inner most for loop only.


Syntax:
              labelname:  
                     for(initialization;condition;incr/decr){  
                                //code to be executed  
                          }  

Example:
                        public class LabeledForExample {  
                        public static void main(String[] args) {  
                        aa:  
                             for(int i=1;i<=3;i++){  
                          bb:  
                                  for(int j=1;j<=3;j++){  
                                    if(i==2&&j==2){  
                                    break aa;  
                           }  
                                 System.out.println(i+" "+j);  
                         }  
                    }  
               }  
          }  

Output:
           1 1
                1 2
                1 3
                2 1










No comments:

Post a Comment