What is Javascript do while loop
In this article, we will see what is a javascript do-while loop with examples. Do-while is similar while loop, the difference between the while and do-while loop is, the while loop is the entry control loop whereas the do-while loop is the exit control loop. In the Entry control loop means the condition is checked at the beginning of the loop whereas in the exit control loop condition is checked at the end of the loop.
Do while loop
It is also called an Exit control loop. The browser executes the statements in a do-while loop at least once. And then repeatedly executes the statements in a do-while loop as long as the given condition expression is true.
Syntax:
Initialization;
do
{
statement;
increment/decrement;
}while(conditional expression); //condition is checked at the end of the loop
Example:
var i =1; //Initialization
do {
document.write("Hello World <br/>");
i++;
}while(i<=5);
Output:
Note: When you want to execute some set of statements at least once, whether the given conditional expression evaluates to true or false not matters, then use do-while loop.