Java do-while Loop
The do-while loop in Java is a control flow statement that allows you to execute a block of code at least once, even if the condition is false. This is the key difference between the while loop and the do-while loop. In a while loop, the condition is checked first; in a do-while loop, the body is executed first and the condition is checked afterward.
Because of this structure, do-while loops are ideal when you want the loop body to run at least once—for example, taking input repeatedly until it matches a requirement.
Syntax of do-while Loop
do {
// code to execute
} while (condition);
Important: Notice the semicolon ; after the while(condition) — this is mandatory and often forgotten by beginners.
How It Works
- The block inside do { } executes first.
- Then Java checks the condition in the while.
- If the condition is true, the loop repeats.
- If the condition is false, the loop stops.
This guarantees one execution of the loop body—no matter what.
Example 1: Basic do-while Loop
public class DoWhileExample1 {
public static void main(String[] args) {
int i = 1;
do {
System.out.println(“Number: ” + i);
i++;
} while (i <= 5);
}
}
Output:
Number: 1
Number: 2
Number: 3
Number: 4
Number: 5
Explanation:
The value of i starts at 1.
The loop executes and prints the value.
After each iteration, i increments.
It stops when i becomes 6 (i.e., the condition i <= 5 becomes false).
Example 2: do-while Always Runs at Least Once
public class DoWhileExample2 {
public static void main(String[] args) {
int num = 10;
do {
System.out.println(“This will print even if condition is false.”);
} while (num < 5); // false condition
}
}
Output:
This will print even if condition is false.
Explanation:
num < 5 is false at the start.
But the do block still runs once before the check.
Example 3: Taking User Input (Common Real-World Use Case)
import java.util.Scanner;
public class DoWhileExample3 {
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
int number;
do {
System.out.print(“Enter a positive number: “);
number = sc.nextInt();
} while (number <= 0);
System.out.println(“You entered: ” + number);
}
}
Explanation:
The program keeps asking for a positive number.
It stops only when the user enters a number greater than zero.
The prompt must appear at least once, making do-while perfect for this situation.
Example 4: Menu-Driven Program Using do-while
import java.util.Scanner;
public class MenuExample {
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
int choice;
do {
System.out.println(“\n— MENU —“);
System.out.println(“1. Say Hello”);
System.out.println(“2. Say Bye”);
System.out.println(“3. Exit”);
System.out.print(“Enter your choice: “);
choice = sc.nextInt();
switch (choice) {
case 1:
System.out.println(“Hello!”);
break;
case 2:
System.out.println(“Bye!”);
break;
case 3:
System.out.println(“Exiting…”);
break;
default:
System.out.println(“Invalid choice.”);
}
} while (choice != 3);
}
}
Explanation:
The menu appears repeatedly until the user enters option 3.
This is a very common use of do-while in console-based applications.
When Should You Use do-while?
Use a do-while loop when:
- You want the loop to execute at least once
- You’re building input validation loops
- You’re creating menu-driven programs
- The condition depends on actions done inside the loop
Use while or for loops when you want the condition to be tested before execution.
Key Points to Remember
The loop body always executes once, regardless of the condition.
The condition is evaluated after the loop body.
Semicolon after while(condition) is mandatory.
Useful for user input, menus, and retry mechanisms.
Less commonly used compared to for and while, but extremely useful in the right scenarios.
Comparison Table: while Loop vs do-while Loop in Java
| Feature | while Loop | do-while Loop |
|---|---|---|
| Condition Check | Condition is checked before executing the loop body | Condition is checked after executing the loop body |
| Minimum Executions | May not execute even once if the condition is false initially | Executes at least once, even if the condition is false |
| Use Case | Best when the number of iterations is unknown and you need to check the condition first | Best when the loop body must run at least once before checking the condition |
| Syntax Complexity | Simple syntax, no mandatory semicolon after condition | Requires a semicolon after while(condition); |
| Common Usage | Iterating until a condition becomes false, reading files, processing lists | Input validation, menu-driven programs, retry logic |
| Flow Control | Entry-controlled loop | Exit-controlled loop |
| Example Condition Behavior | If condition is false → loop doesn’t run | If condition is false → loop still runs once |