Java Iteration (Loops)

Java Iteration or Loops

Whenever we want to do some task repeatedly, and we don't want to write same code again and again then we use loops. Java also don't prefer rewriting code. There are some type of loops which can help to do some iteration task in java.

  1. while loop
  2. do while loop
  3. for loop
  4. for-each or enhanced for loop
  5. recursion


1) while loop is run for the specified condition matches. This is also called entry control loop just because it checks the condition on the entry point, then proceed to execute the body part.

Syntax:
while(condition){
body //This is piece of code you want to repeat for specified time.
}
NOTE: We can make it infinite using "while(true)".
Program:
public class Test {
    public static void main(String[] args) {
        int i = 0;
        while (i < 10) {
            System.out.println("while loop iteration no:" + i);
            i++;
        }
    }
}
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
2) do while loop is  also called exit control loop because it execute the body first and then check for the condition true or false, that means it will execute at least 1 time no matter condition false or true.

Syntax: 
do {
body //This is piece of code you want to repeat for specified time.
} while(condition);
Note: We can also make it infinite using "while(true)".
Program:
public class Test {
public static void main(String[] args) {
int i=0;
do {
System.out.println("while loop iteration no:"+i);
i++;
}while(i<10);
}

}

~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
3) for loop is most popular loop it very easy just because it is inline loop. It is also entry control loop.

Syntax: 
for(initialization ; condition ; increment/decrement){
body //This is piece of code you want to repeat for specified time.
}
Note: We can also make it infinite using "for(;;)".
Program: 
public class ForLoopExample {
public static void main(String[] args) {
for(int i=0 ; i<10; i++){
System.out.println("while loop iteration no:"+i);
}
}
}
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
4) for-each loop is enhanced version of for loop.

Syntax: 
for(data_type item : collection){
body // We can perform any operation on the collection iteration.
}
Program: 

class ForEachLoopExample {
   public static void main(String[] args) {
      
      char[] vowels = {'s', 'u', 'r', 'a', 'j'};
      // foreach loop
      for (char item: vowels) {
         System.out.println(item);
      }
   }
}
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
5) recursion is kind of loop it is call itself for the specified condition matches.
Syntax:
return_type methodName(){
//your code goes here this is body of the recursion loop.
methodName();
}
Program:
public class RecursionExample1 {  
static void myMethod(){  
System.out.println("Suraj");  
myMethod();  
}  
  
public static void main(String[] args) {  
myMethod();  
}  
}  

Comments

Popular Posts