JavaScript (fifth day)

Source: Internet
Author: User

ECMA-262 sets up a set of process control statements. A statement defines the primary syntax in ECMAScript, which typically consists of one or more keywords to accomplish a given task. such as: Judgment, circulation, exit and so on.

A definition of a statement

in the in ECMAScript, all the code is made up of statements. Statements indicate the process, qualification, and convention in the execution, which can be either a single line statement or a compound statement enclosed by a pair of curly braces "{}", in which the composite statement can be treated as a single-line statement in the syntax description.

Types of statements

Type

Sub-type

Grammar

Statement statement

Variable declaration statements

var box = 100;

Label declaration statement

An expression statement

Variable assignment statements

box = 100;

Function Call statement

Box ();

Property Assignment Statements

Box.property = 100;

Method Invocation Statement

Box.method ();

Branch statements

Conditional branching statements

if () {} else {}

Multiple branching statements

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

Types of statements ( continued )

Type

Sub-type

Grammar

Looping statements

For

for (;;;) {}

For ... in

for (x in x) {}

While

while () {};

Do ... while

Do {} while ();

Control structure

Continue to execute clause

Continue;

Terminal execution clauses

break;

function return clause

return;

Exception triggering clauses

throw;

Exception capture and processing

try {} catch () {} finally {}

Other

Empty statement

;

With statement

With () {}

Two if statement

The IF statement is a conditional judgment statement with a total of three formats:

    1. An if ( conditional expression ) statement ;

var box = 100;

if (Box >) alert (' box greater than '); //A line of if statement, after which a statement is executed

var box = 100;

if (Box > 50)

Alert (' box greater than '); // two-line if statement, and execute a statement after judgment

Alert (' anyway, I can be executed! ');

var box = 100;

if (Box < 50) {

Alert (' box greater than ');

Alert (' anyway, I can be executed! '); to execute a compound statement with a compound statement

}

for an expression in parentheses in the IF statement,ECMAScript automatically calls the boolean () transformation function to convert the result of the expression to a Boolean value. If the value is true, a subsequent statement is executed, otherwise it is not executed.

PS: If the expression in parentheses is true, only the following statement is executed, and if there are more than one statement, you must include multiple statements with a compound statement.

PS2: It is recommended to use the first or third format, the if statement for a row , or a multi-line if compound statement. This will not cause confusion because of multiple statements.

PS3: Compound statements We generally like to weigh as: blocks of code.

    1. if ( conditional expression ) { statement ;} else { statement ;}

var box = 100;

if (Box > 50) {

Alert (' box greater than '); // condition is trueto execute this block of code

} else {

Alert (' box is less than '); // condition is false, execute this block of code

}

3.if ( conditional expression ) { statement ;} else if ( conditional expression ) { statement ;} ... else { /c7> statement ;}

var box = 100;

if (box >=) {// if the condition is met, any of the following branches will not be executed

Alert (' a ');

} else if (box >= 90) {

Alert (' b ');

} else if (box >= 80) {

Alert (' C ');

} else if (box >= 70) {

Alert (' d ');

} else if (box >= 60) {

Alert (' pass ');

} else {// if none of the above is satisfied, the output is less than the grid

Alert (' less than lattice ');

}

Three Switch statement

A switch statement is a multiple-condition judgment used to compare multiple values for equality.

var box = 1;

Switch (box) {// to determine Multiple values for box equality

Case 1:

Alert (' one ');

Break Break used to prevent the statement from penetrating

Case 2:

Alert (' both ');

Break

Case 3:

Alert (' three ');

Break

Default:// equivalent to the elsein an if statement , otherwise meaning

Alert (' Error ');

}

Four do...while Statements

The Do...while statement is a looping statement that runs first and then judges. In other words, the loop body is run at least once, regardless of whether the condition is satisfied.

var box = 1; if it is 1, execute five times, if it is ten, execute 1 times

do {

alert (box);

box++;

} while (box <= 5); Run first, then Judge

Five while statement

The while statement is a loop statement that is first judged and then runs. That is, the condition must be fulfilled before the loop body can be run.

var box = 1; if it is 1, execute five times, if it is ten, do not execute

while (box <= 5) {// first judge, then execute

alert (box);

box++;

}

Six for statement

The For statement is also a loop statement that is first judged and then run. However, it has the ability to execute code before the loop executes the initial variables and defines the loop.

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

alert (box); Step Two, Judge box <=5

}// third step,alert (box)

Fourth Step,box++

fifth step, from the second step, until the judgment is false .

Seven for...in Statements

The for...in statement is a precise iterative statement that can be used to enumerate the properties of an object.

var box = {// Create an object

' Name ': ' Zhang San ', // key value pair, left is the property name, the right is the value

' Age ': 28,

' Height ': 178

};

For (var p in box) {// enumerate all properties of an object

Alert (p);

}

Eight break and continue statements

The break and continue statements are used to precisely control the execution of code in a loop. where the break statement exits the loop immediately, forcing the statement following the loop body to continue executing. While The Continue statement exits the current loop, the loop continues behind.

for (var box = 1; box <=; box++) {

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

document.write (box);

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

}

for (var box = 1; box <=; box++) {

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

document.write (box);

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

}

Nine with statement

the function of the WITH statement is to set the scope of the code to a specific object.

var box = {// Create an object

' Name ': ' Zhang San ', // Key-value pair

' Age ': 28,

' Height ': 178

};

var n = box.name; assigning values from objects to variables

var a = Box.age;

var h = box.height;

The above three-segment assignment can be rewritten as:

With (box) {// omitted box object name

var n = name;

var a = age;

var h = height;

}

JavaScript (fifth day)

Related Article

E-Commerce Solutions

Leverage the same tools powering the Alibaba Ecosystem

Learn more >

Apsara Conference 2019

The Rise of Data Intelligence, September 25th - 27th, Hangzhou, China

Learn more >

Alibaba Cloud Free Trial

Learn and experience the power of Alibaba Cloud with a free trial worth $300-1200 USD

Learn more >

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.