Branch statement
if branch statement
1. Decide whether to execute a piece of code according to a concept or a condition
2. Grammatical form
(1) if statement
if (conditional) {snippet}
Meaning: when the condition is true, the code segment in {} will be executed, otherwise it will not be executed
var age=16 if (age>=18){ console.log("Go to the Internet cafe") }
(2) if else statement
1) if (condition) {snippet} else {snippet}
2) Meaning: if the condition is true, execute the code in {} after if, otherwise execute the code in {} after else
3) Note: code within two {} can and can only execute one
var age = 16 if (age >= 18) { console.log('Go to the Internet cafe') } else { console.log('Borrow an ID card') }
(3) If else... Statement
1) If (condition) {snippet} else if (condition) {snippet}
2) Meaning: start from the first if and judge each condition in turn. Which condition is true and execute the code in {} after which condition. As long as the previous if condition is true, the latter will be ignored; If none of the conditions holds, then none will be implemented
3) Note: only one code in multiple {} can be executed at most
var age = 5 if (age >= 18) { console.log('go to university') } else if (age >= 12) { console.log('middle school') } else if (age >= 6) { console.log('primary school') }
(4) If else statement
1) If (condition) {snippet} else if (condition) {snippet} Else {snippet}
2) Meaning: start from the first if, judge each condition in turn, which condition holds, and execute the code in {} after which condition;
As long as the previous if condition is true, the latter will be ignored; If none of the conditions holds, execute the code in {} after else
3) Note: Code in multiple {} can and can only execute one
var age = 5 if (age >= 18) { console.log('go to university') } else if (age >= 12) { console.log('middle school') } else if (age >= 6) { console.log('primary school') } else { console.log('Play at home') }
**Judge whether a number is even or odd** var n = 13 if (n % 2 === 0) { console.log('even numbers') } else { console.log('Odd number') } ----------------------------------------- leap year: year Is a multiple of 4, and year Not a multiple of 100 leap year: year Is a multiple of 400 Namely leap year: year % 4 === 0 && year % 100 !== 0 leap year: year % 400 === 0 var year = 1900 if (year % 4 === 0 && year % 100 !== 0 || year % 400 === 0) { console.log(year + ' It's a leap year') } else { console.log(year + ' It's a normal year') }
switch branch statement
1. Basic syntax:
switch (data){
case 1:
Code executed when condition 1 is met
break
case 2:
Code executed when condition 2 is met
break
default:
Code executed when all conditions are not met
}
Note 1: the contents in: () will not be converted to Boolean, but will get a final result and enter the switch to judge
Note 2: the data to be judged and each case must have the same value and data type (= = =) to meet the condition
Note 3: try not to write an expression in the case position, because conditional judgment will be made according to the result of the expression
Note 4: switch is not responsible for making scope judgment
Note 5: default can not be written. When it is not written, there will be no code execution if all case s are not satisfied
Note 6: if the break is not written, the break penetration effect will occur
- Penetration effect: whether the next case is satisfied or not, the code will be executed directly until the next break is encountered or the whole code is executed
According to 1 ~ 12 How many days are these numbers output in the current month(2 The month is temporarily calculated as 28 days) var month = 9 switch (month) { case 1: case 3: case 5: case 7: case 8: case 10: case 12: console.log('31') break case 4: case 6: case 9: case 11: console.log('30') break case 2: console.log('28') break } ----------------------------------------------- The judgment is the day of the year var year = 2020 var month = 12 var date = 31 var total = date switch (month) { case 12: total += 30 case 11: total += 31 case 10: total += 30 case 9: total += 31 case 8: total += 31 case 7: total += 30 case 6: total += 31 case 5: total += 30 case 4: total += 31 // Superimposed March case 3: total += year % 4 === 0 && year % 100 !== 0 || year % 400 === 0 ? 29 : 28 case 2: total += 31 // Superimposed January } console.log(total) ----------------------------------------------- var year = 2020 var month = 12 var date = 31 var total = date if (month > 1) total += 31 if (month > 2) total += year % 4 === 0 && year % 100 !== 0 || year % 400 === 0 ? 29 : 28 if (month > 3) total += 31 if (month > 4) total += 30 if (month > 5) total += 31 if (month > 6) total += 30 if (month > 7) total += 31 if (month > 8) total += 31 if (month > 9) total += 30 if (month > 10) total += 31 if (month > 11) total += 30 console.log(total)