Do…While Loop

The do…while statement creates a loop that executes a specified statement until the test condition evaluates to be false. The condition is evaluated after executing the statement.
Syntax for do… while is

  1. do{
  2. // statement
  3. }
  4. while(expression) ;

Lets for example see how to print numbers less than 10 using do...while loop:

  1. var i = 0;
  2. do {
  3. document.write(i + " ");
  4. i++; // incrementing i by 1
  5. } while (i < 10);

Note: i = i + 1 can be written i++.

{% exercise %}
Using a do…while-loop, print numbers between less than 5.
{% initial %}
var i = 0;
{% solution %}
var i = 0;
do {
i++; // incrementing i by 1
} while (i < 5);
{% endexercise %}