Take you to understand JavaScript from zero to essence (V) JavaScript branch statements
1, What are JavaScript statements
Expressions are phrases in JavaScript. A newline phrase or a semicolon ending phrase is a line of statements. A command issued by a JavaScript statement to a browser. Statement tells the browser what to do. Statements usually use one or more keywords to complete the specified task, and the browser will execute each statement in writing order. The statement is case sensitive.
JavaScript statements: commands sent by JavaScript statements to browsers. Statement tells the browser what to do.
semicolon
- Semicolons are used to separate JavaScript statements.
- Usually we add a semicolon at the end of each executable statement.
- Another use of semicolons is to write multiple statements on a line.
1.1 statement declaration
// 1. One statement per line var a = 1; var b = 2; var sum = a + b; var c = 1 + 2; console.log(a,b,sum); // 2. Multiple statements on one line var a,b;a = 1;b = 2;var sum = a + b; console.log(a,b,sum);
1.2 JavaScript code block
JavaScript can be organized allocatively, with code blocks starting with left curly braces and ending with right curly braces.
Function: the code block can arrange multiple lines of statements into a sequence and execute them in turn
2, Conditional judgment statement
Conditional statements are used to perform different actions based on different conditions. Usually when writing code, you always need to perform different actions for different decisions. You can use conditional statements in your code to accomplish this task.
In JavaScript, we can use the following conditional statements:
- If statement - use this statement to execute code only if the specified condition is true
- if...else statement - executes code when the condition is true and other code when the condition is false
- if...else if....else statement - use this statement to select one of multiple code blocks to execute
Conditional statements: determine whether to execute or skip certain statements by judging the value of the specified expression
Syntax:
// 1. First judge whether the conditions are met if (condition) { When the condition in brackets above is true Code executed when } // 2. The if code block can be followed by an else code block to indicate the code to be executed when the conditions are not met if (condition) { When the condition in brackets above is true Code executed when } else { When the condition in brackets above is not true Code executed when } // 3. Multi branch statement if (Condition 1) { When condition 1 is true Code executed when } else if (Condition 2) { When condition 2 is true Code executed when } else { When neither condition 1 nor condition 2 is true Code executed when }
Example:
/* if-alse Select structure: condition is a range judgment Result of conditional operation: true/ false if(Condition) {/ / condition result can only be true/false .... } else { .... } */ /* In life, there are often realistic scenes with conditions Examination results Grade > = 60 pass */ // The user is required to enter a score from the keyboard var score = prompt("Please enter the score"); // Range judgment if(score >= 60) { alert("Pass the grade"); } // How to output unqualified results? It indicates the code to be executed when the conditions are not met if (score >= 60){ alert("Pass the grade"); } else { alert("Fail in grade"); } // Accept the entered gender with a gender var gender = prompt('Your gender is:'); // if judge gender if (gender == 'male') { alert('Go to the men's room!') } else if (gender == 'female') { alert('Go to the ladies' room!') } else { alert('I don't know') }
/* The user enters the report card, the score is 86 ~ 100, the evaluation is excellent; 71 ~ 85 is good; 60 ~ 70 passes; 0 ~ 59 fails! */ // Keyboard entry score var score = prompt("Please enter the score"); // If else judgment // If (0 < = score < 60) {/ / wrong!!!!!!! if(score >= 0 && score < 60){// If if (condition) alert("fail,"); } else if(score >= 60 && score <= 70) { // If else if (condition) alert("pass"); } else if(score >= 71 && score <= 85) {// alert("good"); } else if(score >= 86 && score <= 100) { alert("excellent"); } else {// Otherwise, there are no conditions alert("This is no longer your stage......"); } /* Logical operator And:&& Condition 1 and condition 2 and condition 3 When all conditions are true, the result is true When any condition is false, the result is false */
3, Switch statement
The switch statement is used to perform different actions based on different conditions. It works by setting the expression n (usually a variable) first . the value of the expression will then be compared with the value of each case in the structure. If there is a match, the code block associated with the case will be executed. Use break to prevent the code from automatically moving to the next case. Case penetration because the switch case statement uses congruence when comparing
Syntax:
/* switch-case Select structure The condition is a specific data, and the specific data is used for congruent comparison with the data in the case switch(Value){ case Value 1: / / in fact, the value = = = value 1 is judged .... break; .... } Like multiple choice questions */ switch(n) { case 1: Execute code block 1 break; case 2: Execute code block 2 break; default: break; //n code not executed simultaneously with case 1 and case 2 };
/* switch-case Select structure The condition is a specific data, and the specific data is used for congruent comparison with the data in the case switch(Value){ case The value 1: / / is actually the judgment of value = = = value 1 ..... break; ..... } */ /* 1. Practice the first case of switch case*/ /* var choice = prompt("Please enter your options: "; switch(choice) { case "A":// choice === "A" alert("Wrong answer "); break; case "B": alert("The answer is correct) "; break; case "C": alert("Wrong answer "); break; case "D": alert("Wrong answer "); break; default: alert("You have a gun in your head... "); } */ /*2. Practice the second case of switch case*/ var choice = prompt("Please enter your options:"); switch(choice) { case "A":// Failure to add break; in case will cause case penetration var x = "*******"; case "C": var c = "********"; case "D": alert("Wrong answer"); break; case "B": alert("The answer is correct"); break; default: alert("This option is not available"); } /*3.Enter an integer of 0-6 to judge the week*/ /* // User input integer var num = prompt("Please enter an integer: "); / /" 3“ // judge switch(num) {// switch()There is a specific value in the middle of the bracket, and there is no implicit type conversion case "0":// case This is followed by the value to be compared with num alert("Sunday "); break;// After executing the case, end the selection statement case "1": alert("Monday "); break; case "2": alert("Tuesday "); break; case "3": alert("Wednesday "); break; case "4": alert("Thursday "); break; case "5": alert("Friday "); break; case "6": alert("Saturday "); break; default:// otherwise alert("Without this day "); } */
default keyword
Please use the default keyword to specify what to do when the match does not exist
Example:
var num = Number(prompt("Please enter a number:")); switch(num) { case 1: alert("Hello, number 1"); break; case 2: alert("Hello, number 2"); break; default: alert("Hello, other numbers"); break; }
Write after:
The furthest distance in the world is that you are in if and I am in else. It seems that you have been together but separated forever;
The most infatuated waiting in the world is that you are switch and I am case. You may never choose yourself, but you will always be with me.
summary
The above is today to bring you into zero understanding, to bring you into zero understanding JavaScript To the essence (V) JavaScript Branch statement Will continue to update It's not easy to be original. We look forward to your praise, attention and forwarding comments๐๐๐