Java Do While Loop – Tutorial With Examples

This tutorial will explain how to use the Do While Loop in Java with examples:

In this tutorial, we will discuss the third iteration statement of Java (after for loop and while loop) i.e. the “do-while loop”.

We will explore this iteration statement along with the syntax and programming examples. We will take a look at an example to illustrate how we can achieve the same iteration using for loop, while, and a do while loop respectively.

We are also going to illustrate a comparison between a while loop and a do while loop through the programming.

=> Explore The Simple Java Training Series Here

Java do-while loop

Do While Loop In Java

Given below are the variations that we are going to cover in this tutorial.

  • do while loop
  • Infinitive do while loop

Sufficient programs and briefings will be provided in this tutorial with some frequently-asked questions on this topic.

Apart from a while loop and a for loop, we have one more iteration statement which is a do while loop. This iteration statement is just like a while loop except for the fact that a do while loop is guaranteed to execute at least once.

Syntax:

do { 

// instructions or body of the loop to be executed
} while(condition)

Flowchart:

Flowchart - Java Do While Loop

Printing The First 10 Natural Numbers

Given below is the program where we have printed the first 10 natural numbers using a do-while loop. Here, we have initialized the count variable with 1 as the natural numbers start from 1.

Then, we have started a do while loop in which we have specified a do where the counter will be incremented by 1 after printing the value of ‘i’ and then a while condition i <=10, so it will keep on printing ‘i’ until the value reaches 11.

public class example {

	public static void main(String[] args) {
		
		// natural number starts from 1
		int i=1;
		/*
		 * do-while loop starts here in which we have specified
		 * a do where the counter will be incremented by 1 after 
		 * printing the value of 'i' and then a while condition i<=10, 
		 * so it will keep on printing i until
		 * the value reaches 11.
		 */
		
		do{
			System.out.println(i);
			i++;
			
		}while (i<=10);
	}
}

Output:

Printing the first 10 natural numbers - Output

Java For Loop Vs While Loop Vs Do While Loop

In Java, the for loop, while loop and do while loops are the most common statements that are used for iteration. In this section, we will see how we can accomplish the same result through all these iteration statements.

Given below is the program where we have initialized an ArrayList with three elements and then tried to iterate the ArrayList using a for loop, while loop, and a do-while loop respectively.

import java.util.*;

public class example {
	public static void main(String[] args) {
		ArrayList list = new ArrayList();
		list.add("Saket's");
		list.add("Loop");
		list.add("example");
		
		// getting the size of the arraylist
		System.out.println(list.size());
		
		/*
		 * In while loop, iterating through the arraylist
		 * using the inbuilt methods hasNext() and next().
		 * The hasNext() method will check if there is
		 * an element exists and next() method will
		 * print that element.
		 */
		
		System.out.println("while loop starts here:");
		Iterator itr = list.iterator();
		while(itr.hasNext()) {
			System.out.println(itr.next());
		}
		
		/*
		 * In do-while loop, iterating through the arraylist
		 * using a do with the inbuilt methods hasNext() 
		 * and next(). A while loop with the condition
		 * will appear at the end of do.
		 * The hasNext() method will check if there is
		 * an element exists and next() method will
		 * print that element.
		 */
		
		System.out.println();
		System.out.println("do-while loop starts here:");
		Iterator itr1 = list.iterator();
		do {
			System.out.println(itr1.next());
		}while(itr1.hasNext());
		
		/*
		 * In for loop, iterating with a count variable initialized
		 * with 0 and it will be less than the size of the arraylist
		 * which is 3. The count variable will be incremented by 1
		 * after each iteration.
		 */
		
		System.out.println();
		System.out.println("for Loop starts here:");
		for(int i=0; i<list.size(); i++) {
			System.out.println(list.get(i));
		}
	}
}

Output:

Java for loop vs while loop vs do-while loop

Infinite Do While Loop

Just like a while loop, we have an infinitive do while loop which will go into an infinite loop if the specified condition is met even after the iteration.

Given below is an example of an infinite do while loop.

Note: Just like the example of infinitive while loop, here also we have externally halted the execution of do while loop capturing the output of the below program after a few seconds of its execution.

public class example {

	public static void main(String[] args) {
		int i=5;
		do{
			System.out.println("infinite do-while loop");
			
		}while (i==5);
	}
}

Output:

Infinitive do-while loop

Frequently Asked Questions

Q #1) Do while Vs While Loop Java.

Answer: Given below is the comparison between these two iteration statements.

While LoopDo-while Loop
It is an iteration statement with the general form

while(condition){
// body of the loop
}
It is also an iteration statement with the general form

do{
// body of the loop
} while(condition);
This loop is not guaranteed to execute at least once.This loop is guaranteed to execute at least once.
The while condition comes in the beginning of the loop and is based on the condition, the loop behaves.The while condition comes at the end and is based on the condition, this loop behaves.
All the executing statements come under the while loop.All the executing statements come under a do-while loop.

Q #2) How do you write a do-while loop?

Answer: Given below is the sample program of a do-while loop. Here, we have initialized a variable and inside a do, we have been printing the value of ‘i’ for the condition i < 10.

After every iteration, the value of ‘i’ will be incremented by 1. So, ‘i’ will be printed 5 times.

public class example {

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

Output:

Do While Loop

Q #3) What are for loop, while loop, and do-while loop?

Answer: These are all iteration statements that are used to execute a particular set of instructions repeatedly based on a certain condition. As long as the condition is met, the loop will keep on iterating.

Also, you can make these loops go into an infinite loop by specifying a condition that is going to be met forever. For more details on how these loops work, refer to the section “Java for loop vs while loop vs do-while loop”.

Q #4) How does a do-while loop work in Java?

Answer: First of all, the code or the executable statements inside a do executes, and then the while condition is checked. If the while condition is met, then the loop goes into the iteration, otherwise, the control comes out of the do while loop.

Q #5) Which loop is guaranteed to execute at least once?

Answer: A do while loop.

Conclusion

In this tutorial, we have explained the Java do while loop which is the last but not the least form of iteration statement that Java supports. We saw a proper description, syntax, flowchart, and programming examples that will be sufficient for you to understand the loop.

Further reading =>> How to use Java Continue statement

Some frequently asked questions related to the topic were also discussed here. We covered an infinite do while loop that is the other variation of the loop. A detailed comparison between a for loop, while loop, and a do while loop was also discussed along with the concerned program.

=> Visit Here To Learn Java From Scratch