Loops In Java

In this page, we will learn about What is Loops in Java?, Loops in Java, Differece between for loop and while loop in Java?, Differece between for loop and do while loop in Java.

What is Loops in Java?

Not only in java, but also in other programming languages, the concept of loop is same. Loops are used to execute a set of instructions that needs to execute n number of times. In Java, there are there types of loops and these are:

  • For Loop: For loop in java is used to itarate a portion of the program in a number of times. If the of the number of iterations is fixed, then is good to go with for loop. For loop systax in java is given below:

    
      for(varible init; condition; incr/decr){  
        // code to be executed 
      }
    
    
  • While Loop: While loop in java is used to iterate a portion of the program in number of times and If the of the number of iterations is not fixed or unknown, then is good to go with

    
      while(boolean condition){  
        // code to be executed 
        incr/decr;
      }
    
    
  • Do While Loop: Do While loop in java is used to iterate a part of the program several times and If you need to iterate atleast one time, then is good to go with Do While loop.

    
      do{  
          //code to be executed
          incr/decr;  
        }while(condition);
    
    
loops In java

Differece between for loop and while loop in java and different between for loop and do while loop :-

Key For While Do While
Intro The Java for loop may be a management flow statement that iterates a locality of the programs multiple times. The Java whereas loop could be a management flow statement that executes a locality of the programs repeatedly on the idea of given Boolean condition. The Java do whereas loop may be a management flow statement that executes an area of the programs a minimum of once and therefore the any execution depends upon the given mathematician condition.
When to use If the number of iteration is fixed, it is recommended to use for loop. If the quantity of iteration isn't mounted, it's suggested to use whereas loop. If the amount of iteration isn't mounted and you want to have to be compelled to execute the loop a minimum of once, it's counseled to use the do-while loop.
Syntax
for(init;condition;incr/decr){  
 // code to be executed 
}
while(condition){  
    // code to be executed
    incr/decr  
}
do{  
  //code to be executed  
  incr/decr 
}while(condition);