Conditional statements
The conditional statement (Conditional statement) is one of the basic constructs in JavaScript, and the program executes or skips a branch depending on whether the expression is true or false, and the conditional statement can sometimes be called a "branch statement"
1. If & else if & else
The basic wording is as follows:
if (expression 1) { // If expression 1 is true, execute code block 1 Else if (expression 2) { // If expression 2 is true, execute code block 2 else { // Otherwise, execute code block 3 code block 3}
Give me a chestnut:
varScore = 78;if(Score >= 90) {alert ("Score: Excellent");} Else if(Score >= 80) {alert ("Score: Liang");} Else if(Score >= 70) {alert ("Score: Medium");} Else if(Score >= 60) {alert ("Score: Poor");} Else{alert ("Score: Failed");}//popup Result: "Score: Medium"
2. Switch
The If & else if & else logic above is still a bit confusing, and to write multiple expressions, the structure is not very clear. So, with the switch statement
The basic wording is as follows:
Switch(n) { Case1://if N==1, execute code block 1code block 1; Break; Case2://if n==2, execute code block 2code block 2; Break; Case3://if n==3, execute code block 3code block 3; Break; default://if n is not equal to the above, execute the code block ncode block N; Break;}
Give me a chestnut:
varDay = 4;Switch(day) { Case0: Alert ("Sunday"); Break; Case1: Alert ("Monday"); Break; Case2: Alert ("Tuesday"); Break; Case3: Alert ("Wednesday"); Break; Case4: Alert ("Thursday"); Break; Case5: Alert ("Friday"); Break; default: Alert ("Saturday"); Break;}//popup Result: "Thursday"
Looping statements
Similarly, a looping statement (looping statement) is also one of the basic constructs of JavaScript, which can execute a piece of code repeatedly until a given condition is not true.
JavaScript has 4 loop statements: While & Do/while & for & for...in
Each loop structure has a slightly different, be careful to distinguish, if you can determine the number of cycles in advance, with a for loop, otherwise, consider using while or do/while
where while and do/while are slightly different, while the loop is the first to judge after execution, and the Do/while loop is executed first after judgment. Therefore, Do/while is executed at least once, regardless of whether the condition is true or not.
PS:You do not have to add a semicolon at the end of the while, Do/while need to add semicolons at the end
Why don't you just give me a chestnut?
Summation: 1+2+3+...+100
1. While
// While Loop var sum1 = 0; var j = 1; while (J <=) { = sum1 + J; J+ +;} Alert ("1 + 2 + 3 + ... + + =" + sum1); // 5050
2. Do/while
// Do and Loop var sum2 = 0; var k = 1; Do { = sum2 + k; K++ while (k <=); alert ("1 + 2 + 3 + ... + + =" + sum2); // 5050
3. For
// For Loop var sum3 = 0; for (Let i = 1; I <=; i++) { = sum3 + i;} Alert ("1 + 2 + 3 + ... + + =" + sum3); // 5050
4. for...in
// For ... in iterating over array elements var arr = [1,2,3,4,5,6,7,8,9,10]; for (var in arr) { console.log (arr[i]);} // output array elements: 1,2,3,4,5,6,7,8,9,10
Where for/in is commonly used to traverse object member properties
// For ... in iterating over an array index for (var in arr) { console.log (key);} // output array index (properties of the Array object): 0,1,2,3,4,5,6,7,8,9
JavaScript basic syntax--conditional Statements & looping statements