⏱️ 7 min read • Beginner Level • Lesson 27
Completed on . You can revise this lesson or continue to the next topic.
The do-while loop in Java is used to execute a block of code repeatedly
as long as a given condition is true. Unlike the while loop,
the do-while loop checks the condition after executing the loop body.
This means a do-while loop executes the loop body
at least once, even if the condition is false at the beginning.
Before learning this topic, you should understand
Java while loop,
Java operators, and
Java variables.
A do-while loop is an exit-controlled loop. It first executes the loop body
and then checks the condition. If the condition is true, the loop repeats.
If the condition is false, the loop stops.
The loop body executes before the condition is checked.
The do-while loop always runs at least once.
It is useful when you want to execute code first and validate the condition later.
Use a do-while loop when a block of code must execute at least once before checking the condition.
The syntax of a do-while loop is slightly different from a
while loop because the condition appears at the end.
initialization;
do {
// loop body
update;
} while (condition);
do-while loop must end with a semicolon
after the condition: while (condition);
The following diagram shows how a do-while loop works.
Java executes the loop body first and then checks the condition.
do block.while.true, the loop runs again.false, the loop stops.
The following program prints Hello five times using a
do-while loop.
public class DoWhileDemo {
public static void main(String[] args) {
int i = 1;
do {
System.out.println(i + ". Hello");
i++;
} while (i <= 5);
}
}
Output:
1. Hello 2. Hello 3. Hello 4. Hello 5. Hello
i is initialized with value 1.do block execute first.i with Hello.i++ increases the value of i by 1.i <= 5 is checked.i becomes 6, the condition becomes false and the loop stops.
In a do-while loop, the condition is checked after the loop body.
Therefore, even if the condition is false from the beginning, the loop body still runs once.
public class DoWhileRunsOnce {
public static void main(String[] args) {
int i = 10;
do {
System.out.println("This runs once");
} while (i < 5);
}
}
Output:
This runs once
while loop may execute zero times,
but a do-while loop always executes at least once.
This example calculates the sum of numbers from 1 to 5
using a do-while loop.
public class SumDoWhileLoop {
public static void main(String[] args) {
int i = 1;
int sum = 0;
do {
sum = sum + i;
i++;
} while (i <= 5);
System.out.println("Sum = " + sum);
}
}
Output:
Sum = 15
The do-while loop is commonly used in menu-driven programs because
the menu should be displayed at least once.
import java.util.Scanner;
public class MenuDoWhileExample {
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
int choice;
do {
System.out.println("MENU");
System.out.println("1. Say Hello");
System.out.println("2. Exit");
System.out.print("Enter your choice: ");
choice = sc.nextInt();
if (choice == 1) {
System.out.println("Hello User!");
} else if (choice == 2) {
System.out.println("Goodbye!");
} else {
System.out.println("Invalid choice!");
}
} while (choice != 2);
sc.close();
}
}
Sample Output:
MENU 1. Say Hello 2. Exit Enter your choice: 1 Hello User! MENU 1. Say Hello 2. Exit Enter your choice: 2 Goodbye!
| while Loop | do-while Loop |
|---|---|
| Condition is checked before loop body. | Condition is checked after loop body. |
| It may execute zero times. | It executes at least once. |
| It is entry-controlled. | It is exit-controlled. |
| Useful when condition should be checked first. | Useful when code must run at least once. |
while(condition);.while loop with do-while loop.do-while when the loop should not execute even once if the condition is false.do-while loop executes a block of code repeatedly.do-while loop is useful for menus and input validation.🧠 Test your understanding with a quick quiz
Topic: Do-while-loop | Language: Java
int i = 10;
do {
System.out.println("Hello");
} while (i < 5);int i = 1;
do {
System.out.println(i);
i++;
} while (i <= 3);🎉 Great job! Continue learning Java step by step.
Mark this lesson as completed to track your learning progress. To keep track across devices, please login and save your learning progress.
Discussion
Ask questions, share suggestions, or discuss this lesson.