Loops in Java is an essential topic that is considered a key feature for coding. The loops in Java execute a certain block of code until the condition specified remains “true”. The basic three types of loops are for, while, and do-while loop in Java. All three loops consist of the same basic functionality except for the difference in syntax and the working.
This write-up will effectively demonstrate the implementation of one of the loops in Java that is known as the “do-while” loop.
Java do-while loop with Examples
The do-while loop in Java runs the block of code at least once then it repeatedly executes the code block until the declared condition remains “True”. This loop checks the condition when each iteration is ended and therefore, is termed an “ Exit control loop”. The syntax of the do-while consists of a “test Expression” that is explained below
do {
// loop’s body
} while(textExpression);
In the syntax above:
- The body of the loop is executed first.
- Once the loop’s body is executed, the “testExpression” will be evaluated.
- If the “testExpression” is found to be true, the loop’s body is executed again.
- This process will be repeated until the “testExpression” becomes “false”.
Example 1: Basic Implementation of the do-while Loop in Java
The below example shows how the numbers are printed from 1 to 7 using the do-while loop.
//Import the Package
import java.util.Scanner;
//Declare a Class
class LoopExample1 {
public static void main(String[] args) {
int a = 1, b = 7;
System.out.println("The numbers printed using do-while loop are: " );
//do-while loop in Java
do {
System.out.println(a);
a++;
} while(a <= b);
}
}
In the above code block:
- The Scanner class of the util package is imported.
- A class is declared as “LoopExample1” in the next step.
- The numbers are initialized as a=1 and b=7 respectively.
- The last part of the code implements the do-while loop along with the println() method that prints the number from “1” to “7”.
Output
The output below demonstrates the implementation of the do-while as the numbers from 1 to 7 are printed.

Example 2: Print the String a Certain Number of Times Using the do-while Loop
The strings can be printed a specific number of times using the do-while loop as displayed in the code below.
//Import the Package
import java.util.Scanner;
//Declare a Class
class LoopExample2 {
public static void main(String[] args) {
int a = 1;
System.out.println("The Strings printed using do-while loop are: " );
//do-while loop in Java
do {
System.out.println("Java Programming");
a++;
} while(a < 4);
}
}
This code block shows that:
- A string is declared as “Java Programming”.
- In the next step, the do statement increments the variable “a” and the while loop contains the condition “a < 4” which means the loop will execute three times to print the declared string.
Output
The output below shows that “Java Programming” is printed 3 times according to the given condition.

Example 3: Calculate the Sum of Integers
Another application of the do-while loop is to calculate the sum of the integers specified.
//Declare the class
class LoopExample3 {
// Main method of Java
public static void main(String args[])
{
//Initializing the value
int a = 35, s = 0;
// Do-while loop
do {
s += a;
a--;
}
// Now looking for the condition
while (a > 10);
// Adding the integers
System.out.println("The Sum of Numbers are: " + s);
}
}
In the above code block:
- The integers are declared as “a = 35” and the sum is initialized as “s = 0”.
- The do statement calculates the sum of the numbers and the while loop checks for the condition.
- The do-while loop will compute the sum of numbers starting from 35 and decreasing by 1 in each iteration until the value of “a” becomes “10” or less.
- The output for the sum of the numbers is obtained using the println() method.
Output
The output below shows that “575” is the sum of the numbers using the do-while loop.

Example 4: Iterating Over an Array Using the do-while Loop
One of the applications is to iterate over an array in Java using the do-while loop. The elements in the array declared are iterated as per the condition declared in the do-while loop.
//Declare the class
class Example5 {
// Main driver method
public static void main(String args[])
{
//Initializing the values
int arr1[]={1,5,9,15,23,58};
int a=0;
System.out.println("The Array is: ");
// Do-while loop
do {
System.out.println(arr1[a]);
a++;
}
// Now looking for the condition
while (a < 6);
}
}
In the above code block:
- The integer array is declared as “arr1”.
- The do statement increments the iterator while checking the condition declared in the while loop.
- When the condition is fulfilled the loop stops executing.
Output
In the below output, the elements of the Array are printed using the do-while loop in Java.

This sums up the discussion on do-while in Java.
Conclusion
The do-while loop in Java executes the code block until the condition specified remains true. The main benefit of this loop is that it runs the code once even if the condition has already appeared as “false”. In this blog post, we have implemented the do-while loop in Java using different examples.