What is the difference between a while loop and a do-while loop?

A while loop checks its condition before each iteration, so the body may run zero times if the condition starts false. A do-while loop runs the body first and checks the condition afterward, so the body always executes at least once.

Oracle’s Java tutorials put it clearly: do-while evaluates its expression at the bottom of the loop instead of the top, which is why statements inside the do block are guaranteed to run once. The same entry- vs exit-controlled distinction appears in C-family languages and JavaScript.

Source: The while and do-while Statements — Oracle Java Tutorials

Leave a Comment