JavaScript advanced programming Reading Notes (7) Statements in ECMAScript

Source: Internet
Author: User

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:

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 codeCode: 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

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.