//code to be executed
} while (condition);
The above syntax is clearly signifying that the set of statements that are placed in do block will be executed once before the condition is satisfied. 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
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.
- 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 while and do while loop is that with while loop the condition is evaluated at the beginning of every iteration.
- If the condition specified evaluates to false, then the loop which is being followed by this condition will never be executed. When do while comes into picture then the loop is executed at least once. Though the condition is not satisfied it will be getting executed once. This is because in do while loop the condition is specified at the end of the 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 the means of a flow chart.
4.5 (3,011 ratings)
- 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 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 statements in the process block 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.
<h1> Do While </h1>
var num = 10, total=0;
total = total + num;
document.write("<br\>Number = " + num);
document.write("<br\>Total Value is = " + total);
}while (number < 15);
document.write("<br\>Total Value from outside the Loop is = ", total);
In the above code, we have declared a variable number which 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
After the total reaches 60, the num will increment to 15. Here the condition becomes 15<15. This is not satisfied. The do while loop exits as the condition is not satisfied.
Number = 10
Total Value is = 10
Number = 11
Total Value is = 21
Number = 12
Total Value is = 33
Number = 13
Total Value is = 46
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.