JS Branch Structure Switch--case

Source: Internet
Author: User

Switch...case ... Multiple branches, depending on the condition, select Execute

Syntax:     Switch (expression)        {case-  expression 1:             code snippet 1;
Break ; Case expression N: code snippet N;
Break
Default
Default execution code;

Operating mechanism: If a case is found that matches the value of the expression, not only the code under the current cases is executed, but all the code is then triggered!

Break : exits the current structure;

When multiple conditions are common with the same set of execution logic, no break is added;

<! DOCTYPE html>//Please enter a score:            //if the score>=90 output is excellent            //if the score>=80 output is excellent            //if the score>=60 output C            //otherwise output d            varScore = parseint (Prompt ("Please enter fractions"))); Switch(true){                 CaseScore>=90:                 CaseScore>=80: Console.log (Excellent);  Break;  CaseScore>=60: Console.log (C);  Break; default: Console.log (D);  Break; }        </script> </body>

The implementation of the above switch...case can also be implemented using the If...else If...else method, as follows:

<! DOCTYPE html>varScore = parseint (Prompt ("Please enter fractions"))); if(score>=90) {Console.log (A); }Else if(score>=80) {Console.log (B); }Else if(score>=60) {Console.log (C); }Else{Console.log (D); }        </script> </body>

The following examples should be noted below:

Switch...case is congruent = = = strictly equal without automatic type conversion

If Num===1, perform

If num===2, perform

If num===3, perform

<! DOCTYPE html>varNum=parseint (Prompt ("Please select service type"))); Switch(num) { Case1://if Num===1Console.log ("In Balance query");  Break;  Case2://if num===2Console.log ("Withdrawal in progress");  Break;  Case3://if Num===3Console.log ("Transfer in Progress");  Break; default: Console.log ("Exit System");  Break; }        </script> </body>
// Modify: If you remove parseint  Quote ' case ' 1 ' :    case ' 2 ':

The modified code looks like this:

varNum=prompt ("Please select the type of service"); Switch(num) { Case' 1 '://if Num===1Console.log ("In Balance query");  Break;  Case' 2 '://if num===2Console.log ("Withdrawal in progress");  Break;  Case' 3 '://if Num===3Console.log ("Transfer in Progress");  Break; default: Console.log ("Exit System");  Break; }        </script> </body>

JS Branch Structure Switch--case

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.