#1
When you're learning how to program in Java, one of the key concepts you'll come across is loops. Loops allow your code to repeat actions automatically, which is super useful when you want to perform a task multiple times without writing the same code over and over.
Among Java’s looping options—for, while, and do-while—the do-while loop is a bit unique. It guarantees that your code block runs at least once, even if the condition is false.
In this guide, we’ll focus on Java’s do-while statement: what it is, how it works, when to use it, and how it’s different from other loops.

What Is a Do-While Loop?

A do-while loop is a type of loop in Java that runs a block of code at least once, and then continues running it again and again as long as a specified condition is true. The basic format looks like this:
do {
    // Code to run
} while (condition);
Unlike other loops, the condition is checked after the code runs—this means the loop will always run at least once, no matter what.

Do-While vs While: What’s the Difference?

Here’s a quick comparison:

While Loop

while (condition) {
    // Code runs only if the condition is true
}

Do-While Loop

do {
    // Code runs at least once, then repeats if condition is true
} while (condition);
The key difference:
  • while checks the condition first.
  • do-while runs the code first, then checks the condition.

A Simple Example

Let’s look at a basic example of a do-while loop:
int count = 1;

do {
    System.out.println("Count is: " + count);
    count++;
} while (count <= 5);
Output:
Count is: 1  
Count is: 2  
Count is: 3  
Count is: 4  
Count is: 5
Here’s what’s happening:
  • The loop starts by printing "Count is: 1"
  • After each print, count increases by 1
  • It continues as long as count is less than or equal to 5

What Happens When the Condition Is False?

Even if the condition is false to begin with, the do-while loop runs once.
Example:
int number = 10;

do {
    System.out.println("This will run once.");
} while (number < 5);
Output:
This will run once.
Even though number < 5 is false, the code still executes once before checking the condition.

When to Use a Do-While Loop

Use a do-while loop when:
  • You want the code to run at least once, no matter what.
  • You’re taking user input and want to validate it.
  • You’re building a menu or prompt that should display before making a decision.

Example: User Input Validation

import java.util.Scanner;

public class InputExample {
    public static void main(String[] args) {
        Scanner scanner = new Scanner(System.in);
        int number;

        do {
            System.out.print("Enter a number between 1 and 10: ");
            number = scanner.nextInt();
        } while (number < 1 || number > 10);

        System.out.println("You entered: " + number);
    }
}
This code keeps asking the user for a valid number until they enter one between 1 and 10. But even if their first guess is correct, the prompt still appears—because do-while always runs once.

How the Do-While Loop Works Step-by-Step

Let’s break it down:
  1. Do: Run the code inside the {} braces.
  2. While: Check the condition at the bottom.
  3. If the condition is true, go back and repeat.
  4. If the condition is false, exit the loop.
That’s it! Simple and predictable.

Tips for Using Do-While Loops

  • Avoid infinite loops: Always make sure something inside the loop changes the condition. Otherwise, it could run forever.
    // Don't do this!
    do {
        System.out.println("Oops...");
    } while (true); // Infinite loop!
  • Use comments if your loop is doing something complex.
  • Use meaningful variable names to keep your code easy to understand.

Real-Life Use Cases

Here are a few examples of when a do-while loop can be helpful:
Use Case Why Use Do-While?
User menu in a console app Menu shows at least once before exiting
Validating user input Prompt runs at least once
Retry logic for network calls Attempt happens once, retry if needed
Setup logic before condition Setup must happen before checking condition

Common Mistakes to Avoid

  • Missing the semicolon (;) at the end of the while line:
    Incorrect:
    } while (x < 10)
    Correct:
    } while (x < 10);
  • Not changing the condition inside the loop, causing an infinite loop.
  • Using do-while when you don’t need the code to run at least once—in those cases, a regular while or for loop may be better.

image quote pre code