Why use a "do while" loop?

Posted by Stanni on Stack Overflow See other posts from Stack Overflow or by Stanni
Published on 2010-06-09T06:14:13Z Indexed on 2010/06/09 6:22 UTC
Read the original article Hit count: 146

Filed under:
|
|

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"){
    document.write("ok");
}

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.

© Stack Overflow or respective owner

Related posts about while-loops

Related posts about benefits