A do-while loop is similar to a while loop, but it will always execute at least once even if the condition is initially false. The loop will continue to execute until the condition becomes false.
<!DOCTYPE html>
<html>
<head>
<!-- <script src="main.js"></script> -->
</head>
<body>
<script>
var number = 0;
do {
document.write("Number atm: " + number + "<br>");
number = number + 1;
} while (number < 50);
</script>
</body>
</html>
This code initializes a variable called number
to 0, and then enters a do-while loop. The loop body consists of a block of code enclosed in curly braces that writes the current value of number
to the document and increments it by 1.
The loop will continue to execute as long as number
is less than 50.
The main difference between a do-while loop and a while loop is that a do-while loop guarantees that the loop body will be executed at least once, regardless of the initial condition.
No comments:
Post a Comment