Java Do/While Loop - Complete Guide
Introduction to Do/While Loop
The do/while loop in Java is a type of loop that ensures the code block runs at least once before checking the condition. This is useful in cases like user input, menu systems, or processing that must occur at least once.
The key distinction is evaluation timing: a while loop checks the condition first, but a do/while loop checks after executing the body.
Basic Do/While Loop Syntax
A do/while loop begins with the do keyword, followed by a block of code. After execution, the while keyword and a condition in parentheses follow, ending with a semicolon.
public class DoWhileLoop {
public static void main(String[] args) {
int count = 1;
do {
System.out.println("Count: " + count);
count++;
} while (count <= 5);
int number = 10;
do {
System.out.println("This runs once even though condition is false: " + number);
number++;
} while (number < 5);
}
}
Count: 1 Count: 2 Count: 3 Count: 4 Count: 5 This runs once even though condition is false: 10
Comparing While and Do/While Loops
While loops may not execute at all if the condition is false initially, but do/while loops always run once before checking.
This makes do/while a better choice when at least one execution is required, such as menu prompts or repeated input validation.
public class WhileVsDoWhile {
public static void main(String[] args) {
int whileCounter = 10;
int doWhileCounter = 10;
System.out.println("While loop:");
while (whileCounter < 5) {
System.out.println("While iteration: " + whileCounter);
whileCounter++;
}
System.out.println("\nDo/While loop:");
do {
System.out.println("Do/While iteration: " + doWhileCounter);
doWhileCounter++;
} while (doWhileCounter < 5);
}
}
While loop: Do/While loop: Do/While iteration: 10
Practical Examples of Do/While
Do/while loops are commonly used in input validation, authentication, and menu-driven programs because the user must be prompted at least once.
public class DoWhileExamples {
public static void main(String[] args) {
// Menu simulation
int choice;
boolean validChoice = false;
do {
System.out.println("\nMenu:");
System.out.println("1. Option 1");
System.out.println("2. Option 2");
System.out.println("3. Exit");
choice = 1; // simulated input
System.out.println("You selected: " + choice);
validChoice = (choice >= 1 && choice <= 3);
} while (!validChoice);
// Password simulation
String password;
boolean success = false;
int attempts = 0;
do {
attempts++;
password = (attempts < 3) ? "wrong" : "secret";
System.out.println("Attempt " + attempts + ": " + password);
if (password.equals("secret")) {
success = true;
System.out.println("Access granted!");
} else if (attempts >= 3) {
System.out.println("Too many failed attempts.");
}
} while (!success && attempts < 3);
}
}
Menu: 1. Option 1 2. Option 2 3. Exit You selected: 1 Attempt 1: wrong Attempt 2: wrong Attempt 3: secret Access granted!
Best Practices for Do/While Loops
- ✅ Use do/while when at least one execution is required
- ✅ Suitable for menus, prompts, and retries
- ✅ Don’t forget the semicolon after while
- ✅ Keep conditions simple for readability
- ✅ Prefer while if zero executions are acceptable
- ✅ Break early when necessary
- ✅ Test edge cases to avoid infinite loops
- ✅ Use descriptive variable names to clarify loop logic