JavaScript basic teaching While statement basics

Source: Internet
Author: User

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>

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.