The difference between a do-while and a while is when the comparison is done. With a do-while, you'll compare at the end and hence do at least one iteration.

Equivalent code for your example

do
{ 
    i++; 
    ++j;
    System.out.println( i * j );

}
while ((i < 10) && (j*j != 25));

is equivalent to:

i++; 
++j;
System.out.println( i * j );
while ((i < 10) && (j*j != 25)) {
    i++; 
    ++j;
    System.out.println( i * j );
}

General comprehension

A do-while loop is an exit controlled loop which means that it exits at the end. A while loop is an entry controlled loop which means that the condition is tested at the beginning and as a consequence, the code inside the loop might not even be executed.

do {
    <block>
} while (<condition>);

is equivalent to:

<block>
while (<condition>) {
    <block>
};

Use case

A typical use case for a do-while is the following: you ask the user something and you want do repeat the operation while the input is not correct.

do {
   // Ask something
} while (input is not correct);

In that case, you want to ask at least once and it's usually more elegant than using a while which would require either to duplicate code, or to add an extra condition or setting an arbitrary value to force entering the loop the first time.

At the opposite, while loops are much more commons and can easily replace a do-while (not all languages have both loops).

Answer from Maxime Chéramy on Stack Overflow
Top answer
1 of 4
20

The difference between a do-while and a while is when the comparison is done. With a do-while, you'll compare at the end and hence do at least one iteration.

Equivalent code for your example

do
{ 
    i++; 
    ++j;
    System.out.println( i * j );

}
while ((i < 10) && (j*j != 25));

is equivalent to:

i++; 
++j;
System.out.println( i * j );
while ((i < 10) && (j*j != 25)) {
    i++; 
    ++j;
    System.out.println( i * j );
}

General comprehension

A do-while loop is an exit controlled loop which means that it exits at the end. A while loop is an entry controlled loop which means that the condition is tested at the beginning and as a consequence, the code inside the loop might not even be executed.

do {
    <block>
} while (<condition>);

is equivalent to:

<block>
while (<condition>) {
    <block>
};

Use case

A typical use case for a do-while is the following: you ask the user something and you want do repeat the operation while the input is not correct.

do {
   // Ask something
} while (input is not correct);

In that case, you want to ask at least once and it's usually more elegant than using a while which would require either to duplicate code, or to add an extra condition or setting an arbitrary value to force entering the loop the first time.

At the opposite, while loops are much more commons and can easily replace a do-while (not all languages have both loops).

2 of 4
3

The key difference between do-while and while, with do-while you are guaranteed at least one run of your code before the checks.

*It does not need to get anymore complicated than that.

🌐
W3Schools
w3schools.com › java › java_while_loop_do.asp
Java Do/While Loop
Java Examples Java Videos Java Compiler Java Exercises Java Quiz Java Code Challenges Java Server Java Syllabus Java Study Plan Java Interview Q&A Java Certificate ... The do/while loop is a variant of the while loop. This loop will execute ...
People also ask

