While Loop: A while loop is a control flow statement that allows code to be executed repeatedly based on the given Boolean condition. The while loop can be thought of as a repeating if statement.
Syntax:
while (condition) {
// Statements
}Example: This example illustrates the use of while loop.
<!DOCTYPE html> <html>
<head>
<title>
JavaScript While loop
</title>
</head>
<body style="text-align:center;">
<div>
<h1>GeeksForGeeks</h1>
<h2>JavaScript While Loop</h2>
</div>
<p id="GFG"></p>
<!-- Script to use while loop -->
<script>
var print = "";
var val = 1;
while (val < 6) {
print += "Geeks " + val;
print+="<br>"
val+=1;
}
document.getElementById("GFG").innerHTML
= print;
</script>
</body>
</html>
|
Output:
Do-While loop: A do-while loop is a control flow statement that executes a block of code at least once, and then repeatedly executes the block or not depending on a given boolean condition at the end of the block.
Syntax:
do {
// Statements
}
while (condition);Example: This example illustrates the use of do-while loop.
<!DOCTYPE html> <html>
<head>
<title>
JavaScript While loop
</title>
</head>
<body style="text-align:center;">
<div>
<h1>GeeksForGeeks</h1>
<h2>JavaScript While Loop</h2>
</div>
<p id="GFG"></p>
<!-- Script to use do-while loop -->
<script>
var print = ""
var val = 0;
do {
print += "Geeks " + val;
print +="<br>";
val+=1;
}
while (val < 6);
document.getElementById("GFG").innerHTML
= print;
</script>
</body>
</html>
|
Output:
Comparison between while and do-while loop: The do-while loop executes the content of the loop once before checking the condition of the while loop. While the while loop will check the condition first before executing the content.
| While Loop | Do-While Loop |
|---|---|
| It is an entry condition looping structure. | It is an exit condition looping structure. |
| The number of iterations depends on the condition mentioned in the while block. | Irrespective of the condition mentioned in the do-while block, there will a minimum of 1 iteration. |
| The block control condition is available at the starting point of the loop. | The block control condition is available at the end point of the loop. |
Example: This example illustrates both while and do-while loop.
<!DOCTYPE html> <html>
<head>
<title>
JavaScript loop
</title>
</head>
<body style="text-align:center;">
<div>
<h1 style="color:green;">
GeeksForGeeks
</h1>
<h2>JavaScript Loop</h2>
</div>
<h3>While Loop</h3>
<p id="GFG"></p>
<!-- Script to use while loop -->
<script>
var text = "";
var i = 1;
while (i < 6) {
text += "Geeks " + i+"<br>";
i++;
}
document.getElementById("GFG").innerHTML
= text;
</script>
<h3>Do While Loop</h3>
<p id="GFG1"></p>
<!-- Script to use do-while loop -->
<script>
var text = ""
var i = 1;
do {
text += "Geeks " + i+"<br>";
i++;
}
while (i < 6);
document.getElementById("GFG1").innerHTML
= text;
</script>
</body>
</html>
|
Output:
Recommended Posts:
- while and do while Loop in Scala
- Which will be faster while(1) or while(2)?
- R - while loop
- Validation of file size while uploading using JavaScript / jQuery
- What’s the difference between “Array()” and “[]” while declaring a JavaScript array?
- File Type Validation while Uploading it using JavaScript
- How to solve the issue that arise while using performance.now() method in javaScript?
- Hide scroll bar, but while still being able to scroll using CSS
- How to change opacity while scrolling the page?
- How to avoid Compile Error while defining Variables
- How to trigger onchange event on input type=range while dragging in Firefox ?
- How to validate input field while focusout ?
- How to change Input characters to Upper Case while typing using CSS/jQuery ?
- Loops in R (for, while, repeat)
- Next.js | SWR (Stale While Revalidate) - Introduction
- Why <big> tag is not in HTML5 while <small> tag exists ?
- SASS @for and @while Rule
- How to auto suggest rich contents while searching in Google AMP ?
- JavaScript | For Loop
- How to use loop through an array in JavaScript?
If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to contribute@geeksforgeeks.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.
Please Improve this article if you find anything incorrect by clicking on the "Improve Article" button below.

