Facebook Page Twitter Page LinkedIn Page
× Java Tutorial Java Advanced Java Resources

The Java do keyword is used with while keyword to create a do-while loop. A while loop allows a set of statements to be executed repeatedly until a given condition is true.

Whereas, the do-while loop is a variant of while loop which execute statements before checking the conditions. Therefore the do-while loop executes statements at least once.


do {
while (condition);

Flow Diagram:

Java Do-While Loop


In the below example, even if the condition is not fulfilled, the do-while loop executes the statements once.

public class MyClass {
  public static void main(String[] args) {
    int i = 10;
    int sum = 0;
        sum = sum + i;
        i = i+1;
    while (i <= 5);

The output of the above code will be:


❮ Java Keywords