In the previous section we introduced the function, this section we introduce the Process control statements, for whatever programming language, process control is very important, that is, we often say the order structure, selection structure and loop structure.
Select Structure ***************
1.JAVASCRIPT uses conditional statements to implement a selection structure, which provides a choice structure primarily if statements, if....else statements, and if...else if ... else statements, and another switch statement, much like PHP.
2. These statements are relatively basic, so we'll just give if...else if. The pseudo-code in the form of else such as the following:
if(condition1) {When the condition1 fortruecode to run when}Else if(condition2) {When the condition2 fortruecode to run when}Else{when the condition1 and Condition 2 are nottruecode to run when}
3. The following is the pseudo-code of the switch statement:
switch Span style= "Line-height:1.5!important" > (n) { Case 1: Run code block 1 break case 2 : Run code block 2 break default n with 1 and 2 code running at different times}
4. It is necessary to note that the break here is used to jump out of the switch statement, and default is used to run the code when the above content is not matched.
For Loop ************
1.js for Loop and C, C + +, Java, PHP for Loop are the same, are the following form, first look at the pseudo-code:
for (initial conditions; finally inferred condition; step run increment) { // code to run each cycle }
2. Then there is an example:
var x = 0; for (var i = 0;i <5;i++) { + = i;}
3. Then x is 10, which calculates the and from 0 to 4.
4. We are able to iterate over the properties of an object with a for in loop, such as the following code:
var msg = ""; var person = {name: "Sinsing", Age:24}; for inch Person ) { = msg + person[x];} Alert (msg);
5. The above. Person is an object, about the object we will also explain later, here we see that X is a property of the person that appears, the person calls this property as we use the array in other programming languages use method.
While Loop ******************
Pseudo-code for the syntax of the 1.while statement:
while (condition) { // function code }
2. Let's write out its real Code demo sample based on this pseudo-code:
var sum = 0; var i = 0; while (i <10) { + = i; I+ +;} alert (sum);
It is important to note that the i++ here do not ignore, otherwise the code will enter the dead loop.
3. Similarly there are do...while loop statements, pseudo-code such as the following:
Do { // function code;}while (condition);
The only difference between the 4.do...while loop and the while loop is that the do...while loops first and then infers the condition, in fact, we can replace it with a while loop, so some languages discard the do...while statement, However, our JS language is still supported.
Summary **************
1.js code as a script, its program run order is from top to bottom, it does not start from the main function as the C language.
2. This section refers to three kinds of order and several statements, there are other programming language Foundation, very easy to understand, and JS itself is modeled after the C syntax, it is easier to read.
2014 Sinsing JavaScript interpretation Fourth flow control statements