JavaScript do...while Loop
In JavaScript, a do...while loop executes a block of code at least once, and then repeatedly executes the block as long as a specified condition evaluates to true.
The do...while loop in JavaScript can be used to generate the cubes of numbers from 1 to 10.
Here's an example of generating the cubes of numbers from 1 to 10:
// Variable to store the starting number
var i = 1;
var cube = 0;
do {
cube = i * i * i;
document.write("<br>" + i + "<sup>3</sup> =" + cube);
i++;
} while (i <= 10);
Explanation of the example:
var i = 1;: Initializes the starting number.do { ... } while (i <= 10);: Executes the loop to calculate and display the cube of each number from 1 to 10.i++;: Increments the number.
All in one Example:
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>Do While loop</title>
</head>
<body>
<script>
var i=0; //Initialisation
do{
document.write("<br>"+i);
i++; //Incrementation or Decrementation
}while(i<=10); //Condition
</script>
</body>
</html>
Run Code