The function of a circular statement is to execute the same piece of code repeatedly, albeit with several different types, but with almost the same principle, and as long as the given condition is satisfied, the statements contained within the loop are executed continuously and terminated once the condition is no longer satisfied.
The while loop is a pre-test loop, which means that the condition that terminates is judged before the code is executed, so the body of the loop may not be executed at all. The syntax is as follows:
while (expression) statement
When expression is ture, the program executes the statement statement continuously until expression is false.
Two cases
Copy Code code as follows:
<script type= "Text/javascript" >
var i=isum=0;
while (I <= 100) {
Isum = i;
i++;
};
document.write (isum+ "<br>"); The sum of the numbers in 100 is calculated
</script>
<p> Click on the button below to loop the code block as long as I is less than 5. </p>
<button onclick= "MyFunction ()" > click here </button>
<p id= "Demo" ></p>
<script>
function MyFunction ()
{
var x= "", i=0;
while (i<=10)
{
X=x + "The number is" + i + "<br>";
i++;
}
document.getElementById ("Demo"). Innerhtml=x;
}
</script>