16

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.

1

9 Answers 9

19
  • 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.

3 Comments

Enter at least 15 characters
simple, put it in a function :D
Enter at least 15 characters
Enter at least 15 characters
@NielsLucas True, but you also want to avoid having lots of small functions littered all over the place. But I agree that the advantage given by a do while is not big.
Enter at least 15 characters
Enter at least 15 characters
@Anakhand dont you use objects....? Objects (what ever kind/design pattern you use) should contain functions that u re-use that are relevent to the object. So I agree with "avoid having lots of small functions littered all over the place" but I do hope people use objects these days.
Enter at least 15 characters
Enter at least 15 characters
Enter at least 15 characters
15

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

1 Comment

Enter at least 15 characters
I see now, I didn't realise that it saves you form having to duplicate the inner code. Thanks for the help.
Enter at least 15 characters
Enter at least 15 characters
Enter at least 15 characters
9

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

2 Comments

Enter at least 15 characters
What languages have do-while but no procedures?
Enter at least 15 characters
Enter at least 15 characters
You could probably put the 100,000 lines of code into a function...but point taken.
Enter at least 15 characters
Enter at least 15 characters
Enter at least 15 characters
5

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

Comments

Enter at least 15 characters
Enter at least 15 characters
3

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.

Comments

Enter at least 15 characters
Enter at least 15 characters
2

Although

do { 
    s1;
    ...
    sn;
} while (p);

could be written as

boolean done = false;
while (!done) {
    s1;
    ...
    sn;
    done = !p;
}

the former is more comprehensible.

1 Comment

Enter at least 15 characters
This is one of the few cases where I kind of miss not having a do while in Python. (Though p still has to be declared outside of the do scope in most languages that have it, right?)
Enter at least 15 characters
Enter at least 15 characters
Enter at least 15 characters
1

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.

Comments

Enter at least 15 characters
Enter at least 15 characters
1

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.

Comments

Enter at least 15 characters
Enter at least 15 characters
1

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.

Comments

Enter at least 15 characters
Enter at least 15 characters

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.