Original: Learn JavaScript from the beginning (vi)--statement
One, Conditional branch statement: if
Basic format:
if (< expression 1>) {
< statement Group 1>
}else if (< expression 2>) {
< statement Group 2>
}else{
< statement Group 3>
}
Execution process:
Second, the circular statement
2.1 Pre-Test loop statement: The exit condition is evaluated before the code in the loop body is executed.
2.1.1 While statement
Basic format:
do {
< statement groups >
} while (< expression >)
Execution process:
2.1.2 For statement
Basic format:
for (< initial expression >;< conditional expression >;< change Amount expression >) {
< statement groups >
}
Execution process:
2.2 Post-Test loop statement: The export condition is not tested until the code in the loop body is executed.
2.2.1, do-while statements
Basic format:
do {
< statement groups >
} while (< expression >);
Execution process:
Three, accurate iteration statement: for-in
Basic format:
For (attribute in object) {
< statement groups >
}
Function: Executes all properties of the specified object repeatedly, and can be used to enumerate the properties of the object.
Example:
MyFunction () { var Span style= "color: #000000;" > X; var txt= "; var person={fname: "Bill", lname: "Gates", age:56 }; for (x in person) {txt =txt + person[x];} document.getElementById ( "Demo"). Innerhtml=txt;} </script></body>
If the object to be represented is null or undefined, the loop body will no longer execute, or throw an error, so when executing the for-in loop, you should first detect whether the property value of the modified object is null or undefined.
Iv. lable Statements
Basic syntax:
Label: < statement group >
such as: Begin:for (var i = 0; i <; i++) {alert (i); } illustrates the role of the lable statement: no lable:var num = 0; for (var i = 0; i < i++) { for (var j = 0; J <; J + +) {
11>if(i = = 5 && J = = 5
) { break
; } Num+ +
; } } alert (num);
//
the
Join lable:
var num = 0; Outpoint: for (var i = 0; i <, i++) { for (var j = 0; J < 10; J + +) { if(i = = 5 && j = 5 ) { break outpoint; } num++ ; } } // -
The first example of output 95 is not difficult to understand, the second example why the output 55, because the execution to break outpoint, jump directly to the Putpoint layer, execute the alert statement.
If you change the second example to the following:
1 varnum = 0;2 for(vari = 0; I < 10; i++){3 outpoint:4 for(varj = 0; J < 10; J + +){5 if(i = = 5 && J = = 5 ){6 Breakoutpoint;7 }8num++;9 }Ten }; One Aalert (num);//95
This result is consistent with the result of the first example.
V. Break and Continue statements
5.1break statement:
1 var num = 0; 2 for (var i = 1; i < i++) {3 if (i%5==0) {4 Break ; 5 }6 num++; 7 }; 8 alert (num);//4
Break statement jumps to alert statement after execution
5.2continue statement:
1 var num = 0; 2 for (var i = 1; i < i++) {3 if (i%5==0) {4 continue; 5 }6 num++; 7 }; 8 alert (num);//8
The continue statement jumps to the for () loop and resumes execution of the loop until the loop condition is not true.
Vi. with statements
Basic syntax:
With (object) { statements}
To illustrate:
Do not use with the notation:
var qs = location.search.substring (1); var hostName = location.hostname; var url = location.href; alert (QS); alert (hostName); alert (URL) ;
Use the WITH notation:
1 with var qs = search.substring (1 3 hostname; 4 var URL = href; 5 6
7 alert (QS); 8 alert (hostName); 9 alert (URL);
As you can see from the example above, the WITH statement is about setting the scope of the code to a specific object, reducing the number of duplicate inputs.
But the JS interpreter needs to check if the variables in the With block belong to the with object, which will make the WITH statement perform much slower and cause the JS statement to be difficult to optimize.
It is therefore not recommended to use the WITH statement on a large scale.
Vii. swith Statements
Basic syntax:
Switch (< expression >) {
Case < numeric 1>:< statement group 1>
Break
Case < numeric 2>:< statement group 2>
Break
...
Default < Statement group >
}
Execution process:
A switch statement can be any data type, and the value of each case is not necessarily a constant, it can be a variable, an expression, and so on, for example:
1 Switch("Hello World") {2 Case"Hello" + "world": 3Alert ("Greeting was found."));4 Break;5 Case"Goodbye": 6Alert ("Closing was found."));7 Break;8 default: 9Alert ("unexpected message was found."));Ten}
1 varnum = 25;2 Switch(true) {3 CaseNum < 0: 4Alert ("less than 0."));5 Break;6 Casenum >= 0 && num <= 10: 7Alert ("Between 0 and 10."));8 Break;9 Casenum > Ten && num <= 20: TenAlert ("Between and 20.")); One Break; A default: -Alert ("More than 20."); -}
The switch statement uses the equality operator comparison when comparing, so the type conversion does not occur.
Practice:
1 <script type= "Text/javascript" >2 var count = ten; 3 for (var i=0; i < count; i++) {4 alert (i); 5 }6 alert (i); // output What? 78 </script>
for (;;) { alert ("2");//How many times is the output 2? }
Learn JavaScript from the beginning (vi)--statement