JavaScript advanced programming Reading Notes (7) Statements in ECMAScript _ javascript skills

Source: Internet
Author: User
The statements in ECMAScript. if you want to learn JavaScript, you can refer to the if statement.
Syntax:

The Code is as follows:


If (condition ){
Statement1;
}
Else {
Statement2;
}


Iteration statement
1. do-while statement
Syntax:

The Code is as follows:


Do {
Statement
} While (expression );


2. while statement
Syntax:

The Code is as follows:


While (expression ){
Statement
}


3. for statement
Syntax:

The Code is as follows:


For (initialization; expression; post-loop-expression ){
Statement;
}


4. for-in statement
Syntax:

The Code is as follows:


For (property in expression ){
Statement
}


Example:
Use the above four methods to traverse the array:

The 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
// While
Index = 0;
While (index ++ Console. log (iArr [index-1]);
}

//
For (index = 0; 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:

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

The Code is as follows:


With (expression ){
Statement
}


Usage example:

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

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

The 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.