Different types of Loops in Java: For, For Each, While, Do-While loop

In our previous tutorial, we discussed various control statements like If-Else & Switch Statement in Java. Today we are going to discuss control flow statements like different types of loops in Java for example For, For Each, While and Do-While loop. Let me remind you Control statements & Control flow statements are the basic building block of the programming language and your testing logic. These statements make your programming life easier.

Let’s understand them one by one.

Java For Loop

Java for loop is the basic iteration technique that is used when the number of iteration is in a fixed quantity. It simply means we use for loop when we know the total length of iteration.

Components of For loop

You need to furnish the following information to write a for loop. Hence, the following items would be considered as components of for loop:

  • Initialization: This is the entry point for starting the iteration. We set an integer variable with an initial value, let’s say int i=0 or int i=10.
  • Condition: After initialization, we set a condition in favor to initial value. Loop will iterate until this condition becomes false. Example: Iterate over the total length of the string, i<str.length().
  • Increment/Decrement: If above condition is true, then it increases or decreases the initial value based on your requirement. Example: i++ or i- – or ++i or – -i.

Types of For loop

We are going to discuss three types of for loops. They are used in different phases of your test programming journey.

Let’s discuss them.

Simple For Loop

for(initialization; condition; increment/decrement;){

}

Sample Program

public class Loops {
	
	public static void main(String[] args){
		
		for(int i=5; i>0; i--){
			System.out.println(i);
		}
		
	}

}

Nested For Loop

for(initialization; condition; increment/decrement;){

for(initialization; condition; increment/decrement;){

}

}

Sample Program

public static void main(String[] args){
for(int i=0; i<5; i++){
  for(int j=5; j>i; j--){
   System.out.print("*");
  }
    System.out.println("");
    }	
}

For Each Loop

For each loop is beneficial when you want to iterate over an array or collections. It uses either data type directly or wrapper. Here we do not need to bother about initialization, condition and increment or decrement; it returns an object of Collection or Wrapper or Array on each iteration.

for(WrapperType type : Array){

}

Sample Program

public static void main(String[] args){
int[] num = new int[]{1,2,3,4,5};
 for(int n : num){
	System.out.println(n);
   }	
}

Java Labeled For Loop

When we are using nested for loop and we want to break the outer loop from any of the inner loops then we do so by its label.

label:

for(initialization; condition; increment/decrement;){

}

Sample Program

public static void main(String[] args){
	       a1:
		for(int i=0; i<20; i++){
			b1:
			for(int j=0; j<10; j++){
				if(i==3 && j==2){
					break a1;
				}
				System.out.println(i+" - "+j);
			}
		}	
	}
}

For while dowhile loops in java

Java While Loop

When you do not have clear information about the total number of iteration, then you can identify certain conditions and can use a while loop.

while(conditions){
}

Sample Program

public static void main(String[] args){
	int i=0;
	while(i<5){
		System.out.println(i);
		i++;
	}
}

Java Do While Loop

Java do-while loop is just the extended version of the while loop which is discussed above. When you are even not clear about the condition but want to iterate the loop at least once to further set the condition then do-while loop is the right choice for a programmer.

do{
} while(conditions)

Sample Program

public static void main(String[] args){
	int i=0;
	do{
		System.out.println(i);
		i++;
	}while(i<5);
}

These were all about different types of loops in Java. Feel free to contact me if you face any difficulties. Stay safe and take care.

Join Inviul fb group

Leave a Reply