4. Process Control Statements

Source: Internet
Author: User

Process Control Statements

Learning Essentials:
1. Definition of statements
2.if statements
3.switch statements
4.do...while statements
5.while statements
6.for statements
7.for...in statements
8.break and Continue statements
9.whit statements

ECMAScript-262 sets up a set of process control statements. Statements define the syntax in ECMAScript, and statements are usually made by one or more
Key word to complete a given task. such as: Judgment, circulation, exit and so on.

Definition of a statement

In ECMAScript, all the code is composed of statements. Statements indicate the process, qualification, and convention in the execution process, and can be formally
A single-line statement, or a compound statement enclosed by a pair of curly braces "()", where the compound statement as a whole can be used as a single-line statement in the syntax description
Acting

Types of statements
Type subtype Syntax
Declaration statement Variable declaration statement var box = 100;
Label declaration statement Label:box;

Expression statement variable assignment statement box = 100;
Function call statement box ();
Attribute Assignment Statement box.property = 100;
Method invocation statement Box.method ();

Branch statement Condition Branch statement if{} else{}
Multi-branch Statement Swich () {Case N: ...}


Second, if statement

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

1.if (conditional expression) statement;
var box = 100;
if (box>50) alert (' box is greater than 50 ');//A line of if statement that executes a statement after judgment

var box = 100;
if (box>50)//if inside the parentheses (box>50) Returns the result of the turn to a Boolean value of True
Alert (' box is greater than 50 ');//A two-line if statement that evaluates to execute after a statement, if the expression in the IF statement results in a return
is false, only the following statement is not executed
Alert (' If True or FALSE, I will execute ');


var box = 10;
if (box>50) {
Alert (' box greater than 50 ');
Alert (' If True or FALSE, I will execute ');
}//here is a compound statement, treated as a statement, the function of the code block is reflected
If followed by another compound statement, the following compound statement is executed

var box = 100;
if ("Lee") {//() inside if 123 will automatically turn into true,0 converted to false, ' Lee ' is converted to true if NULL is not executed
alert (box);
}

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

Ps1:if statement in parentheses if true, only the following statement is executed, and if there are multiple statements, you must use the compound language
Sentence to include multiple statements.

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 multiple statements
Cause confusion.

PS3: Compound statements We generally like to call: code blocks.


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


var box = 100;
if (Box > 50) {
Alert (' box is greater than 50 ');//condition is true to execute this block of code
}else{
Alert (' box is less than 50 ');//condition is false, execute this block of code
}


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

var box = 100;
if (box >=100) {//Do not perform any of the following branches if the conditions are met
Alert (' a ');
}else if (box>=90) {
Alert (' B ');
}else if (box>=80) {
Alert (' C ');
}else{
Alert (' d ');
}


Third, switch statement

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

var box = 1;
Swich (box) {//For determining Multiple values of box equality
Case 1://CASE1: Equivalent to the IF statement (box==1), if box is 1
Alert (' one ');
Break;//break; To prevent the penetration of a statement
Case 2:
Alert (' both ');
Break
Case 3:
Alert (' three ');
Break
default;//equivalent to the else in an if statement, otherwise the meaning
Alert (' Error ');
}


Iv. 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;
do{//if it is 1, execute five times first, if 10, perform 1 times
alert (box);
box++;
}while (box<=5);//First run once, in judging

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 first, if 10, do not execute
while (box<=5) {//First judge, then execute, not execute when condition is not satisfied
alert (box);
box++;
}


VI. For statement

The For statement is also a circular statement that is first judged and then executed. However, it has the ability to execute the 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);//Second step: Judging box<=5
}//Step three: alert (box)
Fourth Step: box++
Step fifth: Come back from the second step


Vii. 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 This object has three properties Name,age,height
' Name ': ' Journey ',//key-value pair, left is the property name, the right is the value
' Age ': 23,
' Height ': 175
};
For (var p in box) {//Name,age,height all properties of the object are assigned to box
Alert (p);
}


Viii. 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
The statement following the line loop body. While the continue statement exits the current loop, the loop continues behind.

for (var box=1; box <=; box + +) {
If box ==5 break;//exit the current loop and the back loop if it is 5
document.write (box);
document.write (' <br/> ');
}

for (var box=1;box <=; box++) {
if (box==5) continue;//if box is 5, exit the current loop and continue to the back loop
document.write (box);
document.write (' <br/> ')
}


Nine, with statements

The function of the WITH statement is to set the scope of the code to a specific object.
var box={//create an Object
' Name ': ' Journey ',//key-value pair
' Age ': 23,
' Height ': 175
};

var n = box.name;//Assigns a value from an object to a variable
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;
}

4. Process Control Statements

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.