Branching statements in JavaScript

Source: Internet
Author: User

There are two types of branch statements in JavaScript, if...else ... Branch statement; Switch...case. The branch statement.

If...else ... Branch

If a bool value or a logical expression is required inside the parentheses after the IF, the curly braces can be omitted if there is only one line to execute. If you have more than one condition, you can add an if judgment after else.

Look at a few examples below:

var num = +;  // define the variable num, and assign the value // The IF statement starts, determines whether NUM is equal to 100, and if so, executes the statement inside the curly braces if (num = =) {       num+ +;    alert (num);}

The above code shows how to execute an if branch, and if NUM is 100, the statement inside the curly braces is executed. The idea is that if there is only one line in the curly braces, you can omit the curly braces.

var num = +;      // define the variable num, and assign the value if (Num > 100) {    //If statement start    alert (num + "greater than");} Else {              //Else statement start    alert (num + "less than or equal to");}

This code demonstrates a if...else ... statement, because each conditional branch statement block has only one line of statements, so you can also omit the curly braces, the modified code:

var num = +;      // define the variable num, and assign the value if (num > +)    // If statement starts    Alert (num + "greater than"); Else               // Else statement begins    Alert (num + "less than or equal to 100");

If...else ... Statements can evolve into successive judgments in addition to being used, and here is a demo code:

var num = +;      // define the variable num, and assign the value if (num > +)      // If statement starts    Alert (num + "greater than"); Else if (num==100)   // else if statement    Alert (num + "equals"); Else                // Else Statement    Alert (num + "less than 100");

As you can see, in the above code, there is more than one row else if statement, if there are multiple branches, you can use this method to implement.

Switch...case ... Branch

The (n) after switch can be an expression, or (and usually) a variable. The values in the expression are then compared to the numbers in the case, and if they match the one, the subsequent code is executed. The effect of break is to prevent the code from automatically executing to the next line.

varnum = 100;//define the variable num, and assign the valueSwitch(num) { Case1: {alert ("1"); };  Break;  Case50: {alert ("50"); };  Break;  Case100: {alert ("100"); };  Break; default: {alert ("The default message box! "); }}

The above code simply describes the switch...case ... The usage.

Note: The case branch must be followed by a constant; Break statement do not forget, otherwise after the completion of the sub-branch, continue to execute his branches below until the break statement jumps out! The default branch is not required, and it is often necessary to use the default branch in order to avoid unhandled data.

Branching statements in JavaScript

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.