Java Script


JavaScript Tutorial


Admission Enquiry Form


What is Do While loop in JavaScript ?

Do While loop is also called unknown loop.In JavaScript do while loop is used if number of iteration or repetition are not known.The JavaScript do while loop iterates the do while loop block for the infinite number of times like while loop.But, the do while loop will be executed at least once whether condition is true or false.The syntax of do while loop is given below.

Syntax of Do While loop

initialization;
do
{
//statements.
increment/decrement;
}while(condition);

Example of Nested For loop:

<html>
<head>
<title>While loop</title>
</head>
<body>
<script>
var n=1;
do
{
document.write(n+"<br>");
n++;
}while(n<=5);

</script>
</body>
</html>

Output:

1
2
3
4
5

In the above example,initially value of n=1. Then, control will come inside do block whether the condition is true or false .Iit will execute once if condition is false.But, if condition is true it will execute 5 times and give the above output.