Updated March 17, 2023
Introduction to Do While Loop in JavaScript
Do while loop in any programming language is used as an exit controlled conditional loop, where the loop is executed at least once and until when the while condition does not return the true value as a result. For example, the syntax of the do while loop in javascript is ‘do { …. } while (condition), where the code snippet between ‘{‘ and ‘}’ will be executed once before reading the condition inside the ‘while.’ The loop will be continuously executed until the control reads the while condition, returns the value as false and that will end the do while loop processing.
Syntax
The syntax for Do while loop in JavaScript is as below:
do {
//code to be executed
} while (condition);
The above syntax clearly signifies that the set of statements placed in a do block will be executed once before the condition is satisfied. Thus, the statements are run without being tested for the condition. Once this block is run, then it will be tested as a normal while loop. To check this, we can set a variable to 0. This can be incremented inside the do statement and then set the condition to false.
Let us take an example as below:
//setup variable as 0
let a=0;
do{
a++;
console.log(a);
} while(false);
The output here would be 1. When the code executes, the code starts execution, and the loop will run once from 0 until the condition is not satisfied. The loop, when created, will run at least once even though the condition specified is not satisfied.
How does do while loop work in JavaScript?
- The do while loop is a variant of while loop, which executes a set of statements until the mentioned condition evaluates to false. In do while the difference that is found is that the set of statements in the loop are executed at least once even if the condition mentioned is not being satisfied. The main difference between the while and do while loop is that the condition is evaluated at the beginning of every iteration with the while loop.
- If the condition specified evaluates to false, then the loop which is being followed by this condition will never be executed. However, when do while comes into the picture, then the loop is executed at least once. Though the condition is not satisfied, it will be getting executed once. This is because the condition is specified at the end of the loop in the do while loop. Due to this, the conditions in the loop are executed once.
Do while Flow Diagram
Let us understand the working of this loop by means of a flow chart.
- The flowchart here explains the complete working of do while loop in JavaScript. The do while loop works similar to while loop, where there are a set of conditions that are to be executed until a condition is satisfied.
- Once the flow starts, the process box in the above diagram explains that the code will start executing. Once the code is executed, it will check if the condition is satisfied with not. This is shown in the decision box where the condition is assessed. If this condition is true, then the code is again executed. It will go back to the process box in the diagram and execute the given set of statements.
- If the given condition is false, then the code will stop executing, and the loop will exit. Here the main difference between while and do while is that even though the condition is not true, the process block’s statements will be executed once, even before the condition is assessed. The flow chart is also signifying the same. The loop will run continuously after that first execution if the condition is true and will exit if the condition is false.
Examples
Code:
<!DOCTYPE html>
<html>
<head>
<title> Do While Loop in JavaScript</title>
</head>
<body>
<h1> Do While </h1>
<script>
var num = 10, total=0;
do
{
total = total + num;
document.write("<br\>Number = " + num);
document.write("<br\>Total Value is = " + total);
num++;
}while (number < 15);
document.write("<br\>Total Value from outside the Loop is = ", total);
</script>
</body>
</html>
In the above code, we have declared a variable number that has a value initialized to 10. The total variable is initialized to 0. This variable will calculate the total while the loop runs. When the loop starts, the number is added to the total. The next step increments the value of the num variable by 1. The while condition is then tested, which is true, i.e. 10 < 15. The loop will run again as below:
0= 0 + 10
21= 10+11
33= 21+12
46= 33+13
60= 46+14
After the total reaches 60, the num will increment to 15. Here the condition becomes 15<15. This is not satisfactory. The do while loop exits as the condition are not satisfied.
Output:
Number = 10
Total Value is = 10
Number = 11
Total Value is = 21
Number = 12
Total Value is = 33
Number = 13
Total Value is = 46
Number 14
Total Value is = 60
Total Value from outside the loop is = 60
This is the way in which a do while loop works. The loop will keep executing until the condition is satisfied once the condition is not being satisfied, the loop exits and the statements which are being followed getting executed.
Conclusion – Do While Loop in JavaScript
The do while loop is similar to the while loop, where a given set of statements is executed. The difference here is that the do while loop executes completely even though the condition is not satisfied. The do while loop executes till the specified condition is true and exits as soon as the condition is not satisfied. To complete tasks that need to be performed in an iteration do while loop can be used. Hence in Javascript do while loop can be useful when iterative tasks are to be performed. Javascript supports this loop, and it can be used whenever required.
Recommended Articles
This is a guide to Do While Loop in JavaScript. Here we discuss the Syntax, Flowchart with Examples, and How does it work in JavaScript. You may also look at the following article to learn more –