Let’s learn difference between while and do while loop in java.
difference between while and do while loop in java
while loop | do while loop |
while loop will check condition before the loop starts. | do while loop will check condition after the loop starts. |
while loop will run zero times. | do while loop will run at least once. |
while loop executes code only 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