JavaScript Syntax
Seven, circular statements
1.while
Grammar:
while (exp) { //statements;}
Description: while (variable <= end value) {code to be executed}
Example:
// output numbers from 1 to 100 var i=1; while (i<=100) { document.write (i); I+ +;}
2.do...while
Grammar:
Do { //statements;} while (condition);
Description: Do {code to be executed} while (variable <= end value)
Example:
// output numbers from 1 to 100 var i=1; Do { document.write (i); I+ +;} while (i<=100);
// output 1 to 100 digits, odd underlined var i=1; do { if (i%2!=0 <u> +i+ </u> else {document.write (i); } I ++;} while (i<=100);
// Output 1 to 100 numbers, even with the underlined var i=1; Do { if(i%2==0) { document.write ("<u>" +i+ "</u>"); } Else { document.write (i); } I+ +;} while (i<=100);
3.for Loop statement
Grammar:
for (initial; expression; post-loop-expression) { //statement;}
Description: for (variable = start value; variable <= end value; variable = variable + step value) {
Code to be executed
}
Example:
// output numbers from 1 to 100 var i=1; for (var i=1;i<=100;i++) { document.write (i);}
// an odd number of outputs from 1 to 100 var i=1; for (var i=1;i<=100;i++) { if(i%2!=0) { document.write (i);} }
// Output 1 to 100 even var i=1; for (var i=1;i<=100;i++) { if(i%2==0) { document.write (i);} }
4.break
The break statement can exit the loop immediately;
For example, there are 10,000 people in line to buy Joker Xue concert tickets, when the NO. 500 person to buy tickets to stop the ticket, then the people behind can not buy tickets. (Example unscientific, only for analogy, not to blame)
var n = 0; for (var i=1; I <= 10000; i++) { if(i >) { break; c14/>} N+ +;} alert (n);
5.continue
The continue statement simply exits the current loop;
For example, 10,000 people lined up to buy Joker Xue Concert tickets, of which the NO. 500 person found that he had forgotten the money, he withdrew from the queue, and the back of the people can still continue to line up to buy tickets. (Example unscientific, only for analogy, not to blame)
var n = 0; for (var i = 1; I <= 10000; i++) { if(i = =) { continue; c13/>} N+ +;} alert (n);
Summarize the IF condition statement, the switch statement, and the loop statement for the previous blog post
Program execution Flow:
① generally follow the top-down process
② if it is an if statement and the condition is not true, the code snippet does not execute
Code snippets that do not conform to case values in ③switch are generally not executed
The loop body in the ④while,for loop statement is automatically cycled as long as the condition is set.
JavaScript Basics Summary (iii)