do-while Loop
Post-test loop
4 min read
The do-while Loop
The do-while loop is a post-test loop that guarantees the loop body executes at least once before checking the condition.
Syntax
do {
// loop body
// executes at least once
} while (condition);
🔑 Key Difference: while checks condition first (may never execute), do-while executes first then checks (always runs at least once).
When to Use do-while
- Menu-driven programs (show menu at least once)
- Input validation (ask for input, then validate)
- Game loops (play at least one round)
- When action must happen before condition check
⚠️ Don't Forget: The semicolon after while(condition); is required! Missing it causes a compilation error.
Code Examples
Basic do-while and guaranteed execution
java
1// Basic do-while
2int count = 1;
3do {
4 System.out.println("Count: " + count);
5 count++;
6} while (count <= 5);
7// Output: Count: 1, 2, 3, 4, 5
8
9// Executes at least once even if condition is false
10int x = 10;
11do {
12 System.out.println("This prints once!");
13 x++;
14} while (x < 5); // Condition already false!Classic menu-driven program pattern
java
1// Menu-driven program
2Scanner scanner = new Scanner(System.in);
3int choice;
4
5do {
6 System.out.println("\n=== Menu ===");
7 System.out.println("1. Add");
8 System.out.println("2. Subtract");
9 System.out.println("3. Exit");
10 System.out.print("Enter choice: ");
11 choice = scanner.nextInt();
12
13 switch (choice) {
14 case 1 -> System.out.println("Adding...");
15 case 2 -> System.out.println("Subtracting...");
16 case 3 -> System.out.println("Goodbye!");
17 default -> System.out.println("Invalid choice!");
18 }
19} while (choice != 3);Input validation - ask first, validate after
java
1// Input validation with do-while
2Scanner scanner = new Scanner(System.in);
3int age;
4
5do {
6 System.out.print("Enter your age (1-120): ");
7 age = scanner.nextInt();
8
9 if (age < 1 || age > 120) {
10 System.out.println("Invalid age! Try again.");
11 }
12} while (age < 1 || age > 120);
13
14System.out.println("Your age is: " + age);while vs do-while comparison
java
1// Comparison: while vs do-while
2int n = 0;
3
4// while - never executes
5while (n > 0) {
6 System.out.println("while: " + n);
7 n--;
8}
9
10// do-while - executes once
11do {
12 System.out.println("do-while: " + n); // Prints "do-while: 0"
13 n--;
14} while (n > 0);
15
16// Sum digits of a number
17int number = 12345;
18int sum = 0;
19do {
20 sum += number % 10;
21 number /= 10;
22} while (number > 0);
23System.out.println("Sum of digits: " + sum); // 15Use Cases
- Menu-driven applications
- Password/input retry mechanisms
- Game "play again" loops
- Validating user input
- Processing that must occur at least once
- Reading file records
Common Mistakes to Avoid
- Forgetting semicolon after while(condition)
- Using do-while when while would be cleaner
- Not updating condition variable in loop body
- Confusing with while loop behavior
- Unnecessary use when action is not guaranteed needed
- Creating infinite loops