DEV Community

Cover image for doWhile vs While
Sam
Sam

Posted on

doWhile vs While

I'm trying to find a use for doWhile that's unique to it.

I feel like you could just use While?

Many thanks

Sam

Top comments (2)

Collapse
 
stokesm profile image
Matthew Stokes •

For any case where you need the instructions within the loop to execute at least once, regardless of the while condition.

A common contrived example is continuously asking for valid user input. My condition to check if the input is valid would be within the while, however I need the request for input to execute at least once before checking the condition, so I use a do...while.

Collapse
 
samuelcrudge profile image
Sam •

Thank you! makes sense. :)