While and do/while loops in Java | Explained

Loops are widely used in every programing language because of their notable features like reusability of code, time-saving, etc. Java loops allow us to run a block of code/tasks n number of times based on a condition. The test condition returns a boolean value i.e. true or false and a loop keeps iterating unless the test condition returns a false value. Java provides multiple types of loops such as. for loop, while loop, etc.

This write-up will present a complete overview of while and do-while loops and in this regard following concepts will be considered in this article:

  • while and do-while loops in Java
  • Basic syntax of while and do-while in Java
  • How to use while and do-while loops in Java

So, let’s begin!

While loop in Java

The while loop runs a code repeatedly/continuously until the test condition stays true. It takes a condition within the parenthesis(), if the specified condition is true then the block of code written within the loop’s body will be executed.

The basic syntax of the while loop is described in the following snippet:

  //statements or block of code
  // increment/decrement;

From the above-given syntax, we noted the following points:

  • The condition comes at the start.
  • If the specified condition is true, all the code within the loop’s body will execute.
  • Loop will stop when the particular condition turns false.
  • If the given condition is not true in the initial/first iteration then the loop’s body wouldn’t execute a single time.
  • Increment or decrement must be stated within the loop’s body otherwise the loop wouldn’t stop.

How to Use do while loop in Java

Let’s consider an example to understand how the do-while loop works:

The below example will print the numbers from 1 to 4 and their respective squares using while loop:

public static void main(String[] args) {
 int number=1;
   System.out.println("Number is : "+ number);
   System.out.println("Square of the" + number + "is : "+ number*number);

The below figure shows the complete code and output of while loop:

The output shows that the while loop executes and prints the square until the condition stays true.

Now let’s consider the below snippet to understand how while loop works if the condition is false in the first iteration:

The output verifies that if the condition is false in the very first iteration then the while loop didn’t execute a single time.

do-while loop in Java

It is very much similar to the while loop, the significant difference among both these loops is that the do-while loop ensures that it will execute at least once.

The below-given snippet presents the syntax of the do-while loop:

  // statement or block of code
  // increment/decrement;
} while(condition);

From the syntax, we can observe the following key points:

  • The condition occurs at the end.
  • All the code within the loop’s body will execute at least one time before the condition is tested(even if the condition is not true).
  • If the condition is true then the control will be shifted back to the do and all the statements within the loop’s body will execute again until the condition stays true.
  • Increment/decrement must be specified within the loop’s body.
  • The loop will stop its execution when the condition becomes false.

How to Use do while loop in Java

The practical implementation will present a better understanding of the do-while loop, so, let’s consider the below-given example:

Let’s modify the previous example a little bit and utilize the do-while loop instead of while loop:

public static void main(String[] args) {
 int number=1;
    System.out.println("Number is : "+ number);
    System.out.println("Square of " + number + " is : "+ number*number);
    number ++;

Following will be the output for above code snippet:

The do-while loop prints the first four numbers and their respective squares.

In the below-given code snippet, the condition is “number should be less than 5’’ however the number provided is equal to 6 and already greater than “5”, however, the above code generates the following output:

The output verifies that even if the condition was false but the code executed one time and hence it shows the square of the number “6”.


The while loop iterates the body of the loop until the condition stays true, if the condition is false in the first iteration then the loop wouldn’t execute. On the other hand, the do-while loop specifies the condition at the end so it executes the block of code at least one time irrespective of the condition. This write-up demonstrates a comprehensive guide for the while and do-while loop.

About the author

Anees Asghar

I am a self-motivated IT professional having more than one year of industry experience in technical writing. I am passionate about writing on the topics related to web development.