If statement
Syntax:
1 If(Condition ){
2Statement1;
3}
4 Else{
5Statement2;
6}
Iteration statement
1. Do-while statement
Syntax:
1 Do{
2Statement
3}While(Expression );
2. While statement
Syntax:
1 While(Expression ){
2Statement
3}
3. For statement
Syntax:
1 For(Initialization; expression; post-loop-expression ){
2Statement;
3}
4. For-in statement
Syntax:
1 For(PropertyInExpression ){
2Statement
3}
Example:
Use the above four methods to traverse the array:
1 VaR Iarr = New Array (1, 2, 3, 4, 5 );
2 VaR Index = 0;
3
4 // Do-while
5 Do {
6 Console. Log (iarr [Index]);
7 }While (++ Index <iarr. Length );
8
9 // While
10 Index = 0;
11 While (Index ++ <iarr. Length ){
12 Console. Log (iarr [index-1]);
13 }
14
15 // For
16 For (Index = 0; index <iarr. length; index ++ ){
17 Console. Log (iarr [Index]);
18 }
19
20 // For-in
21 For (XIn Iarr ){
22 Console. Log (iarr [x]);
23 }
Tagged statements
You can use the following syntax to add labels to statements for future calls:
1Label: Statement
For example:
1Start:VaRIcount = 10;
In this example, the tag start can be called by subsequent break statements or continue statements.
Break and continue statements
Both Break and continueCodeExecution provides stricter control. The break statement can immediately exit the loop, while the continue only exits the current loop and enters the next loop. Example:
1 VaR Inum = 0;
2 For ( VaR I = 1; I <10; I ++ ){
3 If (I % 5 = 0 ){
4 Break ;
5 }
6 Inum ++;
7 }
8 Console. Log (inum ); // 4
9
10 Inum = 0;
11 For ( VaR I = 1; I <10; I ++ ){
12 If (I % 5 = 0 ){
13 Continue ;
14 }
15 Inum ++;
16 }
17 Console. Log (inum ); // 8
18
19 Inum = 0;
20 Outer: // tag
21 For ( VaR I = 0; I <10; I ++ ){
22 For ( VaR J = 0; j <10; j ++ ){
23 If (I = 5 & J = 5 ){
24 Break Outer;
25 }
26 Inum ++;
27 }
28 }
29 Console. Log (inum ); // 55
30
31 Inum = 0;
32 Outer: // tag
33 For ( VaR I = 0; I <10; I ++ ){
34 For ( VaR J = 0; j <10; j ++ ){
35 If (I = 5 & J = 5 ){
36 Continue Outer;
37 }
38 Inum ++;
39 }
40 }
41 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:
1 With(Expression ){
2Statement
3}
Usage example:
1 VaRSmessage = "Hello World ";
2 With(Smessage ){
3Console. Log (touppercase ());//Hello World
4}
Switch statement
The sister statement of the IF statement is the switch statement. The switch syntax is as follows:
1 Switch (Expression ){
2 Case Value1:
3 Statement
4 Break ;
5 Case Value2:
6 Statement
7 Break ;
8 ...
9 Case Valuen:
10 Statement
11 Break ;
12 Default :
13 Statement
14 }
The switch in ecmascript can be used as a string, for example:
1 VaR Scolor = "green ";
2 Switch (Scolor ){
3 Case "Red ":
4 Console. Log ("# ff0000 ");
5 Break ;
6 Case "Green ":
7 Console. Log ("#00ff00 "); // #00ff00
8 Break ;
9 Default :
10 Console. Log ("# ffffff ");
11 }