Difference Between While And Do While Loop In java

By Faruque Ahamed Mollick

Do while and while states may look similar and also the functionality of this loop may look similar, but there is the difference between do while and while statement. In this tutorial, I am going to tell you the difference between these two types of statements and also going to show you the simple code snippets of these two types of loops.

  • In while loop in Java, the condition is tested at the beginning of the loop and if the condition becomes true, it will execute the code between the loop. As the condition tested very first, so somehow if the condition is false then the statements inside the while loop will never run.
  • In the case of doing while loop, first the statements execute and after that at the end the condition tested. If the condition is true then it returns to the statement again with the return value. But if the condition is false, the statements execute only once and then it stops the loop.

So from the above content, you have seen that do while loop in Java execute at least one time even if the condition is false and in the case of while loop, the statements never executes for the condition false.

Java Program to Print Star Triangle Pattern Using for Loop

Reading A Text File Line By Line In Java With Example

You will understand better if you see example code. Please check the example below if you are confused because the example is always a better way to understanding.

Java while loop example

In the below example we are going to take an integer variable and then assign 0 to it. After that, we will check whether the integer is greater than 5 or not. We have used another one System.out.println outside the loop. We are going to make the condition false.

The Java code:

public class TestWhile {
  
  public static void main(String[] args) {
    int num = 0;
    
    while (num > 5)  {
      System.out.println("Number is Greater Than 5");
    }
    System.out.println("This Statement is Outside of the while loop");
  }
}

The above code will only return the below text:

This Statement is Outside of the while loop

 

Java do while loop example

here we are going to check the same condition like we did in while loop and here we are also going to make the condition false.

Java code:

public class TestDoWhile {
  
  public static void main(String[] args) {
    int num = 0;
    
    do {
      System.out.println("Number is Greater Than 5");
    }while (num > 5);
    System.out.println("This Statement is Outside of while loop");
  }
}

From the above code, it is clear that the condition is false. But it will still execute the statement once because the condition is tested after the execution of the statements.

Below is the given output of do while loop:

Number is Greater Than 5
This Statement is Outside of while loop

I hope you understand the main difference between while and do while loop. You can also test the above codes on your machine.

Leave a Reply

Your email address will not be published. Required fields are marked *