How to Use the do while Loop in Java?
Learn via video course
How to Use the Do While Loop in Java?
The one thing that comes to mind when you hear about a loop is iteration. To iterate through a linear data structure, we often use “for loop”, “while loop”, or “do while” loop. But loops are not only used to iterate linear data structures; they are also used to execute a block of programme repeatedly.
Do While loop in Java
The other name for the Do While loop in java is called as an exit-control loop (condition is evaluated at the end of each iteration). Do While loop in Java is the same as the While loop, but the main difference is that Do While will run at least once. This happens because the condition is checked after the end of the loop body. Following is the syntax for the Do While loop in Java.
Syntax
Example
Output
Flow Chart of Do While Loop in Java
Following is the flow chart of a do while loop in java.
The code inside the do while loop will be executed in the first step. Then after updating the loop variable, we will check the necessary condition; if the condition satisfies, the code inside the do while loop will be executed again. This will continue until the provided condition is not true.
Application of Do While Loop in Java
Do while loop in Java has many applications that we can see in our daily coding practices. For example; if you want to traverse through an array (array of size greater than 1), you use the do while loop. Also, in various graph algorithms like bfs (breadth first search), you can use the do while loop.
For example, we can traverse a List in the following manner.
Code
Output
Note: In the above code, if we had zero elements in the list then it would have thrown an error. In this case, we must prefer a for loop.
Java Infinitive do-while Loop
Infinitive do while loop means never-ending code. If you fail to write the proper conditional statement, then your code might get stuck in an Infinite loop. Let us see an example that will help you understand the infinitive do while loop.
Code
Explanation There will be no output for the above code also, the code will never end. As we are initializing the value of i to 0 and incrementing the value of i by 1 every time. In the while condition we are saying that if the value of i is less than -1 break the do while loop. So the value of i can never be -1 hence the loop will never end and this is called as infinitive do while loop.
What are the Components of Do while Loop in Java?
There are two components of do while loop in java. The components are listed below:
- Text Expression
- Update Expression
Text Expression
The code we write inside the simple bracket of the while statement (while(.. Text expression ..)) is called text expression. For example, in the code below the underlined part is text expression.
Update Expression
This expression increases or decreases the loop variable by a specified amount after the loop body has been executed. In other words, the update expression is simply updating a loop variable. For example, the underlined part inside the “do” block is update expression component.
Execution of Do While Loop in Java
Let us discuss the do while loop in detail. Breaking down the do while loop in 6 steps they are as follows:
- In the first step we will enter the do while loop.
- In the second step all the main code inside the do while loop will be executed.
- Updation of variables will take place in third place.
- Then a check for a conditional statement will be done inside the while block at fourth step.
- If the result of the conditional statement is true then it will preform fifth step, i.e., again entering the do while loop.
- If the result of the conditional statement is false then the do while loop will be terminated.
Examples of Do While Loop in Java
Example 1
In this example we will be writing a table of any integer n.
Code
Output
Explanation In the above Java program we defined a class called DoWhile with a method called printTable. The printTable method takes an integer as input and uses a "do-while" loop to print the multiplication table for up to .
Inside the do-while loop, the program prints the table of "n" multiplied by the loop variable . Then, it updates the value of "i" by incrementing it by . The loop continues executing as long as the value of is less than or equal to .
In the main method, an object of the DoWhile class is created and the printTable method is called on this object with the argument of . This will print the multiplication table for up to .
Example 2
In this example, we will be implementing a program that takes continuous integer input from user until user enters as input.
Code
Output
Explanation In the above example we have entered several values as a input. But as soon as we input as a input the do while loop breaks. This is because of the conditional statement, the do while loop executes until the user doesn’t enters as a input integer.
Do While vs While
do while | while |
---|---|
The loop will execute at least once. | The loop does not guarantee to execute at least once. |
The conditional statement comes at the end of the do while block. | The conditional statement comes at the top of the while block. |
The general form of do while loop is: do{ code } while( condition ); | The general form of while loop is: while( condition ) { code } |
Learn More
Still want to get more knowledge about loops in Java? You can visit Scaler Topics for more information. Check out this article on loops in Java for detailed information of all types of loops in Java. Also you can visit:
Conclusion
- It is guaranteed that do while loop will always execute at least once.
- The conditional statement is given at the end of the do while block.
- We can also called do while loop as post tested loop. As condition is checked after updating loop variables.