Do While loop Executes a statement once, and then it repeats the execution of the loop until a condition expression becomes false.
The do while loop consist-
do {
statement
}
while (expression) ;
statement--> The statement to be executed if expression is true.
Expression --> It relates to Boolean ,it shows true or false. If expression is true, the loop is executed otherwiseit is terminated.
We can also use the break statement in the do while loop . It cause the program to exit from the loop.
Below is the example of the loop-
<!DOCTYPE html>
<html>
<body>
<p>Click the button</p>
<button onclick="myFunction()"> Click Here</button>
<p id="button"></p>
<script>
function myFunction() {
var text = ""
var i = 0;
do {
text += "<br> " + i;
i++;
}
while (i < 10) // it will run to loop from 0 to 10
document.getElementById("button").innerHTML = text;
}
</script>
</body>
</html>
0 Comment(s)