The cycle is a construction that allows you to repeat the execution of the programming code specified number of times. Every certain execution of the instructions inside the cycle’s body is called iteration. There are several types of cycles in JavaScript such as for, for…in, while and do…while.

The do…while cycle is somehow similar to a conditional operator - the block of code will be executed in case the specified condition is true. However unlike the if operator, where the condition is checked only once, the do…while cycle will check the condition multiple times until it returns the false value. 

Notes on using the do…while cycle

In contrast to for cycle, you don’t need to specify the exact amount of times the command needs to be executed when using do…while. The syntax of the do…while cycle is quite simple, the brackets are not obligatory if you have only one command to execute. 

do {
    // the body of your cycle
} while(condition);

As you can see the cycle begins with the do statement followed by the curly brackets. Inside the brackets you are supposed to specify the commands you need to execute. After that there is the while statement followed by parentheses where you need to write a condition. 

If it returns a boolean value true, the program will go back to the body of the cycle and perform the commands written there again. If the returned value is false, the cycle will stop executing. 

The main distinction of the do…while cycle is that the block of code will be always executed at least once even when the conditional statement returns false every time. That’s because the evaluation of the statement inside parentheses is performed only after the main body of the cycle is completed.

This feature can be illustrated with a simple example. Let’s define a variable and assign 0 to it, then increment it in the do…while cycle and set false as a conditional value.

x = 0;
do {
    x++;
    console.log(x);
} while (false);  // logs 1

This program will print 1 in the output because the operation of an increment was executed first and only after that the condition was checked. Since, the value of x was equal to 1 at the moment of checking, the cycle stopped working and incrementing did not happen again.   

Infinite cycles

An infinite cycle is a loop that will never stop working. If you accidentally create an infinite cycle, it can lead to a crash of your browser or computer, so you need to be careful and prevent such situations from happening. Make sure there will definitely be a moment when the conditional statement returns false

Below is a simple example of the infinite cycle. There is no way for the condition to be changed to false so the cycle will never stop working and will print the word “test” in the console an infinite number of times. 

do {
   console.log("test"); // logs "test" infinite number of times
} while (true);  

You can create a variable for iterating, increment it in the body of your cycle and then check its value in the while statement in order to avoid problems and print the word a certain amount of times.

Break statement

There is an alternate way to exit the loop at any time - using the break statement. For example, you want to create a program to calculate the sum of numbers entered by the user. The field for input will be shown to the user first. Then, if there is a number entered, the sum will be calculated. If the user left the input field empty, you can stop the cycle using break

After this statement the execution of the program will continue from the next command after the cycle, in this case it’s alert(). The following code illustrates the implementation of such a program.

let sum = 0;
do
 {
  let number = +prompt("Enter the number: ", '');
  if (!number) break;
  sum += number;
} while (true)

alert("The sum: " + sum );

Sometimes you may face situations when several cycles need to be executed at the same time. That is when nested loops become necessary. You can add do…while cycles inside another cycle of the same or different type and they will be performed sequentially. 

In order to not get confused with multiple nested loops you can give them specific names. Write the identifier before the do statement and follow it by a colon. You will be able to use this label along with the break or continue statements when needed to stop execution of a certain loop. The correct syntax for nested cycles and their labels is shown in the following code.

i =0;
j =0;

outer: do{
   inner: do{
       console.log("inner loop\n");
       j++;
       }  while(j<2)
console.log("outer loop\n");
i++;
}   while(i<2)

Conclusion

The automation of repetitive tasks is an essential part of programming. Cycles help make  programs more effective and code shorter. In practice, the do…while cycles are used a bit more rarely than the regular while loops because there are not so many situations when the command needs to be executed at least once. However, in some cases it’s still very convenient. For example, when you want to create a guessing game where the question is presented to the user and must be shown again if the answer is wrong.