Difference between while and do while loop in java

Let’s learn difference between while and do while loop in java.

Difference between while and do while loop in java

While loopdo while loop
In while loop condition is checked first and then statements are executed.In do while loop statements are executed atleast once and then condition is checked.
In while loop statements are executed zero times if condition is false.In do while loop statements are executed at least once.
while loop statements executes code if the condition is true.do while loop executes statements at least once even if condition fails.

Let’s see an example on difference between while and do while in java.

// while loop runs zero times
int a = 20;
while(a < 5)  // here condition executes false
{
   System.out.println("hello world");
   a++;
}

---------------------------------------------------------

// do-while loop
int a = 20;
do{
   System.out.println("hello world");
   a++;
}while(a < 5);  // here condition executes false



Also read – major features of java