1. Getting started with the do-while loop
Imagine you are writing a program that guesses a number chosen by the user. You need to ask for the number at least once and keep asking until you guess it. If you check the condition beforehand, a regular while may not enter the loop at all.
This is where do-while helps. Unlike while, it will always execute the loop body at least once — even if the condition is immediately false. It’s like getting a piece of gum at the checkout regardless of whether you made a purchase.
Do-while syntax
It’s simple: just one more “pair” of keywords in Java.
do
{
// Loop body: this code will run at least once
}
while (condition);
do-while loop
Pay attention to the semicolon ; at the end after the condition’s parenthesis! In a regular while it is absent, but here it is — a classic pitfall for beginners.
Steps of the do-while loop
- Enter the loop body (always at least once).
- Check the condition.
- If the condition is true — repeat the body.
- If the condition is false — exit the loop.
2. Difference from a regular while
Recall the basic flow of a regular while:
while (condition)
{
// Loop body
}
The body will not run even once if the condition is false from the very beginning!
Now an example with do-while:
do
{
// Loop body
}
while (condition);
The loop body will run at least once, even if the condition is false immediately!
Let’s illustrate the difference with code:
// Regular while
int count = 0;
while (count > 0)
{
System.out.println("count = " + count);
count--;
}
// Nothing will be printed
// do-while
int count2 = 0;
do
{
System.out.println("count2 = " + count2);
count2--;
}
while (count2 > 0);
// It will print "count2 = 0" even though the condition is false!
Sometimes this may seem “odd”, but this is exactly the behavior you need for user input and data validation.
3. Practical use cases for do-while
Where does this loop appear most often?
Data validation
A common scenario: ask the user to enter valid data (for example, a password at least 6 characters long) and repeat the prompt until everything is correct.
String password;
do
{
System.out.print("Enter a new password (at least 6 characters): ");
password = console.nextLine();
}
while (password.length() < 6);
System.out.println("Password accepted!");
The loop body will execute at least once — even if the user immediately enters a long password.
Menus in console applications
A common pattern: show a menu, wait for a command, and exit only on a special command.
String command;
do
{
System.out.println("Menu:");
System.out.println("1. Show greeting");
System.out.println("2. Exit");
System.out.print("Choose an action: ");
command = console.nextLine();
if (command.equals("1"))
{
System.out.println("Hello, world!");
}
else if (!command.equals("2"))
{
System.out.println("Unknown command.");
}
}
while (!command.equals("2"));
System.out.println("Goodbye!");
4. Table: comparing while with other loops
| Loop type | Condition check | How many times the body can run | When appropriate |
|---|---|---|---|
|
Before | 0 or more times | When the number of repetitions is unknown in advance |
|
After | 1 or more times | When it is important to execute the body at least once |
|
Before | 0 or more times | When the number of iterations is known |
GO TO FULL VERSION