How does the Java do-while loop work?

The Java do-while loop ensures that a program executes instructions until a defined termination condition is reached. You can also combine the loop with other functions.

What is a do-while loop?

The do-while loop is used in Java to execute and repeat an instruction until a defined termination condition is fulfilled (i.e., true). Similar loops exist in most programming languages and are used to execute certain blocks of code multiple times. The loop can also be combined with other functions. In Java, the do-while loop is a variant of the while loop.

What is syntax for Java’s do-while loop?

The syntax of the do-while loop in Java looks like this:

do {
	//statements
}
while(condition);
java

The loop starts with the keyword do, followed by the instructions or command that the program should carry out. After the closing parenthesis, the keyword while appears, which triggers the loop’s termination condition. This condition is always expressed as a Boolean expression, meaning, within the program’s logic, it evaluates to either true or false. A common and useful example of such an expression is a counter variable, which allows you to determine the number of times the loop should repeat.

Web Hosting
Fast, scalable hosting for any website
  • 99.9% uptime
  • PHP 8.3 with JIT compiler
  • SSL, DDoS protection, and backups

How the loop works

When the Java do-while loop is executed, the program looks at the instructions within the loop body and executes them. Once this has been completed, it checks the termination condition. If this is true (i.e., not yet fulfilled), the loop starts from the beginning and repeats all statements until it is checked again. However, as soon as the termination condition is met and therefore false, the program terminates the loop and continues with the rest of the code.

Example of a simple loop

To illustrate how the loop works, we’ll show you its structure using a simple example. In the following example, we’re going to assign the variable x the value 10. With the help of the Java operator ++, this value will be increased by 1 in each pass and displayed with the Java command System.out.println. Our termination condition is the value 20, so the loop is repeated as long as x is less than 20. The code looks like this:

public class Main {
	public static void main(String[] args) {
		int x = 10;
		do {
			System.out.println(x);
			x++;
		}
		while (x < 20);
	}
}
java

If we execute this code, we get the following output:

10
11
12
13
14
15
16
17
18
19
java

The program then executes the loop again but recognizes the value of x as being 20. Since it is no longer less than 20, the termination condition is fulfilled and the do-while loop is ended.

How to create an infinite loop with the value true

With the help of the Boolean expression true, it’s possible to design an endless loop with do-while. This will never stop itself since no termination condition can be reached. To end the loop, you must manually use the combination [Ctrl] + [C]. The corresponding code looks like this:

public class Main {
	public static void main(String[] args) {
		do {
			System.out.println("You are in an endless loop.");
		}
		while(true);
	}
}
java

If you execute this code, you’ll receive the following output:

You are in an endless loop.
You are in an endless loop.
You are in an endless loop.
You are in an endless loop.
You are in an endless loop.
ctrl+c
java

If you do not end the loop manually, it will continue to run.

The difference between do-while and while

In many ways, the Java do-while loop and the while loop are alike. However, they differ in one crucial aspect, which is that the while loop evaluates its termination condition right at the start, whereas the do-while loop always executes at least once in full before it may terminate. If you need to ensure the commands are executed at least once, you should use the do-while loop. If the decision to terminate is critical, consider using the head-controlled while loop instead.

Was this article helpful?
We use cookies on our website to provide you with the best possible user experience. By continuing to use our website or services, you agree to their use. More Information.
Page top