In JavaScript the do while loop, as well as in other languages ​​where this construct exists, allows you to execute instructions at least once. If the condition is true then the cycle is repeated again, otherwise it goes to the next instruction.

The do-while loop is in fact a post-conditional construct.

JavaScript do while – syntax

The syntax of the do while is therefore this:



do {
   instructions;
} while (condition);

You can clearly have a single instruction to execute as well.

The semantics of the do-while construct are therefore this:

  1. The instruction (or more than one) is carried out;
  2. Then the condition is evaluated, which can be true or false.
  3. So if the condition is true you go back to point 1; otherwise you go to the next instruction.

JavaScript do while – example 1

As a first example I propose an exercise already developed with the while loop in the previous lesson.

Banner Pubblicitario

We display the numbers from 0 to 9 in ascending order.

For this example we always initialize a counter variable: c = 0.

Then, with the do while loop, we print the first statement, increment c by 1 and only then evaluate the condition. If the condition is true then the cycle will start again by executing the same instructions.

Note that if I had mistakenly entered the condition c> 10, however, the instructions within the do-while would have been executed at least once.

Here is the complete code:


var c = 0;
do {
   document.write(c + '');
   c++;
} while (c < 10);

JavaScript do while - example 2

In this second example we will use the do while loop to check the input. This is in fact one of the major cases in which the do while loop is used.

Check that a number entered is positive and request it if not.

Banner pubblicitario

Inside the loop we insert the instruction that is used to take an input data. Only then do we check if this value is negative. If this condition is true the cycle will be repeated.

So here is the complete code.


do {
  var a = prompt("Insert a number:");
} while (a<0);        
document.write(a);

JavaScript do while - input control on two input data

In this third example on the do while loop in JavaScript we take two numbers as input and check if both are positive, otherwise we require them.

The resolution of the exercise is similar to the previous one, but attention must be paid to the condition. In fact, it is necessary to test whether a or b are less than zero. One can mistakenly lead to use the logical operator and (&&), but in this case the program does not repeat the loop if one of the two numbers is negative.

Try to reflect on this!

Here is the complete code:


do {
   var a = prompt("Insert number a:");
   var b = prompt("Insert number b:");
} while ((a<0) || (b<0));
	   
document.write(a + '');
document.write(b);

Conclusion

In this lesson we have introduced the do while loop in JavaScript, in the next lessons I will propose many other examples.

Some useful links

JavaScript tutorial

JavaScript Calculator

Object in JavaScript

For in loop

Caesar cipher decoder

Slot Machine in JavaScript

Introduction to JavaScript language

Learn JavaScript – basic concepts

JavaScript variables and costants