A do-while loop in Java executes a block of code at least once and then checks the condition. If the condition is true, the loop executes again.
A do-while loop is useful when you want the code to execute at least once, even if the condition is false initially.
int i = 1;
do {
System.out.println(i);
i++;
} while (i <= 5);
Output:
1
2
3
4
5
do {
// Code to execute
} while (condition);
The syntax contains three important parts:
Notice that a semicolon is required after the
while (condition) statement.
int i = 1;
do {
System.out.println(i);
i++;
} while (i <= 3);
| Iteration | Value Printed | Condition After Update |
|---|---|---|
| 1 | 1 | 2 <= 3 (true) |
| 2 | 2 | 3 <= 3 (true) |
| 3 | 3 | 4 <= 3 (false) |
The loop stops when the condition becomes false.
Consider an example where the condition is false from the beginning.
int i = 10;
do {
System.out.println(i);
i++;
} while (i <= 5);
Output:
10
Although i <= 5 is false, the body executes
once before the condition is checked.
public class Main {
public static void main(String[] args) {
int i = 1;
do {
System.out.println(i);
i++;
} while (i <= 10);
}
}
Output:
1
2
3
4
5
6
7
8
9
10
Print numbers from 10 to 1 using a do-while loop.
int i = 10;
do {
System.out.println(i);
i--;
} while (i >= 1);
Output:
10
9
8
7
6
5
4
3
2
1
The i-- operator decreases the value of i by 1.
Calculate the sum of numbers from 1 to 10.
public class Main {
public static void main(String[] args) {
int i = 1;
int sum = 0;
do {
sum = sum + i;
i++;
} while (i <= 10);
System.out.println("Sum = " + sum);
}
}
Output:
Sum = 55
The sum variable stores the total of all numbers.
Print the multiplication table of 5.
public class Main {
public static void main(String[] args) {
int number = 5;
int i = 1;
do {
System.out.println(number + " x " + i
+ " = " + (number * i));
i++;
} while (i <= 10);
}
}
Output:
5 x 1 = 5
5 x 2 = 10
5 x 3 = 15
5 x 4 = 20
5 x 5 = 25
5 x 6 = 30
5 x 7 = 35
5 x 8 = 40
5 x 9 = 45
5 x 10 = 50
This program repeatedly asks the user to enter a number until the user enters zero.
import java.util.Scanner;
public class Main {
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
int number;
do {
System.out.print("Enter a number (0 to stop): ");
number = sc.nextInt();
System.out.println("You entered: " + number);
} while (number != 0);
System.out.println("Program stopped.");
sc.close();
}
}
Example interaction:
Enter a number (0 to stop): 5
You entered: 5
Enter a number (0 to stop): 8
You entered: 8
Enter a number (0 to stop): 0
You entered: 0
Program stopped.
A do-while loop can be used to display a menu at least once and repeat it until the user chooses to exit.
import java.util.Scanner;
public class Main {
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
int choice;
do {
System.out.println("1. Say Hello");
System.out.println("2. Show a Message");
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("Welcome to Java.");
break;
case 3:
System.out.println("Goodbye!");
break;
default:
System.out.println("Invalid choice.");
}
} while (choice != 3);
sc.close();
}
}
The menu repeats until the user enters 3.
| Mistake | Correct Approach |
|---|---|
| Forgetting the semicolon after the while condition | Write while (condition); |
| Forgetting to update the loop variable | Update it with i++ or another suitable expression. |
| Using the wrong condition | Check whether the loop should continue or stop. |
| Assuming the body never runs when the condition is false | Remember that a do-while body executes once before the condition check. |
Correct syntax:
int i = 1;
do {
System.out.println(i);
i++;
} while (i <= 5);
| While Loop | Do-While Loop |
|---|---|
| Checks the condition before executing the body. | Checks the condition after executing the body. |
| May execute zero times. | Executes at least once. |
Syntax begins with while. |
Syntax begins with do. |
| No semicolon is needed after the while condition when followed by a block. | A semicolon is required after the while condition. |
While loop example:
int i = 10;
while (i <= 5) {
System.out.println(i);
}
Output: Nothing
Do-while example:
int i = 10;
do {
System.out.println(i);
} while (i <= 5);
Output: 10
Write Java programs to solve these beginner-level assignments.
1. When is the condition of a do-while loop checked?
2. What is the minimum number of times a do-while loop executes?
3. Which punctuation mark is required after the while condition?