3. do while Statement


          The do while statement is another looping JavaScript statement, similar to the while statement. The do while statement executes a statement or statements once, then repeats the execution as long as it evaluates the true value of a given conditional expression.

          The syntax is as follows for the do while statement:

          Far from the simpler while statement, the statements in a do while statement are always executed at least once, before the conditional expression is tested the first time.

          The following do while statement runs once before the count variable is evaluated by the conditional expression. Thus, a single line reads "The count equals 2." prints. The count variable is equal to 2 when the conditional count < 2 is evaluated. This allows a false value to return to the conditional expression, and the do while statement ends.

var count = 2;
do {
document.write("<p>The count is equal to " + count + ".</p>");
count++;
} while (count < 2);

          In the next example, the code prompts the user for a multiplier, and prints the multiples of 1 to 50 using a do while loop.

<script>
var number = 1;
var multiplier = prompt("Enter a multiplier: ");
       do {
document.writeln(number * multiplier);
        number++;
       } while (number <= 50);
</script>

Output: