The Loop statement in JS

Source: Internet
Author: User

JS in the Loop statement can be divided into three kinds: 1.while;2.do......while;3.for.

While the syntax for

while (exp) {
statements;
}

var a=1,b=0;         while (a<=1000) {            if (a%2==0) {                if (b%20==0)                {                     document.write ("<br>" +parseint (b/20+1) + " Row even ");                 }                 document.write (A + "+");                 b++;             }            a++;         }

where exp is a conditional judgment statement, the final result can be expressed as a Boolean value, if the result is true then the following {} statement, and then continue to judge Exp, until the result of exp is false, if the result of exp is false, then skip this loop statement, Execute the next code. It is important to note that the statement in {} must have code that affects the results of exp, otherwise the loop will continue to repeat and form a dead loop.

The Do......while statement is substantially the same as the while statement, except that the Do......while statement executes the statement first and then the condition is judged. Its syntax is

do {
statements;
}while (condition);
Also the above example, the result becomes

var a=1,b=0;        do{            if (a%2==0) {                if (b%20==0)                {                    document.write ("<br>" +parseint (b/20+1) + "line even");                }                document.write ("<u>" +a+ "</u>" + "+");                b++;                         }            a++;        } while (a<=1000)

The For loop appears to be more concise and effective than the two loops above.

for (variable = start value; variable <= end value; variable = variable + step value) {
Code to be executed
}
() in the three statement, the second is the judgment statement, only the result is true when the loop will be executed.

        for (Var a=1,b=0;a<=100;a++) {            if (a%2==0) {                if (b%20==0)                {                    document.write ("<br>" + parseint (b/20+1) + "line even");                }                document.write ("<u>" +a+ "</u>" + "+");}        }

In the loop, there are times when we need to jump out of the loop or a loop statement without execution, and then we use the break statement and the continue statement. Both of these statements exist in the Loop statement, which terminates the entire loop, which blocks the execution of a loop. Such as

var n=0,m=0;for (var i = 1; i < i++) {if (i = = 4) {    continue;} n++;} alert (n);//8 for (var i = 1; i <, i++) {if (i = = 4) {Break    ;} m++;} Alert (m);//3

The Loop statement in JS

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.