JavaScript (Day 5) [flow control statement], javascript statement

Source: Internet
Author: User

JavaScript (Day 5) [flow control statement], javascript statement

The ECMA-262 specifies a set of flow control statements. A statement defines the main syntax in ECMAScript. A statement generally uses one or more keywords to complete a given task. Such as judgment, loop, and exit.

 

I.Statement Definition

 

In ECMAScript, all code is composed of statements. The statement indicates the process, limitation, and Convention in the execution process. It can be a single-line statement or a composite statement enclosed by a pair of braces "{}". In the syntax description, composite statements can be processed as a single row.

 

Statement type

 

Type

Child type

Syntax

Statement

Variable declaration statement

Var box = 100;

Label declaration statement

Expression statement

Variable Assignment Statement

Box = 100;

Function call statement

Box ();

Attribute Assignment Statement

Box. property = 100;

Method call statement

Box. method ();

Branch statement

Condition branch statement

If () {} else {}

Multiple branch statements

Switch () {case n :...};

 

 

 

 

Statement type (continued)

 

Type

Child type

Syntax

Loop statement

For

For (;;;){}

For... in

For (x in x ){}

While

While (){};

Do... while

Do {} while ();

Control Structure

Continue execution clause

Continue;

Terminal execution clause

Break;

Function return clause

Return;

Exception trigger clause

Throw;

Exception capture and handling

Try {} catch () {} finally {}

Others

Empty statement

;

With statement

With (){}

 

 

II.If statement

The if statement is a condition judgment statement. There are three formats:

 

Var box = 100;

If (box> 50) alert ('box greater than 50'); // a row of if Statements, after judgment, execute a statement

 

Var box = 100;

If (box> 50)

Alert ('box greater than 50'); // if statement in two rows. After judgment, a statement is also executed.

Alert ('no matter what, I can be executed! ');

 

Var box = 100;

If (box <50 ){

Alert ('box greater than 50 ');

Alert ('no matter what, I can be executed! '); // Contains a composite statement. After judgment, a composite statement is executed.

}

 

For expressions in the if statement brackets, ECMAScript automatically calls the Boolean () transformation function to convert the result of this expression into a Boolean value. If the value is true, run the following statement. Otherwise, the statement is not executed.

 

 

PS: if the expression in the brackets of the if statement is true, only the following statement is executed. if there are multiple statements, you must use a composite statement to include multiple statements.

PS2: We recommend that you use the first or third format, one-row if statement, or multiple-row if compound statement. In this way, there will be no confusion due to multiple statements.

PS3: Composite statements are commonly called code blocks.

 

Var box = 100;

If (box> 50 ){

Alert ('box greater than 50'); // The condition is true. Execute this code block.

} Else {

Alert ('box less than 50'); // The condition is false. Execute this code block.

}

 

3. if (condition expression) {statement;} else if (condition expression) {statement;}... else {statement ;}

Var box = 100;

If (box> = 100) {// if conditions are met, the following branches are not executed

Alert ('A ');

} Else if (box> = 90 ){

Alert ('B ');

} Else if (box> = 80 ){

Alert ('bing ');

} Else if (box> = 70 ){

Alert ('ding ');

} Else if (box> = 60 ){

Alert ('pass ');

} Else {// If none of the above conditions are met, the output fails.

Alert ('failed ');

}

 

III.Switch statement

The switch statement is used to compare multiple equal values.

Var box = 1;

Switch (box) {// used to determine multiple equal values of a box

Case 1:

Alert ('one ');

Break; // break; used to prevent statement penetration

Case 2:

Alert ('two ');

Break;

Case 3:

Alert ('Three ');

Break;

 

Default: // equivalent to the else in the if statement. Otherwise

Alert ('error ');

}

 

4.Do... while statement

The do... while statement is a loop statement that runs first and then judges later. That is to say, no matter whether the conditions are met, run the loop body at least once.

Var box = 1; // if it is 1, it is executed five times. If it is 10, it is executed once.

Do {

Alert (box );

Box ++;

} While (box <= 5); // run the command once before determining

 

 

5.While statement

A while statement is a loop statement that is first judged and then executed. That is to say, you can run the loop body only after the conditions are met.

Var box = 1; // if it is 1, it is executed five times. If it is 10, it is not executed.

While (box <= 5) {// first judge and then execute

Alert (box );

Box ++;

}

 

6.For statement

A for statement is also a loop statement that first judges and then runs. But it has the ability to execute code after initial variables and definitions of loops before execution.

For (var box = 1; box <= 5; box ++) {// step 1, declare the variable var box = 1;

Alert (box); // step 2, Judge box <= 5

} // Step 3, alert (box)

// Step 4: box ++

// Step 5: Start from step 2 until the value is false.

 

7.For... in statement

The for... in statement is a precise iterative statement that can be used to enumerate object attributes.

Var box = {// create an object

'Name': 'zhang san', // key-value pair. attribute name on the left and value on the right

'Age': 28,

'Height': 178

};

For (var p in box) {// lists all attributes of an object

Alert (p );

}

 

 

8.Break and continue statements

Break and continue statements are used to precisely control code execution in a loop. The break statement immediately exits the loop and forces the statement after the loop body to continue. The continue statement exits from the current loop and continues the subsequent loop.

For (var box = 1; box <= 10; box ++ ){

If (box = 5) break; // if box is 5, exit the loop.

Document. write (box );

Document. write ('<br/> ');

}

 

For (var box = 1; box <= 10; box ++ ){

If (box = 5) continue; // if box is 5, exit the current loop.

Document. write (box );

Document. write ('<br/> ');

}

 

 

9.With statement

The with statement is used to set the code scope to a specific object.

Var box = {// create an object

'Name': 'zhang san', // key-Value Pair

'Age': 28,

'Height': 178

};

 

Var n = box. name; // assign the value from the object to the variable

Var a = box. age;

Var h = box. height;

 

You can rewrite the assignment operation of the preceding three paragraphs:

With (box) {// box object name is omitted

Var n = name;

Var a = age;

Var h = height;

}

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.