Open In App

Java Do While Loop

Last Updated : 12 Aug, 2025
Comments
Improve
Suggest changes
Like Article
Like
Report

Java do-while loop is an Exit control loop. Unlike for or while loop, a do-while check for the condition after executing the statements of the loop body.

Example:

Java
public class GFG {
  
    public static void main(String[] args) {
      
        int c = 1;

      // Using do-while loop
        do {
            System.out.println("GeeksforGeeks: " + c);
            c++;
        } while (c <= 5);
    }
}

Output
GeeksforGeeks: 1
GeeksforGeeks: 2
GeeksforGeeks: 3
GeeksforGeeks: 4
GeeksforGeeks: 5

Practical Application of Do While Loop

Suppose you are implementing a game where you show some options to the user, press 1 to do this .., press 2 to do this .. etc and press 'Q' to quit the game. So here you want to show the game menu to the user at least once, so you write the code for the game menu inside the do-while loop.

Syntax: 

do
{
// Loop Body
Update_expression
}

// Condition check
while (test_expression);

Note: The test_expression for the do-while loop must return a boolean value , else we would get compile-time error.

Example:

Java
class GFG {

    public static void main(String[] args)
    {
        // initial counter variable
        int c = 0;

        do {

            // Body of loop that will execute minimum 1 time for sure no matter what
            System.out.println("Print Statement");
            c++;
        }

        // Checking condition
        // Note: It is being checked after inimum 1 iteration
        while (c < 0);
    }
}

Output
Print Statement

Explanation: In the above code, we figured out that the condition is checked later as the body inside do will get executed one time without fail as the condition is checked later onwards.

Hence whenever we want to display the menu and later on proceed command on the terminal, we always use do-while loop.

Components of do-while Loop  

A. Test Expression: In this expression, we have to test the condition. If the condition evaluates to true then we will execute the body of the loop and go to update expression. Otherwise, we will exit from the while loop. For example: 

i <= 10

B. Update Expression: After executing the loop body, this expression increments/decrements the loop variable by some value. For example:  

i++;

Execution of do-While loop 

1. Control falls into the do-while loop.
2. The statements inside the body of the loop get executed.
3. Updation takes place.
4. The flow jumps to Condition
5. Condition is tested. 

  • If Condition yields true, go to Step 6.
  • If Condition yields false, the flow goes outside the loop

6. The flow goes back to Step 2.

Flowchart do-while loop:

Example 1: This program will try to print "Hello World" 5 times.  

Java
class GFG {
    public static void main(String args[])
    {
        // Declaring and initialization expression
        int c = 1;

        // Do-while loop
        do {
            // Body of do-while loop
            // Print statement
            System.out.println("Hello World");

            // Update expression
            c++;
        }
        // Test expression
        while (c < 6);
    }
}

Output
Hello World
Hello World
Hello World
Hello World
Hello World

Explanation:

  • int c = 1; → Start counter at 1.
  • do-while loop → Runs at least once.
  • System.out.println("Hello World"); → Prints message.
  • c++; → Increase counter by 1.
  • Loop runs while c < 6.
  • Prints "Hello World" 5 times.

Example 2: do-while loop without curly braces {}

Java
import java.io.*;

class GFG {
    public static void main(String[] args) {
        int c = 1;

        do {
            // Only single statement in do block
            System.out.println("Hello GFG!");
        } 
        // This condition is false, so the loop will execute only once
        while (c >= 3);
    }
}

Output
Hello GFG!

Related Articles:


Do While Loop
Visit Course explore course icon
Video Thumbnail

Do While Loop

Video Thumbnail

Java do-while loop with Examples

Article Tags :