In addition to Weibo, there is also WeChat
Please pay attention
WeChat public account
Shulou
2025-02-22 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >
Share
Shulou(Shulou.com)06/02 Report--
This article mainly introduces what are the choice sentences in javascript. It is very detailed and has a certain reference value. Friends who are interested must finish it!
There are two choice statements in js: 1, "if else" statement, syntax "if (conditional expression) {/ / code} else {/ / code}"; 2, "switch case" statement, syntax "switch (expression) {case value: statement; break;..default: statement;}".
The operating environment of this tutorial: windows7 system, javascript1.8.5 version, Dell G3 computer.
Conditional judgment sentence is a frequently used form of sentence in the process of program development. Like most programming languages, conditional judgment statement is also available in JavaScript. The so-called conditional judgment means that the program performs different operations according to different conditions, such as displaying different contents according to age, and judging whether the operation is successful or failed according to the Boolean value true or false.
The following different forms of conditional judgment statements are supported in JavaScript:
If else statement (with multiple variants)
Switc case statement
If else statement
If else statement is a kind of conditional judgment and execution in process control. When the statement is executed, the condition is judged first, and then the corresponding action is made according to the judgment result. It can be subdivided into three types, namely, if statement, if...else statement and if...else if...else statement.
The if statement is the simplest conditional judgment statement in JavaScript. The syntax format is as follows:
If (conditional expression) {/ / Code to be executed;}
When the conditional expression holds, that is, the result is a Boolean value of true, the code in {} is executed.
The if else statement is an upgraded version of the if statement, which specifies not only the code to be executed when the expression is established, but also the code to be executed when the expression is not valid. The syntax format is as follows:
If (conditional expression) {/ / Code to be executed when an expression is valid} else {/ / Code to be executed when an expression is not valid
Both if and if else statements have only one conditional expression, while the if else if else statement is their more advanced form, which allows you to define multiple conditional expressions in the if else if else statement and execute the corresponding code based on the result of the expression. The syntax format is as follows:
Code executed if if (conditional expression 1) {/ / conditional expression 1 is true} else if (conditional expression 2) {/ / code executed when conditional expression 2 is true. Code to execute if else if (conditional expression N) {/ / conditional expression N is true} else {/ / code to be executed if all conditional expressions are false
Tip: during the execution of the if else if else statement, when it encounters a valid conditional expression, it will immediately execute the code in the following {}, and then exit the entire if else if else statement. If there is a valid conditional expression in the subsequent code, it will not be executed.
Example:
JavaScript var now = new Date (); / / get the current full date var dayOfWeek = now.getDay () / / get a number between 0 and 6 to indicate what day it is 0 means Sunday, 1 means Monday, And so on if (dayOfWeek = = 0) {/ / determine what day it is currently alert ("Sunday")} else if (dayOfWeek = = 1) {alert ("Monday")} else if (dayOfWeek = = 2) {alert ("Tuesday")} else if (dayOfWeek = = 3) { Alert ("Wednesday")} else if (dayOfWeek = = 4) {alert ("Thursday")} else if (dayOfWeek = = 5) {alert ("Friday")} else {alert (Saturday)}
Switch case statement
The switch statement is similar to the if...else if...else statement, but also a branch structure, and the switch statement is more concise and straightforward than the if...else if...else statement.
The switch statement consists of an expression and multiple case tags, the case tag followed by a code block, and the case tag as the identification of this code block. The syntax format of the switch statement is as follows:
Switch (expression) {case value 1: statement block 1; break; case value 2: statement block 2; break;. Case value n: statement block n; break; default: statement block nail1;}
The switch statement compares the value of the expression with the value in case in turn, and if it is not equal, it continues to find the next case;. If it is equal, the corresponding statement is executed until the end of the switch statement or until the break is encountered.
In general, switch statements end up with a default value of default, and if no matching condition is found in the previous case, the default statement is executed, similar to the else statement. Default is a switch clause that can be located anywhere within the switch without affecting the normal execution of the multi-branch case.
Note: in the switch statement, expressions are congruent (=) to match the values in each cese clause. Because the congruent operator is used, the type of each value is not automatically converted.
The execution flow (how it works) of the switch statement is shown in the following figure:
Example:
Var grade = 80 switch (grade/10) {case 10: case 9: console.log ("An etc."); break; / / stop execution and jump out of switch case 8: console.log ("B et al"); break / / stop execution, jump out of switch case 7: console.log ("C, etc."); break; / / stop execution, jump out of switch case 6: console.log ("D etc."); break / / stop execution and jump out of switch default: / / the code console.log ("E, etc.") executed by default when none of the above conditions are met;}
Output:
The following points should be noted when using switch statements:
Unlike the if statement, the data type of the expression after the switch statement can only be an integer or a string, not a bool.
Unlike the if statement, curly braces after the switch statement are required.
The number of case statements is not specified and can be increased indefinitely. But there should be a space between the case tag and the value after the case tag, and the value must be followed by a colon, which is part of the syntax.
After the switch matching is complete, the statements in the matching branch module are executed one by one until the end of the switch structure or the break statement is encountered. So, if a break statement is not written at the end of a branch statement, the program will continue to execute the contents of the next branch statement.
Similar to the else in the if statement, the default tag in the switch statement is directly followed by a colon, which seems to have no condition, but it is conditional on the condition that the value of the "expression" cannot be equal to the value after any previous case tag, and then the statement in the default branch is executed. The default tag, like the else clause in if, is not required in the switch statement and can be omitted.
The above is all the contents of the article "what are the selected sentences in javascript?" Thank you for reading! Hope to share the content to help you, more related knowledge, welcome to follow the industry information channel!
Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.
Views: 0
*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.
Continue with the installation of the previous hadoop.First, install zookooper1. Decompress zookoope
"Every 5-10 years, there's a rare product, a really special, very unusual product that's the most un
© 2024 shulou.com SLNews company. All rights reserved.