Learn JavaScript

JavaScript do while Loop

JavaScript do-while Loop is used to serve the same purpose, as that of other loops in JavaScript. It is quite unlike the while loop. In do-while loop the test condition is checked at the end of the loop. That means, the execution of set of statements under do-while block will be done at least once, even if the test condition fails.

Standard Syntax of JavaScript do while Loop

do
{
Statement 1;
Statement 2;
Statement 3;
.
.
.
Statement n;
}while(test condition);

Explanation

Execution of Code Snippet Under do-while Loop: As the control reaches inside the do-while block, the code snippet inside the block will be executed completely. The control now comes out of the block and the test condition is checked (written inside while). If the test condition is found to be true, then the control passes again inside the do-while block. This process continues until the test condition is true. If the test condition fails, then the control comes out of the do-while block and rest of the program will be executed accordingly.

JavaScript do while Loop Compact Form

In order to understand the compact form of do while looping, consider the following two code snippets. Where you can easily observe that, the first code snippet is more compact than the second one.


Example 1

<html>
<body>
<h2>Program to understand the working of do-while loop</h2>
<h3>List of Favorite Fruits</h3>
<script>
var fruits = ["Papaya", "Banana", "Pomegranate", "Dragon Fruit", "Kiwi"];
var fl = "";
var i = 0;
do{
fl += fruits[i] + "<br>";
i++;
}while (i < 5)
document.write(fl);
</script>
</body>
</html>

Output

Program to understand the working of do while loop

List of Favorite Fruits

Papaya
Banana
Pomegranate
Dragon Fruit
Kiwi


Example 2

<html>
<body>
<h2>Program to understand the compactness of do-while loop</h2>
<h3>List of Favorite Fruits</h3>
<script>
var fruits = ["Papaya", "Banana", "Pomegranate", "Dragon Fruit", "Kiwi"];
var fl = "";
var i;
fl += fruits[0] + "<br>";
fl += fruits[1] + "<br>";
fl += fruits[2] + "<br>";
fl += fruits[3] + "<br>";
fl += fruits[4] + "<br>";
document.write(fl);
</script>
</body>
</html>

Output

Program to understand the compactness of do-while loop

List of Favorite Fruits

Papaya
Banana
Pomegranate
Dragon Fruit
Kiwi


Audience

This JavaScript tutorial is designed to help the beginners as well as the professional developers. For better understanding the language of the tutorial is kept simple and easy to understand.

Prerequisites

This tutorial demands to have the basic knowledge of the programming languages such as c or c++, HTML for the better understanding of the JavaScript programming language. The other basic requirement to learn JavaScript through this tutorial is the basic knowledge of computer and information technology so you can easily acquire a better understanding JavaScript programming.

Community Helping

In case you find any kind of error like typing error, spelling mistakes, incorrect grammar, incorrect information or any other mistakes. Please feel free to write to us at: akyadav@etutororg.com.