Basic Javascript Tutorials: while statements and basic javascript tutorials
Loop statements are used to repeatedly execute the same piece of code. Although they are divided into several different types, their principles are almost the same. As long as the given conditions meet, the statements contained in the loop are executed continuously. once the conditions are no longer met, the statements are terminated.
The while loop is a pre-test loop, which means that the condition judgment for termination is prior to code execution. Therefore, the subject of the loop may not be executed at all. The syntax is as follows:
While (expression) statement
When expression is true, the program continues to execute the statement until expression is false.
Two cases
Copy codeThe Code is as follows:
<Script type = "text/javascript">
Var I = iSum = 0;
While (I <= 100 ){
ISum + = I;
I ++;
};
Document. write (iSum + "<br>"); // calculates the addition of numbers within 100.
</Script>
<P> click the button below to keep repeating the code block as long as I is smaller 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>