On the _javascript techniques of branching structure in JavaScript

Source: Internet
Author: User
Tags switch case

Speaking of the branching structure in JavaScript, we have to mention the word Process control, all of our programs are made up of data and algorithms.

program = data + algorithm

Usually, the algorithm can be composed of "order", "branch", "loop" three kinds of structure to complete.

Some statements (also called Process Control statements, branching structure statements) are defined in ECMA, which, in essence, define the main syntax in ECMAScript, which typically uses one or more keywords to complete a given task.

1.1 If statement

If statement-use this statement to execute code only when the specified condition is true

 if (condition)
  {
  code that executes only if the condition is true
  }

If...else Statement-executes code when the condition is true, and executes other code when the condition is false

if (condition) {code that executes when the
 condition is true
 }
else
 {
 code to execute when the condition is not true
 }

If...else If....else Statement-Use this statement to select one of several code blocks to execute

if (condition 1)
{code that
executes when condition 1 is true
}
else if (condition 2)
{
code to execute when condition 2 is true
}
else
   
    {
code to execute when both condition 1 and Condition 2 are not true

   

1.2 Switch statement

Use the switch statement to select one of the multiple blocks of code to execute.

Switch (n)
{case
1:
 Execute code block 1 break
 ;
Case 2:
 execute code block 2 break
 ;
Default:
 n code that executes when Case 1 and 2 are different

How it works: first set the expression n (usually a variable). The value of the subsequent expression is compared to the value of each case in the structure. If there is a match, the code block associated with the case is executed. Use a break to prevent your code from automatically running down one case.

Default keyword

Use the default keyword to specify what to do when the match does not exist:

var day=new Date (). Getday ();
Switch (day)
{case
6:
 x= "Today it's Saturday";
 break;
Case 0:
 x=, "Today it's Sunday";
 break;
Default:
 x= "Looking forward to the weekend";
}

Explanation: Today is not a code snippet that will be executed in Saturday or Sunday.

Comparison of 1.3 if and switch

Switch Case vs. if
   switch case is only used for conditions equal to comparison
   else if it can be
    implicitly converted with any condition if (Boolean (condition)) else if () Convert condition to Boolean
    efficiency slightly lower
   switch case 
    equals comparison without implicit conversion, with a slightly higher efficiency

The above part of the JavaScript in the branch structure is small to share all the content of everyone, hope to give you a reference, but also hope that we support the cloud-dwelling community.

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.