do-while Loop
The do-while loop in Java is a control flow statement that executes a block of code at least once, and then repeats execution as long as a given condition remains true. This loop is best used when the code must run at least one time, regardless of the condition.
What Is a do-while Loop?
- Executes loop body before checking the condition
- Condition is evaluated after each iteration
- Loop executes at least once
Basic Syntax
do {
// code to execute
} while (condition);
⚠️ Semicolon is mandatory after while(condition);
Simple Example
int i = 1;
do {
System.out.println(i);
i++;
} while (i <= 5);
Output:
1
2
3
4
5
Guaranteed One-Time Execution Example
int x = 10;
do {
System.out.println("Executed once");
} while (x < 5);
Why it matters: Condition is false, but code executes once.
Execution Flow of do-while Loop
- Loop body executes
- Condition is checked
- If condition is true, loop repeats
- If condition is false, loop exits
do-while with break
do {
if (conditionMet) {
break;
}
} while (true);
do-while with continue
int i = 0;
do {
i++;
if (i == 3) {
continue;
}
System.out.println(i);
} while (i <= 5);
Nested do-while Loop
int i = 1;
do {
int j = 1;
do {
System.out.println(i + "," + j);
j++;
} while (j <= 2);
i++;
} while (i <= 3);
More do-while Loop Examples
1. Basic do-while Loop (Print 1 to 5)
int i = 1;
do {
System.out.println(i);
i++;
} while (i <= 5);
Explanation
- Loop executes at least once.
- Condition is checked after execution.
- Output: 1 2 3 4 5
2. do-while Loop Executes Even When Condition Is False
int i = 10;
do {
System.out.println(i);
} while (i < 5);
Explanation
- Condition is false initially.
- Still executes once.
- Output: 10
3. do-while Loop Printing Even Numbers
int i = 1;
do {
if (i % 2 == 0) {
System.out.println(i);
}
i++;
} while (i <= 10);
Explanation
- Prints only even numbers.
- Output: 2 4 6 8 10
4. do-while Loop Printing Odd Numbers
int i = 1;
do {
if (i % 2 != 0) {
System.out.println(i);
}
i++;
} while (i <= 10);
Explanation
- Prints only odd numbers.
- Output: 1 3 5 7 9
5. Reverse do-while Loop
int i = 5;
do {
System.out.println(i);
i--;
} while (i >= 1);
Explanation
- Loop runs backward.
- Output: 5 4 3 2 1
6. do-while Loop with break
int i = 1;
do {
if (i == 3) {
break;
}
System.out.println(i);
i++;
} while (i <= 5);
Explanation
- Loop terminates early when i == 3.
- Output: 1 2
7. do-while Loop with continue
int i = 0;
do {
i++;
if (i == 3) {
continue;
}
System.out.println(i);
} while (i <= 5);
Explanation
- Skips printing when i == 3.
- Output: 1 2 4 5
8. Infinite do-while Loop (Controlled with break)
do {
System.out.println("Running");
break;
} while (true);
Explanation
- Condition always true.
- break is required to stop execution.
9. do-while Loop for Array Traversal
int[] nums = {10, 20, 30};
int i = 0;
do {
System.out.println(nums[i]);
i++;
} while (i < nums.length);
Explanation
- Traverses array elements.
- Output: 10 20 30
10. do-while Loop to Skip Negative Values
int[] nums = {5, -1, 8, -3, 10};
int i = 0;
do {
if (nums[i] < 0) {
i++;
continue;
}
System.out.println(nums[i]);
i++;
} while (i < nums.length);
Explanation
- Skips negative numbers.
- Output: 5 8 10
11. do-while Loop for Sum Calculation
int i = 1;
int sum = 0;
do {
sum += i;
i++;
} while (i <= 5);
System.out.println(sum);
Explanation
- Accumulates values.
- Output: 15
12. do-while Loop for Factorial
int num = 5;
int fact = 1;
do {
fact *= num;
num--;
} while (num > 0);
System.out.println(fact);
Explanation
- Calculates factorial.
- Output: 120
13. do-while Loop for String Traversal
String s = "JAVA";
int i = 0;
do {
System.out.println(s.charAt(i));
i++;
} while (i < s.length());
Explanation
- Iterates character by character.
- Output:
J
A
V
A
14. do-while Loop for Digit Extraction
int num = 1234;
do {
System.out.println(num % 10);
num /= 10;
} while (num > 0);
Explanation
- Extracts digits from right to left.
- Output: 4 3 2 1
15. do-while Loop for Palindrome Check
int num = 121;
int temp = num;
int rev = 0;
do {
rev = rev * 10 + temp % 10;
temp /= 10;
} while (temp > 0);
System.out.println(num == rev);
Explanation
- Reverses number.
- Checks palindrome.
- Output: true
16. Nested do-while Loop
int i = 1;
do {
int j = 1;
do {
System.out.println("i=" + i + ", j=" + j);
j++;
} while (j <= 3);
i++;
} while (i <= 3);
Explanation
- Inner loop completes for each outer loop.
- Used in matrix/grid logic.
17. do-while Loop with Boolean Flag
int i = 1;
boolean found = false;
do {
if (i == 4) {
found = true;
break;
}
i++;
} while (i <= 5);
System.out.println(found);
Explanation
- break exits loop.
- Flag holds result after loop.
18. do-while Loop for Menu Simulation
int choice = 3;
do {
System.out.println("Menu shown");
} while (choice != 3);
Explanation
- Menu displays at least once.
- Common in menu-driven programs.
19. do-while Loop Skipping Empty Strings
String[] data = {"A", "", "B", "", "C"};
int i = 0;
do {
if (data[i].isEmpty()) {
i++;
continue;
}
System.out.println(data[i]);
i++;
} while (i < data.length);
Explanation
- Skips empty values.
- Output: A B C
20. Interview Summary Example (do-while Guarantee)
int i = 5;
do {
System.out.println("Executed once");
} while (i < 3);
Explanation
- Output: Executed once
- Demonstrates:
- Guaranteed execution
- Condition checked after body
- Very common interview concept.
Common Use Cases
- Menu-driven programs
- Input validation
- User interaction loops
- Retry mechanisms
do-while vs while
| Feature | do-while | while |
|---|---|---|
| Condition Check | After loop body | Before loop body |
| Minimum Execution | Once | Zero or more |
| Semicolon | Required | Not required |
Common Beginner Mistakes
- Forgetting semicolon after while()
- Infinite loops due to missing updates
- Confusing do-while with while
- Overusing nested loops
Interview-Ready Answers
Short Answer
The do-while loop executes the loop body at least once and then repeats as long as the condition is true.
Detailed Answer
In Java, the do-while loop checks the condition after executing the loop body, ensuring at least one execution. It is commonly used in menu-driven and user-interaction programs.
Key Takeaway
The do-while loop guarantees at least one execution, making it ideal for interactive and retry-based logic.