Basic Javascript Tutorials: while statements and basic javascript tutorials

Source: Internet
Author: User

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>

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.