Monday, 8 February 2016

Loops in JAVA

Looping statements are using for iterating the single or set of statements. In JAVA we have below looping statements

v  for
v  while
v  do-while
v  for-each

for loop:
Syntax:
               
For (initialization; condition; increment/decrement) {
                                                 Statements;
                }
Important Points:
1.       Curly braces are not mandatory if it is a single statement
2.       Initialization statements will be more than one
3.       Initial statement will executes only once
4.       Condition should be Boolean return type

Example:

        class ForLoopDemo
    {
  public static void main(String[] args)
  {
     for(int i=0;i<10;i++){
              System.out.println("Raju");
              System.out.println("Raju1");
              }     
  }
}

While loop: If we don’t know how many number of times statements should execute we will go for while loop. This loop is known as entry control loop as condition will check before start executing statements

    Syntax:

                         While (condition) {
                               Statements;
                         }
   Important Points:
1.       Curly braces are not mandatory if it is a single statement
2.       Initialization statement should be outside while loop
3.       Initial statement will executes only once
4.       Increment or decrement statement should be inside the loop
5.       Condition should be Boolean return type 
Example:

             class WhileLoopDemo{
              public static void main(String[] args) {
                     int i=1;
                     while(i<10){
                            System.out.println("Raju");
                            i++;
                     }     
              }
}

Do-while loop: If the loop statement should be execute at least once we will go for do while loop. We can call this loop is exist control loop

     Syntax:
          
         do {
               Statements;
         }while (condition); 

  Important Points:
1.       Curly braces are not mandatory if it is a single statement
2.       Initialization statement should be outside while loop
3.       Initial statement will executes only once
4.       Increment or decrement statement should be inside the loop
5.       Condition should be Boolean return type
6.       After while condition ; symbol is mandatory


Example:

     class DoWhileLoopDemo{
  public static void main(String[] args)  {
     int i=1;
        do{
              System.out.println("Raju");
              i++;
       }while(i<10);
   }
}

for-each loop: If we wants to iterate array of objects we will go for for-each loop. We can use it for iterating lists

   Syntax:
 
       for(data_type variable : array | collection){
     } 
Important Points:
1.       Curly braces are not mandatory if it is a single statement
2.       Initialization statement not required
3.       Increment or decrement statement not required
4.       Condition not required
                                                          
Example:

   class ForEachExample1{ 
  public static void main(String args[]){ 
   int arr[]={12,13,14,44}; 
 
   for(int i:arr){ 
     System.out.println(i); 
   } 
 
 }  

}

No comments:

Post a Comment