JavaScript daily must learn conditional branching _javascript tips

Source: Internet
Author: User

Hello everyone, we go on to the front today, the front has probably talked about the operator, today's task is mainly to explain the logical conditions of the branch, loop.
We first to simulate a logic block, we often contact with the purchase of tickets, ticket prices for different people price is a difference, but we are the same to carry out the act of buying tickets, we can buy tickets written as a function buyticket

Code
function Buyticket () {
console.log ("Please pay 200 yuan");
}

You see this function body, in fact, there are defects, each of us to buy tickets, is the implementation of this function, if the military, or students, this function does not apply, their discount is no way to use, we can only write a function to solve such problems, and then, Our JavaScript language has provided us with branching statements, we can easily solve the problem, we can modify the function, we look at the next modified code

function Buyticket (identity) {
if (identity = = "Student") {
console.log ("Please pay 100 yuan")
;  
if (identity = = "Military") {
console.log ("Please pay 150 yuan");
if (identity = = "ordinary Person") {
console.log ("Please pay 200 yuan");
}

//Simulate 3 kinds of people to buy tickets
buyticket ("student");
Buyticket ("Soldier");
Buyticket ("ordinary people");

This use is not more scientific than the previous wording, I will slowly upgrade, slowly everyone will understand the lovely place of the program.

With the example above, it's not hard to see that a function like this can solve the branching problem, and now we're going to explain the IF branch syntax

 if (condition) {
//execute statement
}

The condition is the Non-empty object that we talked about earlier, non-undefined object, Boolean true, not 0 digits, all strings, we use the function in the body of the = = comparison character to get a Boolean value to make the judgment, which we often use a way, we can also use other values as a condition, One more picture, let's understand

A: The conditions are all set up, so all printed out "executed" a few words; B: The condition is not a qualified value of the IF condition, so there is no execution. Note: Here we are using the value of the values, sometimes we will use the return value after the operation, as in the previous Buyticket function used in the = = operation, the return if it is true, the execution, if the return is false, certainly not executed, each time we execute buyticket, The value passed in, only one condition can be met.

Next, we'll learn the complete if branch

First, two branches
if (condition) {
//execute
}else{
//Execute
}

Directly on the map, you will understand

A: Execution of the previous code, B, the condition is not qualified, so it executes the else block inside the statement. Very simple, no more explanations.

Two branches of the case, is always executed one, not two simultaneous

Second, multiple branches if (condition) {//
execute
}else if (condition) {/
/execute
}else if (condition) {/
/execute
}...else if (condition) { c19/>//Execution
}else{
//Execution
}

The case of multiple branches, always execute only one of them, and if you do one of those branches, you will not continue the following condition comparison, is a more efficient way, if simply to write if block, it will be like the Buyticket function above, it is a very inefficient writing, Three if blocks will be compared so, we can change the Buyticket function

Code 
Funciton Buyticket (identity) {
if ("student") {
console.log ("Please pay 100 Yuan");
}else if ("military") {
console.log ("Please pay 150 yuan");
}else{
Console.log ("Please pay 200 yuan");
}
}

This kind of writing is more scientific, the effect of execution is the same as before, the advantage of this writing is that the execution efficiency is high, the condition matches each other, if the condition is qualified to carry on, but will not go to match the other condition block, so the efficiency is better than the function code written in the preceding, however, there is also a multiple branch of the substitution method, Switch structure, see Grammar first

switch (expression) {case
constant expression 1:
//execute break
;
case constant Expression 1:
//execution break
;
case constant Expression 1:
//execution break
;
Default:
//execute break
;
}

The process of executing from the top down, that is, the expression and the following constant expression to match one by one, if the match, on the execution of the statement, remember, in each case block followed by the break keyword, or, in turn, will execute the following case block inside the statement, until you encounter a break, So we can also use this feature, and in the appropriate case, we will not write the break keyword. OK, let's start with the switch structure to modify the previous Buyticket function

function Buyticket (identity) {
switch (identity) {case
"student":
console.log ("Please pay 100 Yuan");
break;
Case "Military":
Console.log ("Please pay 150 yuan");
break;
Case "ordinary person":
console.log ("Please pay 200 yuan");
break;
Default:
console.log ("Please show your ID card");
}
}

The default block is when an expression does not match the case, we execute this piece of code, because this is also the last code block, so you can omit the break keyword

To sum up, today, only said conditional branch of knowledge, if, If--else, If--else if--else, switch on these several conditions Branch statement block, every day I can only take out a certain amount of time to write, space is limited, the next is to write a cycle, well, everyone first to digest, Let's continue with the next article.

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.