After these days, JS also came to the interesting place, our goal is what? Lazy! So let's start with the Loop statement:
Looping statements
1.while
Grammar:
while (exp) {
statements;
}
Description
while (variable <= end value) {code to be executed}
// while output 1000 digits from 1 to 1000 var i =1 ; while (I<1001 + ' ' ' (I%10 ==0 " <br/> )//Here The IF statement controls the conditions for line breaks, the same as} i
++
We can also control only the output of odd books or even numbers:
//only to lose the odd numbervari =1; while(i<1001){ if(i%2!=0) {document.write (i+' ') } if(i% -==0) {document.write ('<br/>')} I++}
1.do...while
do {
statements;
}while (condition);
Do {code to be executed} while (variable <= end value)
var i=1; Do { document.write (i); if (i%=0) { document.write ('<br/>' ) ) } I+ +;} while (i<1001);
Ps:while and do...while The difference is not difficult to see, is to first judge or execute first.
3.for Loop statement
The For statement is the most commonly used loop statement, and the following is the basic syntax:
for (initial; expression; post-loop-expression) {
Statement
}
for (variable = start value; variable <= end value; variable = variable + step value) {
Code to be executed
}
for (var1; i <i++) { document.write (' '+i+'. jpg '/>');} // It is important to note that the address of the image is relative to the HTML, and the splicing function of the +
Break statements and Continue statements
The break statement can exit the loop immediately;
The continue statement simply exits the current loop;
//Break Statementvarn=o; for(varI=1;i<Ten; i++){ if(i>5){ Break; } N++;} alert (n);//Continue statementsvarn=0; for(var=1;i<Ten; i++){ if(i==4){ Continue; } N++;} Ducument.write (n);
PS: The execution process of the program is generally carried out according to the process from top to bottom, and if it is not, the code is not executed; the switch does not conform to the case, and the code snippet is generally not executed; The loop body in the FOR Loop statement automatically loops as long as the condition is set.
JS Essay 4