Branching structure in JavaScript

Source: Internet
Author: User
Tags switch case

When it comes to the branching structure in JavaScript, we have to mention the word Process control, and all of our programs are made up of data and algorithms.
program = data + algorithm
Usually we call the algorithm can be done by "order", "branch", "loop" three kinds of structure to complete the combination.

In the ECMA, some statements (also called Process Control statements, branch structure statements) are defined, essentially defining the primary syntax in ECMAScript, which typically uses one or more keywords to accomplish a given task.

1.1 If statement

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

1 if (condition) 2   {3   true  when executing code 4   }

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

1 if (condition) 2   {3   true  when executing code 4  }5Else  6  {7   true  when executing code 8   }

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

1 if(Condition 1)2 {3When condition 1 istruecode to execute when4 }5 Else if(Condition 2)6 {7When Condition 2 istruecode to execute when8 }9 ElseTen { OneWhen condition 1 and condition 2 are nottruecode to execute when A}

1.2 Switch statement

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

1 Switch(n)2 {3  Case1:4Executing code block 15    Break;6  Case2:7Executing code block 28    Break;9 default:TenN and Case1 and Case2code that executes at different time One}

How it works: first set an 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 break to prevent your code from automatically running down a case.

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

1 varday=NewDate (). GetDay ();2 Switch(Day)3 {4  Case6:5x= "Today it ' s Saturday";6    Break;7  Case0:8x= "Today it ' s Sunday";9    Break;Ten default: OneX= "Looking forward to the Weekend"; A}

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

Comparison of 1.3 if and switch
Switch  Case Else if     Switch  Case only for conditions     that are equal to comparison Else if any condition          available if Else if ()       with implicit conversion, the condition is converted to a Boolean       efficiency slightly     lower           than the equal comparison of the implicit conversion of the switch case, Slightly higher efficiency

Branching structure 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.