A required condition branch for javascript every day.
Hello everyone, let's talk about operators in the previous section. Today's task is to explain the logic condition branches and loops.
Let's first simulate a logical block. let's just use the tickets we often come into contact with. The ticket price is different for different people, but we all perform the same behavior to buy a ticket, so we can write the ticket into a function BuyTicket.
// Code function BuyTicket () {console. log ("Please pay $200 ");}
We can see that this function body is actually flawed. when each of us buys a ticket, this function is executed. If it is a military or a student, this function is not applicable, their discounts cannot be used. We can only write one more function to solve this problem. Then, our javascript language has provided us with Branch statements, we can easily solve this problem. We can modify the function. Let's take a look at the modified Code.
Function BuyTicket (identity) {if (identity = "student") {console. log ("Please pay 100 RMB");} if (identity = "military") {console. log ("Please pay 150 RMB");} if (identity = "normal person") {console. log ("Please pay 200 RMB") ;}// simulate 3 types of people to buy tickets BuyTicket ("student"); BuyTicket ("Soldier "); buyTicket ("ordinary person ");
In this way, it is more scientific than the previous method, and I will gradually upgrade it later, and gradually everyone will understand the beauty of the program.
Through the example above, we can easily see that such a function can solve the branch problem. Now let's explain the if branch syntax.
If (condition) {// execution statement}
The condition is the non-empty object, non-undefined object, Boolean value true, non-0 number, all strings, in our function body, the = Operator is used to calculate and obtain a Boolean value for judgment. This is also a method we often use, we can also use other values as the condition. Let's take another picture for you to understand.
A: The condition is true, so the word "executed" is printed. B: the condition is not A qualified value of the if condition, so it is not executed. Note: here we all use the clear value. Sometimes we use the return value after the operation, as in the = operation used in the previous BuyTicket function. If the return value is true, if the returned value is false, it will not be executed. Each time we execute BuyTicket, the input value can only meet one condition.
Next, let's take a look at the complete if Branch
// First, two branch if (condition) {// execution} else {// execution}
Let's get it straight.
At A: The preceding code is executed. If the condition at B is unqualified, the statements in the else block are executed. It is very simple. I will not explain it much.
In the case of two branches, one or both of them will always be executed.
// Second, if (condition) {// execution} else if (condition) {// execution }... Else if (condition) {// execution} else {// execution}
In the case of multiple branches, only one of them is always executed. If one of the branches is executed, the following condition comparison will not continue. This is a highly efficient method, if you simply write the if block, it will be the same as the BuyTicket function above. It is a very inefficient method, and the three if blocks will be compared. Therefore, we can transform the BuyTicket function.
// Code funciton BuyTicket (identity) {if ("student") {console. log ("Please pay $100");} else if ("military") {console. log ("Please pay $150");} else {console. log ("Please pay 200 RMB ");}}
This method is more scientific, and the execution effect is the same as above. The advantage of this method is that the execution efficiency is high and the conditions are matched one by one. If the conditions are qualified, the execution is performed, instead of matching other condition blocks, this is more efficient than the previously written Function Code. However, there is also a replacement method for multiple branches, the switch struct, first look at the syntax
Switch (expression) {case constant expression 1: // execute break; case constant expression 1: // execute break; case constant expression 1: // execute break; default: // execute break ;}
The execution process from top to bottom is to match the constant expressions one by one. If the expression matches one by one, execute the statements in it. Remember to add the break keyword after each case block, otherwise, the statement in the subsequent case block will be executed in sequence until the break is encountered, so we can also use this feature. In the appropriate case, we will not write the break keyword. Now, we can use the switch struct to modify the previous BuyTicket function.
Function BuyTicket (identity) {switch (identity) {case "student": console. log ("Please pay 100 RMB"); break; case "military personnel": console. log ("Please pay 150 RMB"); break; case "normal person": console. log ("pay 200 RMB"); break; default: console. log ("Please show your ID card ");}}
The default block is the code that is executed by default when no expression matches. Because this is the last code block, the break keyword can also be omitted.
To sum up, today, we only talk about the knowledge of condition branches, such as if, if -- else, if -- else, and switch, every day, I can only take some time to write. The length is limited. The next article is the write loop. Well, you can digest it first, and the next article will continue.
Articles you may be interested in:
- Conditional compilation of jscript/javascript in IE
- Condition judgment in javascript
- Tips for Javascript condition judgment
- ExtJS GridPanel changes the font color based on the conditions
- Js uses the and or operator priority to implement the if else condition judgment Expression
- Before leaving the current page, use js to determine whether to exit the page as a condition prompt
- Summary of javascript if condition judgment methods
- Whether the variables of various types in js are true or false in the if Condition
- Extjs example of setting the background color of a row in the table based on the Condition