If statement
Syntax:
Copy codeThe Code is as follows:
If (condition ){
Statement1;
}
Else {
Statement2;
}
Iteration statement
1. do-while statement
Syntax:
Copy codeThe Code is as follows:
Do {
Statement
} While (expression );
2. while statement
Syntax:
Copy codeThe Code is as follows:
While (expression ){
Statement
}
3. for statement
Syntax:
Copy codeThe Code is as follows:
For (initialization; expression; post-loop-expression ){
Statement;
}
4. for-in statement
Syntax:
Copy codeThe Code is as follows:
For (property in expression ){
Statement
}
Example:
Use the above four methods to traverse the array:
Copy codeThe Code is as follows:
Var iArr = new Array (1, 2, 3, 4, 5 );
Var index = 0;
// Do-while
Do {
Console. log (iArr [index]);
} While (++ index <iArr. length );
// While
Index = 0;
While (index ++ <iArr. length ){
Console. log (iArr [index-1]);
}
//
For (index = 0; index <iArr. length; index ++ ){
Console. log (iArr [index]);
}
// For-in
For (x in iArr ){
Console. log (iArr [x]);
}
Tagged statements
You can use the following syntax to add labels to statements for future calls:
Label: statement
For example:
Start: var iCount = 10;
In this example, the tag start can be called by subsequent break statements or continue statements.
Break and continue statements
Both break and continue provide stricter control over the code execution in the loop. The break statement can immediately exit the loop, while the continue only exits the current loop and enters the next loop. Example:
Copy codeThe Code is as follows:
Var iNum = 0;
For (var I = 1; I <10; I ++ ){
If (I % 5 = 0 ){
Break;
}
INum ++;
}
Console. log (iNum); // 4
INum = 0;
For (var I = 1; I <10; I ++ ){
If (I % 5 = 0 ){
Continue;
}
INum ++;
}
Console. log (iNum); // 8
INum = 0;
Outer: // tag
For (var I = 0; I <10; I ++ ){
For (var j = 0; j <10; j ++ ){
If (I = 5 & j = 5 ){
Break outer;
}
INum ++;
}
}
Console. log (iNum); // 55
INum = 0;
Outer: // tag
For (var I = 0; I <10; I ++ ){
For (var j = 0; j <10; j ++ ){
If (I = 5 & j = 5 ){
Continue outer;
}
INum ++;
}
}
Console. log (iNum); // 95
With statement
The with statement is used to set the scope of code in a specific object. Its syntax is as follows:
Copy codeThe Code is as follows:
With (expression ){
Statement
}
Usage example:
Copy codeThe Code is as follows:
Var sMessage = "Hello World ";
With (sMessage ){
Console. log (toUpperCase (); // HELLO WORLD
}
Switch statement
The sister statement of the if statement is the switch statement. The switch syntax is as follows:
Copy codeThe Code is as follows:
Switch (expression ){
Case value1:
Statement
Break;
Case value2:
Statement
Break;
...
Case valueN:
Statement
Break;
Default:
Statement
}
The switch in ECMAScript can be used as a string, for example:
Copy codeThe Code is as follows:
Var sColor = "green ";
Switch (sColor ){
Case "red ":
Console. log ("# FF0000 ");
Break;
Case "green ":
Console. log ("#00FF00"); // #00FF00
Break;
Default:
Console. log ("# FFFFFF ");
}
Author: tianxingjian, self-improvement