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.
Your example code is wrong:
This would be the actual form:
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).