When should I use a while loop instead of a do-while loop?
Use a while loop when you want to check the condition before executing the loop body, potentially skipping it entirely if the condition is initially false.
🌐
shiksha.com
shiksha.com › home › it & software › it & software articles › programming articles › difference between while and do-while loop
Difference between While and Do-While Loop - Shiksha Online
In terms of performance, is there a significant difference between while and do-while loops?
Generally, there's no significant performance difference. The choice between them should be based on the logic required by your program rather than performance considerations.
🌐
shiksha.com
shiksha.com › home › it & software › it & software articles › programming articles › difference between while and do-while loop
Difference between While and Do-While Loop - Shiksha Online
What's the main advantage of a do-while loop over a while loop?
The main advantage of a do-while loop is that it guarantees at least one execution of the loop body, which is useful when you always need to perform an action before checking a condition.
🌐
shiksha.com
shiksha.com › home › it & software › it & software articles › programming articles › difference between while and do-while loop
Difference between While and Do-While Loop - Shiksha Online
🌐
Oracle
docs.oracle.com › javase › tutorial › java › nutsandbolts › while.html
The while and do-while Statements (The Java™ Tutorials > Learning the Java Language > Language Basics)
February 8, 2024 - You can implement an infinite loop ... be expressed as follows: ... The difference between do-while and while is that do-while evaluates its expression at the bottom of the loop instead of the top....
🌐
Shiksha
shiksha.com › home › it & software › it & software articles › programming articles › difference between while and do-while loop
Difference between While and Do-While Loop - Shiksha Online
September 16, 2024 - The most important difference between while and do-while loops is that While loops check the condition before executing the loop body, potentially skipping it entirely if the condition is initially false.
🌐
SitePoint
sitepoint.com › blog › java › java’s while and do-while loops in five minutes
Java's While and Do-While Loops in Five Minutes — SitePoint
November 5, 2024 - While and do-while loops in Java ... key difference between them is that in a while loop, the condition is checked before each iteration, whereas in a do-while loop, the condition is ......
🌐
GeeksforGeeks
geeksforgeeks.org › java › difference-between-while-and-do-while-loop-in-c-c-java
Difference between while and do-while loop in C, C++, Java - GeeksforGeeks
July 17, 2024 - import java.io.*; class GFG { public ... GFG GFG GFG · do-while loop: do while loop is similar to while loop with the only difference that it checks for the condition after executing the statements, and therefore is an example ...
🌐
TutorialsPoint
tutorialspoint.com › what-are-the-differences-between-while-loop-and-do-while-loop-in-java
What are the differences between while loop and do-while loop in Java?
... public class WhileLoop { public ... at all because the condition is FALSE for the first iteration. ... The do-while loop tests the loop continuation condition after the first iteration has completed....
Find elsewhere
🌐
Programiz
programiz.com › java-programming › do-while-loop
Java while and do...while Loop
Hence, the loop body will run for infinite times. The for loop is used when the number of iterations is known. For example, ... And while and do...while loops are generally used when the number of iterations is unknown. For example, ... Before we wrap up, let’s put your knowledge of Java while and do...while Loop to the test!
Top answer
1 of 6
2

In While Loop, before starting the loop block it checks whether the condition provided is satisfied (true) or not. Take this example,

while(false){
 System.out.println("While");
}

on the other hand in do while, the block will be executed once before checking the condition in while. Take this as example,

do{
 System.out.println("Do While");
}while(false);

where the while loop will not print anything but the do while will print Do While once before checking the condition and terminating...

I hope it helps you understand that...

2 of 6
0

Here's an example that will hopefully make it very clear what the difference is between a do...while loop and a while loop.

In the code below, the safeToJump() function will return true or false (it always returns true in this simple example) to indicate whether it is "safe to jump". If it is not safe to jump, then you will die if you still do so. With that in mind, if you jump() before checking if it is safe to do so, then you are gambling with your life. On the other hand, if you check to make sure it's safe before jumping, then you're guaranteed to survive:

public static void main(String[] args) {
    do {
        jump();
    } while (safeToJump());

    while (safeToJump()) {
        jump();
    }
}

public static boolean safeToJump() {
    return true;
}

public static void jump() {   
    System.out.println("Jump!");
}

In main(), in the first do...while loop, we are running the body of the loop BEFORE we check the conditional statement within the while portion, so it's possible that we jumped while conditions were not safe. This is possibly a dangerous action that could result in death. Thus, a do...while loop will always run at least once before checking the conditional statement.

In the second loop, the while conditional statement is checked BEFORE the body, so the jump() command is only executed if true was returned and conditions were actually safe for a jump. With that in mind, if safeToJump() had returned false, then the body of the loop would not run at all. So, a while loop will run zero or more times.

See the difference? Use a do...while loop if you want the body of the loop to run BEFORE the conditional check. Conversely, use a while loop if you want the body of the loop to run AFTER the conditional check.

Top answer
1 of 12
7

If you want to read data from a network socket until a character sequence is found, you first need to read the data and then check the data for the escape sequence.

do
{ 
   // read data
} while ( /* data is not escape sequence */ );
2 of 12
7

The while statement continually executes a block of statements while a particular condition is true

while (expression) {
     statement(s)
}

do-while evaluates its expression at the bottom of the loop, and therefore, the statements within the do block are always executed at least once.

do {
     statement(s)
} while (expression);

Now will talk about functional difference,

while-loops consist of a conditional branch instructions such as if_icmpge or if_icmplt and a goto statement. The conditional instruction branches the execution to the instruction immediately after the loop and therefore terminates the loop if the condition is not met. The final instruction in the loop is a goto that branches the byte code back to the beginning of the loop ensuring the byte code keeps looping until the conditional branch is met.

A Do-while-loops are also very similar to for-loops and while-loops except that they do not require the goto instruction as the conditional branch is the last instruction and is be used to loop back to the beginning A do-while loop always runs the loop body at least once - it skips the initial condition check. Since it skips first check, one branch will be less and one less condition to be evaluated.

By using do-while you may gain performance if the expression/condition is complex, since it is ensured to loop atleast once. In that casedo-while could call for performance gain

Very Impressive findings here, http://blog.jamesdbloom.com/JavaCodeToByteCode_PartOne.html#while_loop

🌐
LabEx
labex.io › questions › what-is-the-difference-between-while-and-dowhile-loops-in-java-178552
What Is The Difference Between While And Do While Loops In Java | LabEx
April 9, 2025 - The loop will continue to execute ... between while and do-while loops is that the do-while loop will always execute the loop body at least once, even if the condition is false....
🌐
Coding Shuttle
codingshuttle.com › java-programming-handbook › java-while-and-do-while-loop
Java while and do while loop | Coding Shuttle
April 9, 2025 - The do-while loop is similar to the while loop, but it guarantees the loop body will execute at least once because the condition is checked after the loop executes. We explored the differences between these loops, infinite loops, and practical ...
🌐
Quora
quora.com › What’s-the-difference-between-a-while-loop-and-do-while-loop-in-Java
What’s the difference between a while loop and do while loop in Java? - Quora
June 13, 2025 - Answer (1 of 4): A do-while loop is an exit controlled loop which means that it exits at the end. A while loop is an entry controlled loop which means that the condition is tested at the beginning and as a consequence, the code inside the loop ...
🌐
Tutorial Gateway
tutorialgateway.org › difference-between-while-and-do-while-in-java
Difference between While and Do While in Java
April 5, 2025 - In this program, We are going to write the same example using Do while. Although the condition fails, the statement inside the loop is executed once. Because the do while loop condition is tested after the execution of the statements. We hope you understand the difference.
🌐
W3Schools
w3schools.com › java › java_while_loop.asp
Java While Loop
In the next chapter, you will learn about the do while loop, which always runs the code at least once before checking the condition. ... If you want to use W3Schools services as an educational institution, team or enterprise, send us an e-mail: sales@w3schools.com · If you want to report an error, or if you want to make a suggestion, send us an e-mail: help@w3schools.com · HTML Tutorial CSS Tutorial JavaScript Tutorial How To Tutorial SQL Tutorial Python Tutorial W3.CSS Tutorial Bootstrap Tutorial PHP Tutorial Java Tutorial C++ Tutorial jQuery Tutorial
🌐
Sololearn
sololearn.com › en › Discuss › 2065905 › which-is-better-while-loop-or-do-while-loop-why-
Which is better ? While loop or do while loop. Why ? | Sololearn: Learn to code for FREE!
July 23, 2025 - Printing a menu While makes sure ... because the do while is exit control loop its check the condition at the end of the loop and the while is entry control that's check the condition at the starting of the loop.....
🌐
Refreshjava
refreshjava.com › java › while-and-do-while-loop-in-java
while and do while loop in Java with Example - RefreshJava
December 25, 2019 - do while is also a looping statement ... true. It's very similar to while loop, the only difference is that in do while loop the statements inside do while block executed first then condition expression is tested....
🌐
BYJUS
byjus.com › gate › difference-between-while-and-do-while-loop-in-c-c-plus-plus-java
Difference Between while and do-while loop in C, C++, Java
July 25, 2024 - Here is a list of the differences present between while and do-while loop in C, C++, Java.
🌐
DigitalOcean
digitalocean.com › community › tutorials › java-do-while-loop
Java do while loop | DigitalOcean
November 21, 2015 - The do-while loop in Java is similar to while loop except that the condition is checked after the statements are executed, so do while loop guarantees the loop execution at least once.