JavaScript do while Complete Tutorial

In this section, we will learn what the do-while loop is and how to use it in JavaScript.

Before reading this section, we’re assuming you’re familiar with the `while` loop statement.

What is do while loop in JavaScript?

The `do while` loop is another variant of the `while` loop.

The main difference between the two is that:

  • In the `while` loop, first the condition of the loop will be checked and if the result of the condition is true, the body will be executed afterward.
  • In the `do while` loop, first the body of the loop will be executed for the first time without checking the condition of the loop. After the first execution, the condition will be checked to see if the result is true and the body can be executed again or not.

So in the `do while` loop, it is guaranteed that at least the body will be executed once.

do while loop Syntax:

do {
// code block to be executed
}
while (condition);

`do`: we use the keyword `do` and the pair of braces `{}` that come afterwards to define the body of the loop.

Again, this body is guaranteed to run at least once before checking the condition of the loop.

`while`: we use the `while` keyword and the parentheses that come afterwards to set the condition of the loop.

Notes:

  • As long as the condition is true, the body of the loop will run.
  • Also, the `while()` statement is set after the closing brace `}` of the `do` keyword.
  • We use the semicolon `;` after the `while()` statement.

Example: using do while in JavaScript

let i = 0;
 let arr = new Array(); 
 do{
     arr.push(i);
     i++;
 }while (i != 100);
 console.log(arr);

Output:

[0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 17, 18, 19, 20, 21, 22, 23, 24, 25, 26, 27, 28, 29, 30, 31, 32, 33, 34, 35, 36, 37, 38, 39, 40, 41, 42, 43, 44, 45, 46, 47, 48, 49, 50, 51, 52, 53, 54, 55, 56, 57, 58, 59, 60, 61, 62, 63, 64, 65, 66, 67, 68, 69, 70, 71, 72, 73, 74, 75, 76, 77, 78, 79, 80, 81, 82, 83, 84, 85, 86, 87, 88, 89, 90, 91, 92, 93, 94, 95, 96, 97, 98, 99]

The execution engine will iterate through the body of the loop as long as the value `i` is not equal to the value `100`.

Note: for each iteration, inside the body of the loop, we’ve increased the value of the variable `i` by one and stored it in the array `arr`.

In a later section, we’ll explain how the `push()` method works.

JavaScript do while and break statement

We can use the `break` statement in the body of a do-while loop to break that loop before it gets finished if it’s needed.

Note: the break statement is explained in greater details in the future lessons, but in short, it will break a loop and send the execution engine to run the statements after the body of the target loop.

Example: using break statement in JavaScript do while

var num = 0; 
do{
  num++; 
  if (num ==100){
    break;
  }
}while(num<100);
console.log(`The final value of the num variable is: ${num}`);

Output:

The final value of the num variable is: 100

As you can see, the condition of this loop is set to true, and that means the while loop should execute infinitely (because the condition is always true). But you can see that after just looping 100 times, the loop broke and the statement that was after this loop gets executed. This is because of the `break` statement that we’ve set in the `if` statement of this loop which triggered when the value of the `num` variable reached the value of the 100.

JavaScript do while and continue Statement

Inside the body of the do-while loop, we can use the `continue` statement as well.

This statement basically tells the loop to skip the current iteration and start the next one.

Note: the continue statement is covered in later sections.

Example: using continue statement in JavaScript

var num = 0; 
do{
  num++; 
  if (num <95){
    continue;
  }
  console.log(num);
}while(num<100);
console.log("End");

Output:

96

97

98

99

100

End

Here the execution of the `console.log()` method in the body of the while loop is skipped for the first 95 iteration because of the call to the `continue` statement.

Difference between do while and while

Again, the main difference between the two loops is that:

  • The while loop first checks the condition and then decides to run the body of the loop. This means there’s a chance that the body might not execute at all.
  • On the other hand, the do-while loop first executes the body of the loop and then checks the condition of the loop. So at least it is guaranteed that the body of the loop will be executed once.
Facebook
Twitter
Pinterest
LinkedIn

Top Technologies