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).
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).
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.
Videos
When should I use a while loop instead of a do-while loop?
In terms of performance, is there a significant difference between while and do-while loops?
What's the main advantage of a do-while loop over a while loop?
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...
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.
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 */ );
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
The do while loop executes the content of the loop once before checking the condition of the while.
Whereas a while loop will check the condition first before executing the content.
In this case you are waiting for user input with scanf(), which will never execute in the while loop as wdlen is not initialized and may just contain a garbage value which may be greater than 2.
While : your condition is at the begin of the loop block, and makes possible to never enter the loop.
Do While : your condition is at the end of the loop block, and makes obligatory to enter the loop at least one time.