CodeGym /Courses /JAVA 25 SELF /Do-while loop: introduction and nuances

Do-while loop: introduction and nuances

JAVA 25 SELF
Level 4 , Lesson 3
Available

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);
Syntax of the 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

  1. Enter the loop body (always at least once).
  2. Check the condition.
  3. If the condition is true — repeat the body.
  4. 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
while
Before 0 or more times When the number of repetitions is unknown in advance
do-while
After 1 or more times When it is important to execute the body at least once
for
Before 0 or more times When the number of iterations is known
1
Task
JAVA 25 SELF, level 4, lesson 3
Locked
First game launch 🎮
First game launch 🎮
1
Task
JAVA 25 SELF, level 4, lesson 3
Locked
PIN Code Input 💳
PIN Code Input 💳
1
Task
JAVA 25 SELF, level 4, lesson 3
Locked
Interactive terminal 💻
Interactive terminal 💻
1
Task
JAVA 25 SELF, level 4, lesson 3
Locked
New account registration 🔒
New account registration 🔒
Comments (2)
TO VIEW ALL COMMENTS OR TO MAKE A COMMENT,
GO TO FULL VERSION
Siddhu545 Level 11, Liverpool, United Kingdom
4 June 2026
Lesson i learned : always write invalid condition in while loop Keep looping while the input is invalid.
akshay pednekar Level 5, CodeGym University in India, India
17 March 2026
do { // Prompt for PIN code input System.out.println("Enter PIN"); // read an integer pin = console.nextInt(); } while (!(pin >=1000 && pin <= 9999));