JavaScript Do While

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

The While loop that we discussed in our previous Js article test the condition before entering into 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.

There are some situations where it is necessary to perform some operations (execute some statements) first and then check for the condition. In these cases, we can go for 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 Do While Loop terminated.

JavaScript Do While Loop Flow Chart

The flow chart of JavaScript Do While is as shown below

JavaScript Do While Flow Chart

Flow chart sequence of a JavaScript do while loop is:

  1. First, we initialize our variables. Next, it will enter into the Do While loop in JavaScript.
  2. It will execute the group of statements inside the loop.
  3. Next, we have to use JavaScript Increment and Decrement operators inside the loop to Increment and Decrement value.
  4. Now it will check the condition. If the condition is True, then the statement inside the JavaScript Do while loop 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 Do While Loop in JavaScript Programming Language

  1. First we declared the variable number and initialized to 6. And, initialized the total variable to 0.
  2. Next, within the Js do while loop, the number has added to the total.
  3. In the next line, we used ++ operator to increment the number value.
  4. After this line, the number has tested against the while condition. If the condition results 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 is coming from outside the JavaScript do while loop.
<!DOCTYPE html>
<html>
<head>
    <title> JavaScript Do While Loop </title>
</head>

<body>
    <h1> JavaScript Do While </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?. If you observe from the above do while loop screenshot, we entered 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 exit from the loop.

JavaScript Do While

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