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