JavaScript Do While

The JavaScript Do While will test the given condition at the end of the loop. So, the Do While loop executes the statements inside the code block at least once, even if the given condition Fails.

The While loop we discussed in our previous Js article tests the condition before entering the code block. If the condition is True, then only statements within this loop will be executed. Otherwise, statements will not execute at least once.

In some situations, it is necessary to perform some operations (execute some statements) first and then check for the condition. In these cases, we can go for the JavaScript Do While loop.

JavaScript Do While Loop Syntax

The syntax of the JavaScript Do While Loop is

do
{
  statement 1;
  statement 2;
    ………….
  statement n;
}
While (condition);

First, it will execute the statements inside this JavaScript do while loop. Then after reaching the end, it will check the condition inside the while. If the condition is True, then it will repeat the process. If the condition fails, then the iteration is terminated.

JavaScript Do While Loop Flow Chart

The flow chart of it is shown below.

Do While Loop Flow Chart

The flow chart sequence of a do while loop in JavaScript Programming Language is:

  1. First, we initialize our variables. Next, it will enter into the loop.
  2. It will execute the group of statements inside the loop.
  3. Next, we have to use JavaScript Increment and Decrement operators inside the do while loop to Increment and Decrement values.
  4. Now it will check the condition. If the condition is True, the statement inside it will execute again. It will continue the process as long as the condition is True.
  5. If the condition is False, it will exit from the loop

JavaScript Do While Loop Example

This program helps us to understand the JavaScript Do While Loop.

  1. First, we declared the variable number and initialized it to 6. And initialized the total variable to 0.
  2. Next, within this, the number has been added to the total.
  3. We used the ++ operator in the next line to increment the number value.
  4. After this line, the number was tested against the while condition. If the condition results are true, then it will repeat the process. Otherwise, it will exit from the loop.
  5. In the next line, we used one more statement to show that it comes from outside the JavaScript do while loop.
<!DOCTYPE html>
<html>
<head>
    <title> Example </title>
</head>

<body>
    <h1> JavaScriptDoWhile </h1>
<script>
    var number = 6, total=0;
    do
    {
        total = total + number;
        document.write("<br\>Number = " + number);
        document.write("<br\>Total Value is = " + total);
        number++;
    }while (number <= 10);
    
    document.write("<br\>Total Value from outside the Loop is = ", total);  
</script>
</body>
</html>
JavaScript Do While Loop 1

Let us enter a value greater than 10 to see what will happen. From the above screenshot, we entered the value 11, and it still displays the total as 11. Because after executing that code, it checked the while condition, and it failed, so it exited from the loop.

Number = 12
Total Value is = 12
Total Value from outside the Loop is = 12