JavaScript – loop with do-while postcondition

In JavaScript, in addition to the while and for loops, there is another variant of the do-while loop:


The meaning of this construct is that what is inside the curly braces is executed first, and then the while() condition is checked. If the condition is true, then the loop is repeated, if false, then the loop ends. Thus, the loop will be executed at least once. This loop is used less frequently than the previous two because it is easier for a human to understand a regular while loop to read the condition, rather than looking for the condition at the end of the loop.

What happens if the loop execution condition is false in this loop? With the do-while syntax, the loop will go through 1 iteration, because The condition is checked after the loop body has passed. If a similar situation happened in the while loop, then not a single iteration would be performed.

This entry was posted in Javascript (en). Bookmark the permalink.

Leave a Reply

🇬🇧 Attention! Comments with URLs/email are not allowed.
🇷🇺 Комментарии со ссылками/email удаляются автоматически.