Home  >  Article  >  Web Front-end  >  JavaScript Advanced Programming Reading Notes (7) Statements in ECMAScript_javascript skills

JavaScript Advanced Programming Reading Notes (7) Statements in ECMAScript_javascript skills

WBOY
WBOYOriginal
2016-05-16 17:55:531038browse

if statement
Syntax:

Copy code The code is as follows:

if(condition){
statement1;
}
else{
statement2;
}

Iteration statement
1. do-while statement
Syntax:
Copy code The code is as follows:

do{
statement
}while(expression) ;

2. while statement
Syntax:
Copy code The code is as follows:

while(expression){
statement
}

 3. for statement
Syntax:
Copy code The code is as follows:

for(initialization;expression;post-loop-expression){
statement;
}

4. for-in statement
Syntax:
Copy code The code is as follows:

for(property in expression){
statement
}

  Example:
Use the above four methods to traverse the array:
Copy code 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
for(index=0;indexconsole.log(iArr[index]);
}

//for- in
for(x in iArr){
console.log(iArr[x]);
}

Tagged statements
You can use the following syntax to give statements Add a label for later calls:

label:statement
For example:

start:var iCount=10;
In this example, the label start can be used later by a break statement or The continue statement calls

break statement and continue statement

Both break and continue provide tighter control over the code execution in the loop. The break statement can exit the loop immediately, while continue only exits the current loop and enters the next loop. Example:
Copy code 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://label
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; 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 within a specific object. Its syntax is as follows:


with(expression){
statement
}


Usage example:


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 the code The code is as follows:

switch(expression){
case value1:
statement
break;
case value2:
statement
break;
...
case valueN:
statement
break;
default:
statement
}

switch in ECMAScript can be used for strings, example:
Copy code 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: Tian Xingjian, self-improvement
Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn