Lesson 25 of 50 – Java Do-While Loop
50%

Java Do-While Loop

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.

1. What Is a Do-While Loop?

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
Remember: a do-while loop checks its condition after executing the loop body.
2. Syntax of a Do-While Loop
do {
    // Code to execute
} while (condition);

The syntax contains three important parts:

  • do: Starts the loop body.
  • Loop body: Contains the statements to execute.
  • while: Checks the condition after the body executes.

Notice that a semicolon is required after the while (condition) statement.

3. How Does a Do-While Loop Work?
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.

4. Do-While Loop Executes at Least Once

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.

5. Print Numbers from 1 to 10
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
6. Print Numbers in Reverse Order

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.

7. Calculate the Sum of Numbers

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.

8. Multiplication Table Using Do-While

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
9. Do-While Loop with User Input

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.
10. Menu-Driven Program

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.

11. Common Do-While Loop Mistakes
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);
12. Difference Between While and Do-While
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

13. Practice Assignments

Write Java programs to solve these beginner-level assignments.

  1. Print numbers from 1 to 20 using a do-while loop.
  2. Print numbers from 20 to 1.
  3. Print even numbers from 2 to 50.
  4. Print odd numbers from 1 to 25.
  5. Print the multiplication table of 9.
  6. Calculate the sum of numbers from 1 to 100.
  7. Calculate the factorial of a positive integer.
  8. Ask the user for numbers until zero is entered.
  9. Create a menu-driven calculator that repeats until the user exits.
  10. Count the digits in a positive integer.
Tip: Use a do-while loop when the task must happen at least once, such as displaying a menu or requesting an initial user input.

🧠 Quick Quiz

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?