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:
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
Explanation:
- do-while loop executes the code block at least once because the condition is checked after the loop body runs.
- In each iteration, the value of c is printed and then incremented using c++.
- The loop continues until the condition c <= 5 becomes false, after which the loop terminates.
Syntax:
do
{
// Loop Body
Update_expression
}
// Condition check
while (test_expression);
Parameters:
- test_expression: boolean condition that controls the loop
- update_expression: updates the loop variable
Note: The test_expression in a do-while loop must evaluate to a boolean value; otherwise, a compile-time error occurs.
Working of Do-While Loop

Example:
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:
- The do block executes once before the condition is checked, so "Print Statement" is printed even though the condition is false.
- The variable c starts at 0 and is incremented to 1 inside the loop.
- The condition c < 0 is false, so the loop terminates after the first iteration
Flowchart do-while loop:

Example 1: This program will try to print "Hello World" 5 times.
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:
- The variable c is initialized to 1 before entering the loop.
- The do block executes first and prints "Hello World", ensuring the loop runs at least once.
- After printing, c is incremented using c++.
- The loop continues as long as the condition c < 6 is true, resulting in the message being printed 5 times.
Example 2: do-while loop without curly braces {}
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!
Explanation:
- The do-while loop executes the do block at least once, even though the condition c >= 3 is false.
- Since there is only one statement inside the loop, it runs once and then terminates when the condition fails.
Related Articles: