Do While Loop in Java: In Java, a Do While Loop is a loop that executes a block of code at least once and then continues to repeat the code as long as a specified condition remains true. The do-while loop is often used when you want to execute a block of code at least once, regardless of whether the condition is true or false.
It is similar to a while loop in that it checks the condition at the end of each iteration but differs in executing the code block before the condition is checked. This means the code block is guaranteed to be executed at least once, even if the condition is false. In this way, the do-while loop is useful when running a piece of code at least once before deciding whether to repeat it. In this article, we will explore the syntax and usage of do-while loops in Java, along with some examples to help illustrate their functionality.
Do While Loop Syntax In Java
In Java, the syntax for a do-while loop is as follows:
do { // code block to be executed } while (condition);
The code block within the curly braces will be executed at least once, and then the condition in parentheses will be evaluated. If the condition is true, the code block will be executed again, and the process will continue until the condition is false.
Note: The condition for the do-while loop must return a boolean value that is true or false; otherwise, you will get a compile-time error.
Do While Loop Example
Here, we first discuss some of the use cases of the Do while loop in Java.
Do While Loop Use Case 1
One common use case for a do-while loop is when you want to prompt a user for input and continue prompting until they enter valid input. For example, consider the following code:
Program: Write a Program to Accept Only Positive Numbers
package com.softwaretestingo.ConditionalStatements; import java.util.Scanner; public class BasicDoWhileLoopEx { public static void main(String[] args) { Scanner input = new Scanner(System.in); int number; do { System.out.print("Enter a positive number: "); number = input.nextInt(); } while (number <= 0); System.out.println("You entered: " + number); } }
Output:
Enter a positive number: -4 Enter a positive number: -5 Enter a positive number: 2 You entered: 2
The program prompts the user to enter a positive number in this example. If the user enters a number less than or equal to 0, the loop will repeat and prompt the user again. This will continue until the user enters a positive number, and at that point, the loop will exit, and the program will print out the number they entered.
Use Case 2
Another use case for a do-while loop is when you want to perform an action at least once and then continue performing that action until a certain condition is met. For example, consider the following code:
Program: Write a Java Program to Print 1 to 10
package com.softwaretestingo.conditionalstatements; public class DoWhileLoop_Example { public static void main(String[] args) { int i = 1; do { System.out.println("Counting: " + i); i++; } while (i <= 10); } }
Output:
Counting: 1 Counting: 2 Counting: 3 Counting: 4 Counting: 5 Counting: 6 Counting: 7 Counting: 8 Counting: 9 Counting: 10
In this example, the program counts from 1 to 10, printing out each number as it goes. The loop will always execute at least once because the initial value i is 1. After each iteration, the i value is incremented, and the loop will continue as long as i is less than or equal to 10.
Infinite do-while loop
One important thing to remember when using a do-while loop is to ensure the condition in the while statement can become false. If the condition is always true, the loop will become infinite, and the program will hang. For example, consider the following code:
package com.softwaretestingo.conditionalstatements; public class DoWhileInfiniteLoop_Example { public static void main(String[] args) { int i = 1; do { System.out.println("Counting: " + i); i++; } while (i > 0); } }
In this example, the program starts counting from 1 and then increments i after each iteration. Since i will always be greater than 0 after the first iteration, the condition in the while statement will never fail, and the loop will become an infinite loop.
Difference Between Do While Vs. While Loop
Here’s a comparison table outlining the key differences between the Do-While and While loops in Java:
Do-While Loop | While Loop |
---|---|
The code block is executed at least once before the condition is checked. | The condition is checked before the code block is executed, which means that the code block may not be executed at all if the condition is false from the beginning. |
The syntax is: do { code block } while (condition); | The syntax is: while (condition) { code block } |
The loop continues as long as the condition is true. | The loop continues as long as the condition is true. |
The condition is checked at the end of each iteration. | The condition is checked at the beginning of each iteration. |
The do-while loop is useful when executing a code block at least once, even if the condition is false. | The do-while loop is useful when executing a code block at least once, even if the condition is false. |
The do-while loop is less commonly used than the while loop. | The do-while loop is useful when executing a code block at least once, even if the condition is false. |
I hope this comparison helps clarify the differences between these two loop types in Java.
Conclusion:
In summary, the do-while loop is a useful tool in Java where you must execute a block of code at least once and then repeat it as long as a certain condition is true. By placing the code block before the condition, the do-while loop ensures that the code block will always be executed at least once, even if the condition is false. However, it is important to be careful when using do-while loops and ensure the condition can become false to avoid infinite loops.
Ref: Article