Keywords: do-while loop, conditional loop, loops
The final article in this chapter will look at the do-while loop in Java. A do-while loop is a conditional loop that is closely related to the while loop. We will see how we can use the do-while loop to first execute the loop’s code block, and then evaluate the condition. We will end the articles with a couple of examples that show the differences compared to the regular while loop and when the do-while loop may be favoured.
The do-while loop is, as the name suggests, closely related to the while loop and therefore also a condition loop. This means repeating a code sequence, over and over again, until a condition is true. The difference between a do-while loop and a while loop is that the do-while loop performs the code block with all the operations first, and then evaluates the loop’s condition. In other words, the do-while loop performs all operations at least once.
In short, a do-while loop is:
The difference between do-while loop and while loop is that do-while evaluates its expression at the bottom of the loop instead of the top. Therefore, the statements within the do block are always executed at least once
Oracle docs
As mentioned, the do-while loop works similarly to the while loop that we saw on in the previous article. If we illustrate the do-while loop with a flow chart, we get figure 1 below.
If we would describe what we see in Figure 1 with words
The do-while loop has many similarities with the while loop in functionality, but pay attention to the differences in the declaration.
If we use the instructions in the list above:
do { // do-while loop code block } while (condition);
Let’s look at a few examples of how to use a do-while loop in Java.
Let’s start with a simple example to demonstrate how to use a do-while loop to print the value of a variable
public class Example{ public static void main(String[] args) { int num = 0; do{ System.out.println("num has the value: " + num); num++; } while (num < 3); } }
Furthermore, the result in this case will be:
num has the value: 0 num has the value: 1 num has the value: 2
However, if we change the value of the variable num to
int num = 20;
and run our program again we would get the result
num has the value: 20
If we had used a while loop, the loop would never have started because the condition never becomes true, but since we use a do-while loop, the operation is always performed once before we evaluate if the condition of the loop is true.
If you would like to try the code in this example in an online compile, press the button below.
In this exercise we are going to:
By using a do-while loop, we will always get at least one result in the terminal, so in the case where the user’s specified number corresponds to the random number on the first try, we will also get a result.
// Import the Random class import java.util.Random; // Import the Scanner class import java.util.Scanner; public class exempel { public static void main(String[] args) { // Creates a scanner object Scanner input = new Scanner(System.in); // Creates a random object Random rand = new Random(); // Ask the user to enter information System.out.println("Enter an integer between 0-9"); // Save the input from the user int num = input.nextInt(); // Random integer between 0 - 9 int randomNum = rand.nextInt(10); // Do-while loop do{ // Generate a new random number randomNum = rand.nextInt(10); // Print information to the user System.out.println("User: " + num + " : " + "Random number: " + randomNum); // As long input is different from random number } while (num != randomNum); } }
Finally, in this case, it will be difficult to show the results because we get different answers every time. But a result might look like this:
User: 8 : Random number: 3 User: 8 : Random number: 6 User: 8 : Random number: 3 User: 8 : Random number: 0 User: 8 : Random number: 9 User: 8 : Random number: 8
Please leave feedback and help us continue to make our site better.
How useful was this article?
We are sorry that this post was not useful for you!
Let us improve this post!
Tell us how we can improve this post?
Similar to the other loops, to avoid having to rewrite our code several times, we can instead repeat an operation several times with the do-while loop. The do-while loop repeats a code block as long as the condition of the loop is true. We usually use the do-while loop when we don’t know in advance how many times it will be repeated, but always want to perform at least one operation.
The do-while loop in Java is a conditional loop that is closely related to the while loop. Similarly to the while loop, a do-while loop repeats a code sequence as long the condition of the loop is true. The difference between a do-while loop and a while loop is that a do-while loop performs the code block with all operations first, and then checks the condition.
do { // do-while loop code block } while (condition);
The difference between a do-while loop and a while loop is that a do-while loop performs the code block with all operations first, and then checks the condition. In other words, the do-wile loop performs the associated code block at least once.
If you know for sure that you will perform the operations inside the code block at least once, then the do-while loop may be preferable. But the most common of the two, in our opinion, is the while loop.
Yes. Just keep in mind that loops can “get stuck” in an infinity loop so that you pay attention so that your program can move on from the loops.
How useful was this article?
We are sorry that this post was not useful for you!
Let us improve this post!
Tell us how we can improve this post?