Java Do While Loop

The Java Do While loop will test the given condition at the end of the loop. So, it 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, only statements inside it will be executed. Otherwise, statements will not execute at least once. In some situations, 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:

do  {
     statement 1;
     statement 2;
      ………….
     statement n;
} While (condition);

First, it will execute the statements inside curly brackets, 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 a semi-colon after the While condition.

The flow chart of the Java Do While Loop

Java Do While Loop Flow Chart

The Java Do while loop Flow chart sequence is:

  1. First, we initialize our variables. Next, it will enter into the flow.
  2. It will execute the group of statements inside it.
  3. Next, we have to use Increment & Decrement Operator inside the Java do while loop to increment or decrease the value. Please refer to Increment and Decrement Operators article in Java to understand increment and decrement operators.
  4. Now it will check for the condition. If the condition is True, the statements within the 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 it.

Java Do While Loop Example

This program helps us to understand Java Do While Loop. This 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.

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 the number = 4. It means, total = 4 + 3 + 2 + 1 = 10

Java Do While Loop 1

Within this Java example, we used the Do While loop. The Condition inside the While (number > 0) will ensure that the number exceeds 0.

In this 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 condition. Here, 2 > 0 is True.

Third Iteration
Within the Second Iteration of Java Do While Loop, the values of both 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.

The last System.out.format statement in the example will print the number of digits in the given number as output.

Infinite Do While Loop in Java

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

package Loops;

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

Here, the number is always 1, and the number is always less than 10. So, it will go on executing the statements infinite times. Now, let us add an increment operator (number++) inside the. iteration to the above example.

package Loops;

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

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

1
2
3
4
5
6
7
8
9
10