Do...While' Vs. 'While'

do...while' vs. 'while'

If you always want the loop to execute at least once. It's not common, but I do use it from time to time. One case where you might want to use it is trying to access a resource that could require a retry, e.g.

do
{
try to access resource...
put up message box with retry option

} while (user says retry);

Difference in while and do-while loops

The do while loops are control flow statements, they execute a block of code at least once and then the iteration of loops depends on the condition which is checked at the bottom of the loop, They are best to use when you want at least once the loop to be executed, for ex

#include <stdio.h>

int main () {


int c = 50;

/* The do will be executed */
do {
printf("value of c: %d\n", c);
c = c + 1;
}while( c < 20 );//It will depend on the condition
printf("any string");
return 0;
}

Here is a Flow diagram of do while loop
Sample Image

When would a do-while loop be the better than a while-loop?

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 difference between while and do while C++?

The while loop is an entry control loop, i.e. it first checks the condition in the while(condition){ ...body... } and then executes the body of the loop and keep looping and repeating the procedure until the condition is false.

The do while loop is an exit control loop, i.e. it checks the condition in the do{...body...}while(condition) after the body of the loop has been executed (the body in the do while loop will always be executed at least once) and then loops through the body again until the condition is found to be false.

Hope this helps :)

For Example:
In case of while loop nothing gets printed in this situation as 1 is not less than 1, condition fails and loop exits

int n=1;
while(n<1)
cout << "This does not get printed" << endl;

Whereas in case of do while the statement gets printed as it doesn't know anything about the condition right now until it executes the body atleast once and then it stop because condition fails.

int n=1;
do
cout << "This one gets printed" << endl;
while(n<1);

Difference between while loop and do 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.

do-while and while comparison

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).

Comparing while vs do-while loops

The C language was designed to allow even very simple compilers to produce reasonably efficient machine code. While it would be possible to express while(condition) {block}; as if (condition) do {block} while(condition); or to express do {block} while(condition); as if (1) {block} do {block} while (condition);, simple compilers would often generate somewhat different code for these alternative ways of writing the same thing, different ways of writing the code would yield better results in different cases.

For example, a common way for compilers to process while(condition) {block}; is to treat it as equivalent to either:

loop:
if (!condition) goto exit;
{block};
goto loop;
exit:

or as

  goto end;
loop:
{block};
end:
if (condition) goto loop;

If one were to rewrite the code as if (condition) do {block} while(condition); the result would be equivalent to:

  if (!condition) goto exit;
loop:
{block};
if (condition) goto loop
exit:

The code executed by all of these is identical except for conditional branches that are taken (T), conditional branches that are not taken (N), and unconditional jumps (U). The costs of branches and jumps will vary between platforms, but one can determine how many of each kind of branch or jump will be required for each way of writing the code. In particular, for the three ways of writing the code, the counts would be:

                  --First--  --Second-- --Third--
T N U T N U T N U
Zero iterations: 1 0 0 0 1 1 1 0 0
One iteration: 1 1 1 1 1 1 0 2 0
Addl. iteration: 0 1 1 1 0 0 1 0 0

Note that the third approach requires the fewest branches and jumps for any number of iterations, but at the expense of duplicating the code required to test the condition. On systems where the combined cost of an unconditional branch and a conditional branch not taken would exceed the cost of a conditional branch taken, the first approach will be cheaper in cases where a loop executes zero times, the costs will be the same if a loop executes exactly once, and the second will be cheaper if the loop executes two or more times.

If it's known that a loop is always going to execute at least once, the duplicated condition test and a non-taken branch can be eliminated from the third approach, making it a very clear winner. It's also easier for compilers to generate code for than the second approach. Having a do {} while() construct in the language makes it easier for simple compilers to easily generate efficient code in such cases.

Performance difference between for, while and do while loop?

The overall semantics of all three iteration statements is the same once they have been compiled into binary code.

They just provide a different taste over the same thing. Performance depends on:

  • the complexity of the body of the loop
  • the complexity of calculating the end condition / next iteration step

Since they all provide both of them there is no performance difference per se in any of them. Unless you consider small irrelevant things that you shouldn't care in any case.

There could be some optimization tricks that can be done according to the kind of loop but you shouldn't rely on them, even because they could be compiler dependent, so meaningless from your point of view.

what is the difference between while and do-while loop in c?

while loop:

A while loop is a control flow statement that allows code to be executed repeatedly based on a given Boolean condition. The while loop can be thought of as a repeating if statement.

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 of Exit Control Loop.

for more info click here.

Why the huge time difference between while and do..while in JavaScript

EDIT: I HAVE AN ANSWER (TL;DR: SKIP TO THE END)

I've done some tests on my own.