Java Do While Loop

The Java Do While loop will test the given condition at the end of the loop. So, Java Do While loop executes the statements inside the code block at least once even if the given condition Fails.

The While Loop tests the condition before entering into the code block. If the condition is True, then only statements inside the loop will be executed. Otherwise, statements will not execute at least once. There are some situations where it is necessary to perform some operations (execute some statements) first, and then check for the condition. In these cases, we can go for Java Do While loop.

Java Do While loop Syntax

The syntax of Do While Loop in Java Programming language is as follows:

-- Java Do While Loop Syntax
do  {
     statement 1;
     statement 2;
      ………….
     statement n;
} While (condition);

First, it will execute the statements inside the loop, and then after reaching the end, it will check the condition inside the while. If the condition is True, then it will repeat the process. If the condition fails, then Java Do While Loop will be terminated.

NOTE: We have to place semi-colon after the While condition.

Java Do While Loop Flow Chart

The flow chart behind the Java Do While Loop

Flow Chart of a Java Do While Loop

Java do while loop Flow chart sequence is:

  1. First, we initialize our variables. Next, it will enter into the Do While loop.
  2. It will execute the group of statements inside the do while loop.
  3. Next, we have to use Increment & Decrement Operator inside the Java do while loop to increment or decrements the value. Please refer to Increment and Decrement Operators article in Java to understand increment and decrement operator.
  4. Now it will check for the condition. If the condition is True, then the statements within the Java Do while loop will execute again. It will continue the process as long as the condition is True.
  5. If the condition is False, then it will exit from the loop.

Java Do While Loop example

This program helps us to understand the Do While Loop in Java. This Java program allows the user to enter an integer value below 10. By using this value, the Javac compiler will add those values up to 10.

// Java Do While Loop example 
package Loops;

import java.util.Scanner;

public class DoWhileLoop {
	private static Scanner sc;
	
	public static void main(String[] args) {
		int number, sum = 0;
		sc = new Scanner(System.in);	
		
		System.out.println("\n Please Enter the any integer Value below 10: ");
		number = sc.nextInt();
		
		do  {
			sum = sum + number;
			number--;
		} while (number > 0); 
		System.out.format(" Sum of the Numbers From the While  Loop is: %d ", sum);
	}
}

OUTPUT: We are going to enter number = 4. It means, total = 4 + 3 + 2 + 1 = 10

Java Do While Loop 1

Within this Java do while loop example, we used the Do While loop. The Condition inside the While (number > 0) will make sure that the number is greater than 0.

do  {
	sum = sum + number;
	number--;
} while (number > 0);

In this do while loop example, User Entered value: Number = 4, and we initialized the sum = 0

First Iteration
sum = sum + number
sum = 0 + 4 ==> 4

Next, the number will be decremented by 1 (number –).
Next, the condition inside the while is True or Not. Here, 3 > 0 is True

Second Iteration: Within the first Iteration, the values of both Number and sum changed as Number = 3 and sum = 4
sum = 4 + 3 ==> 7
After the decrement (number –), check the While Loop condition. Here, 2 > 0 is True

Third Iteration
Within the Second Iteration of Java Do While Loop, the values of both Number and sum changed as Number = 2 and sum = 7
sum = 7 + 2 ==> 9

After the decrement, the condition (1 > 0) is True

Fourth Iteration
After the third Iteration, Number = 1 and sum = 9
sum = 9 + 1 ==> 10

Next, the number will be decremented by 1 (number –).

Next, the condition inside the while is True or Not. Here, 0 > 0 is False.

Last System.out.format statement in the do while loop example will print the number of digits present in the given number as output.

System.out.format(" Sum of the Numbers From the While  Loop is: %d ", sum);

Infinite Do While Loop in Java

If you forgot to increment or decrement the value inside the Java do while loop, then the do while loop will execute infinite times (also called as an infinite loop). For example:

// Infinite Do While Loop in Java Programming example 
 
package Loops;

public class InfiniteDoWhile {
	public static void main(String[] args) {
		int number = 1;
		
		do  {
			System.out.format(" Numbers From the While Loop is: %d \n", number);
		}while (number <= 10);
	}
}
 Numbers From the While Loop is: 1 
 Numbers From the While Loop is: 1 
 Numbers From the While Loop is: 1 
 Numbers From the While Loop is: 1 
 Numbers From the While Loop is: 1 
 Numbers From the While Loop is: 1 
 Numbers From the While Loop is: 1 
 ...
 ...

Here, in this Java do while loop, the number is always 1, and the number is always less than 10. So, the do while loop will go on execute the statements infinite times. Now, let us add an increment operator (number++) inside the do while loop to the above example.

// Infinite Do While Loop in Java Programming example 
 
package Loops;

public class InfiniteDoWhile {
	public static void main(String[] args) {
		int number = 1;
		
		do  {
			System.out.format(" Numbers From the While Loop is: %d \n", number);
			number++;
		}while (number <= 10);
	}
}

Now, when it reaches 10, the condition will fail. Let us see the output of this program.

 Numbers From the While Loop is: 1 
 Numbers From the While Loop is: 2 
 Numbers From the While Loop is: 3 
 Numbers From the While Loop is: 4 
 Numbers From the While Loop is: 5 
 Numbers From the While Loop is: 6 
 Numbers From the While Loop is: 7 
 Numbers From the While Loop is: 8 
 Numbers From the While Loop is: 9 
 Numbers From the While Loop is: 10