if,else if,else judgement statement
Assumption: Today's sweeping allows (No. 1-20) students to sweep.
var num = 10
Let Student No. 10, who stores variables, clean it up
if(num){ console.log("I walked in.") }//true executes the code in brackets when num values are converted to Boolean values
When the condition is false to another branch (condition)
if(0){ console.log("On condition that I come in") }else{ console.log("On condition that I come in") }
Hypothesis: The top 20 students sweep the floor during the cleaning
var num = 10 if(0<num<20){ console.log("Sweep the floor") }
Why 0 < num < 20 is a true analysis
0<num<20 0<10 --> true true<20 --> true 0<num<20 0<30 --> true true<20 --> true //The range of 0 < num < 20 control variables in mathematics (0,20)
- The Num Range (0,20) of 0<num<20 Control Variables in Mathematics
- The range (0,20) interval of num > 0 & & num < 20 control variable num in JS (must & & both sides are true)
Hypothesis: The top 20-40 students sweep the floor during the cleaning.
var num = 30;//Controlling a variable should go to that block of code (control flow) if(num>0 && num<20){ console.log("Sweep the floor") }else if(num >=20 && num <40){ console.log("Surf the Internet") }else if(num>=40 && num < 60){ console.log("Mop the floor") }else if(num>=60 && num < 80){ console.log("Singing jump") }else{else All cases in which none of the above conditions are valid console.log("rap") }(Only when the preceding conditions are not satisfied can we come to this condition.
If (condition 1){ Conditional 1 is true and executes JS code } else if (condition 2){ Conditional 2 is true and executes JS code } else if (condition 3){ Conditional 3 is true and executes JS code }else{ Code of JS executed when condition 123 is not satisfied }
If there can only be one else if there can be many else only one
You can also write an if directly.
If both conditions satisfy that block of code to execute
var num = 30; if(num>1){ console.log("if Set up execution code") }else if(num>2){ console.log("else Set up execution code") }
When the condition is first established, the corresponding code will be executed. When the latter condition or the latter condition is established many times, only the first corresponding code block will be executed (all conditions are mutually exclusive).
If multiple conditions are met at the same time, only the procedure for which the first condition holds will be executed.
- If statement - Use this statement to execute code only if the specified condition is true.
- Other if statement - detects a new condition if the first condition is false to use the statement to execute the code.
- The else statement executes the block of code if the condition of the if statement or else statement is false
-
If the else statement is written in js with an operator, if multiple conditions are met at the same time, only the program with the first condition will be executed.
The result of the condition is ultimately to get the Boolean value true or false. - If there can only be one else if there can be many else only one
Distinguish common objects from js code blocks
From the data type point of view of js, he is a common object (there is no JS statement in it)
If there is one or more js statements in it, then it is a js code block.
ternary operator
Trinomial operators: simplified if and else statements
var num = 20; if(num<10){ console.log("Hot head") }else{ console.log("rap") }
Trinomial Operator: Simplifying if and else statements can only simplify if else
If (condition){ JS Code Executed True Conditions }else{ JS Code with False Execution Conditions } Conditions? Conditions for true execution of JS code: Conditions for false execution of JS code;
The following three items are abbreviated as if else above.
Abbreviated into three headings: Num < 10? Console. log ("ironing"): console.log("rap")
How to simplify two data into three items in an if
var num = 5; if(num<10){ console.log("Hot head"); console.log("smoke") }else{ console.log("rap"); }
Simplified into three orders
num < 50?(console.log("Hot head"),console.log("smoke")):console.log("rap") //If you have 2 sentences of js code, don't simplify it with three eyes
Conditions? (Conditions for true execution of JS code 1, conditions for true execution of JS code 2): Conditions for false execution of JS code;
How to simplify if condition is empty with three eyes
var num = 15; if(num<15){ }else{ console.log("rap"); }
Simplified into three orders
num<10?null:console.log("rap"); //Use null for placement (Boolean values that can be occupied are converted to false)
Conditions? Occupancy: Conditions for false execution of JS code;
How to simplify the else condition with three eyes when it is empty
var num = 15; if(num<10){ console.log("Hot head") }else{ }
Simplified into three orders
num<10? console.log("Hot head"):null; //Use null for placement (Boolean values that can be occupied are converted to false)
Conditions? Conditions for true execution of JS code: placeholder;
Generally, the Boolean value is not used to occupy the place of false, so we can recognize it.
switch judgement statement
switch: Controls which code block the variable should go to
var num = 4; switch(num){ case 2: console.log('My school number is 2.'); break; case 4: console.log('My school number is 4.'); break; case 6: console.log('My school number is 6.'); break; case 8: console.log('My school number is 8.'); break; case '10': console.log('My school number is 10.'); break; default: console.log('My school number is not 246 8 10'); }
Switch (variable){ case data 1: The JS code is executed when the condition is established. break; case data 2: The JS code is executed when the condition is established. break; case data 3: The JS code is executed when the condition is established. break; default: If the above conditions are not valid, the JS code will be executed. }
- case is a scenario (which can be understood as a block of code)
- The variables and the data behind the case are equal==== true before they can be executed.
- The function of break in switch is to terminate the program (if you don't write it, the block of code will miss out like an hourglass).
- true after switch is used for intervals (flexible writing), and variable after switch is fixed writing.
- Default Write default if all conditions are not established.
Practice
var num = parseFloat('height:20.3px'); -->parseFloat Function: Extract numbers from left to right, encounter non-numbers except(Number, decimal point, minus sign)It will stop. -->Meet the letter h Stop extracting-->The result is NaN if(num==20.3){-->NaN==20.3(NaN Do it with any data type==Judgment results are all false) console.log(20.3);-->The result is false Unable to print }else if(num==NaN){--> NaN==NaN -->ftyalse console.log(NaN)-->The result is false Unable to print }else if(typeof num=='number'){ -->1.typeof NaN=="number" -->2.typeof Priority is greater than==(Amount to typeof NaN)NaN(A number that is not a number) -->3."number"=="number" console.log('number');-->The result is true The print is number }else{ console.log('num Nothing'); -->When the above results are false( false)When printing this string }