The do...while statement is a loop executing kind of a statement used to post-test flow conditions.

The do...while loop statement is called a post-testing because the expression is evaluated after the loop has been executed once already; therefore regardless to our escape value the loop will run at least once. The example shows how the syntax looks like:

do {

   statement;

}

while (expression);

Or with the real numbers:

var people_count = 4;

var i = 1;

do {

   alert("We counted :" + i + " people.");

   i++;

}

while (i <= people_count);

In the example above, the variables (counter, limit) must be initialized before the loop tests for the expression. As it was expected that there was at least one person present, the loop started with one (var i = 1) and ran until it reached the count limit (people_count).

Example

The example with do-while statement in JavaScript:

 

›› go to examples ››