views:

254

answers:

8

Hi,

I've never understood why using a do while loops is necessary. I understand what they do, Which is to execute the code that the while loop contains without checking if the condition is true first.

But isn't the below code:

do{
    document.write("ok");
}
while(x == "10");

The exact same as:

document.write("ok");
while(x == "10"){
    document.write("ok");
}

Maybe I'm being very stupid and missing something obvious out but I don't see the benefit of using do while over my above example.

+3  A: 

do while will run at least once - letting you do, while... where as while requires the condition met to start at all..

var i = 6;
do{
    alert(i);
}while( i < 5)

// alerts 6, but while criteria isn't met so it stops

var i = 6;
while( i < 5) {
  alert(i);
}

// no output - while condition wasn't met
Dan Heberden
+5  A: 

Your example code is wrong:

do{
    document.write("ok");
}while(x == "10"){
    document.write("ok");
}

This would be the actual form:

do {
    document.write("ok");
} while(x == "10");

You are correct that it executes the inner code block before checking the condition, but it doesn't need to duplicate the inner code block the way you have it. The do/while construct is (as you've already stated) a way to make sure that a piece of code is executed 1 or more times instead of 0 or more times (depending on the conditional).

inkedmn
I see now, I didn't realise that it saves you form having to duplicate the inner code. Thanks for the help.
Stanni
+2  A: 

The difference is most important when you have more than one line of code to be executed at least once. Simulating a do loop with a while loop would result in substantial code duplication, which is always bad. Granted, a better solution is to refactor that code into a method and then simply have the same method call before and in the loop, but many people dislike even that much duplication, and using the do keyword declares unambiguously "this is a foot-controlled loop".

So, basically, readability and basic SE principles.

Kilian Foth
A: 

What you're missing is that your use of do...while above is 100% WRONG!

A do...while loop in Java, for example, looks something like this:

do {
  //something you want to execute at least once
} while (someBooleanCondition);

See that complete lack of a second block there? See how the while ends the statement completely? So what happens now is that the code in the {...} pair between do and while will get executed, then someBooleanCondition will get tested and if it's true, the code block will get executed again. And again. And again. Until someBooleanCondition gets tested false.

And at this point, perhaps, you'll understand why we have both forms of loop. The above code could be translated to:

//something you want to execute at least once
while (someBooleanCondition) {
  //something you want to execute at least once
}

This code, however, requires you to type the same (potentially large and complicated) code twice, leaving the door open for more errors. Hence the need for a bottom-test loop instead of a top-test loop.

JUST MY correct OPINION
+1  A: 

First of all your do..while syntax is wrong. It is like this:

do
{
  document.write("ok");
}while(x=="10");

It is useful when you want to execute the body of the loop at least once without evaluating its teminating condition. For example, lets say you want to write a loop where you are prompting the user for input and depending on input execute some code. You would want this to execute at least once and then ask the user whether he wants continue. In such cases, do..while loop results in lesser and cleaner code compared to a while loop.

Naveen
+9  A: 
  • As you see you had to repeat the same line in the second example. When you maintain, you most likely want those two lines to be the same, but you have repeated yourself.

  • Now Imagine that was a big block and not a line, not only will it take unnecessary precious visual space, but it will also be harder to maintain and attract inconsistencies.

Bakkal
A: 

Its a better way of writing code:

int count = ReadDataFromStream();
while(count != 0)
{
    count = ReadDataFromStream();
}

Can be written using do-while as:

int count = 0;
do
{
    count = ReadDataFromStream();
} while(count != 0);

There are better examples of do-while but I could not recall at this time.

A9S6
A: 

What about:

do{

   //.....
   // 100000 lines of code!
   //.....

} while(i%10);




Of course you will not write that:

//.....
// 100000 lines of code!
//.....

while(i%10){

   //.....
   // 100000 lines of code!
   //.....

} 



And you will then be forced to use a do-while loop
God Bless it!!

Edit:
Or you will use procedures..

Betamoo
What languages have do-while but no procedures?
Pete Kirkham