The do-while loop in Java is like the while loop except that the condition is checked after evaluation of the body of the loop. Thus, the do-while loop is an example of an exit-controlled loop
do-while
loop is a control flow statement that executes a block of code at least once, and then repeatedly executes the block as long as a specified condition is true. This loop is similar to the while
loop, but the condition is evaluated after the execution of the loop body, ensuring that the loop body is executed at least once, even if the condition is initially false.
Flow Diagram
The syntax of a do-while
loop in Java is as follows:
do {
// Body of the loop
// Code to execute repeatedly
// Iteration or update of loop control variables
} while (condition);
Java- Body of the loop: This is the block of code that is executed repeatedly until the condition evaluates to false. It contains the statements that you want to execute in each iteration.
- Condition: This part specifies the boolean expression that is evaluated after each iteration. If the condition evaluates to
true
, the loop continues; if it evaluates tofalse
, the loop terminates.
Here’s an example of a do-while
loop in Java:
import java.util.Scanner;
public class Main {
public static void main(String args[]) {
Scanner sc = new Scanner(System.in);
do {
// Take numbers as input
System.out.print("Enter First Number: ");
int first = sc.nextInt();
System.out.print("Enter Second Number: ");
int second = sc.nextInt();
// Print the choice list
System.out.println(
"Enter your choice n1 Addn2 Subtractn3 Multiplyn4 Divide"
);
// Perform the required operation
switch (sc.nextInt()) {
case 1:
first += second;
break;
case 2:
first -= second;
break;
case 3:
first *= second;
break;
case 4:
first /= second;
break;
}
// Print the output
System.out.println("Result is " + first + ".");
System.out.println("To continue enter 1, to exit enter 0");
} while (1 == sc.nextInt()); // test Expression
}
}
JavaOutput
Enter First Number: 10
Enter Second Number: 5
Enter your choice
1 Add
2 Subtract
3 Multiply
4 Divide
4
Result is 2.
To continue enter 1, to exit enter 0
1
Enter First Number: 4
Enter Second Number: 5
Enter your choice
1 Add
2 Subtract
3 Multiply
4 Divide
1
Result is 9.
To continue enter 1, to exit enter 0
0
JavaConclusion
The do-while
loop in programming languages like Java provides a powerful way to handle repetitive tasks with the guarantee that the loop’s body is executed at least once. This is particularly useful in scenarios where the initial condition is unknown or the operation needs to be performed before the condition can be evaluated. By evaluating the condition at the end of each iteration, do-while
loops ensure that the loop executes at least once, making them ideal for menus, input validation, and other situations where at least one execution is required regardless of the condition.
Frequently Asked Questions
Ans: The primary difference is that a do-while
loop checks its condition at the end of the loop body, ensuring the code within the loop executes at least once. In contrast, a while
loop checks its condition at the beginning, and the code may not execute at all if the condition is initially false.
Q2. Can the do-while loop execute more than once?
Ans: Yes, like other loops, the do-while
loop will continue executing its body as long as the condition evaluates to true. It is guaranteed to execute at least once but can execute many times depending on the condition.
Q3. Is it possible to exit a do-while loop in the middle of its execution?
Ans: Yes, you can exit a do-while
loop prematurely using a break
statement within the loop body. This will immediately terminate the loop, regardless of the condition.
Q4. How do you prevent an infinite loop in a do-while loop?
Ans: To prevent an infinite loop, ensure that the loop’s condition will eventually evaluate to false. This typically involves modifying a variable used in the condition within the loop body, leading to the condition being false at some point.
Q5. When should you use a do-while loop instead of a while loop?
Use a do-while
loop when you need to ensure that the loop’s body executes at least once, regardless of whether the condition is initially true or false. It is most appropriate when the initial condition depends on user input or an operation that occurs within the loop body.