JavaScript's Process Control statement switch

Source: Internet
Author: User

A switch statement is a multiple-condition judgment used to compare multiple values for equality.

    varbox = 1; Switch(box) {//Multiple values for judging box equality         Case1: Alert (' One ');  Break;//Break ; To prevent a statement from penetrating         Case2: Alert (' Both ');  Break;  Case3: Alert (' Three ');  Break; default://equivalent to the else in the IF statement, otherwise the meaningAlert (' ERROR '); }

Practice:

    1. Analog Arithmetic (four operations subtraction)

        Keyboard entry (PROMOT) the data that participates in the operation, and what the operator is (subtraction)

Determine what to do according to the input operation

Output results based on operation

varFirstnumber = Prompt ("Please enter the first operand:", "0");//the first operand to receive keyboard entry        varoperator = prompt ("Please enter Operator", "-");//operator to receive keyboard entry        varSecondnumber = Prompt ("Please enter the first operand:", "0");//the first operand to receive keyboard entry        /*Note: The keyboard input is a string, not a numeric type, so when the + method operation, the string is used as a connector, so to convert the keyboard input operand to a numeric type for subtraction there are two scenarios: scenario One: The number of operations to be obtained, fir    Both Stnumber and secondnumber subtract 0, so that the string is implicitly converted to a numeric type (number) so that it can be subtraction by using the Praseint () method to convert the string to a numeric type, which is more professional */Firstnumber=parseint (Firstnumber); Secondnumber=parseint (Secondnumber); Switch(operator) { Case"-": Alert (Firstnumber-secondnumber);  Break;  Case"+": Alert (Firstnumber+secondnumber);  Break;  Case"*": Alert (Firstnumber*secondnumber);  Break;  Case"/": Alert (Firstnumber/secondnumber); Break; default: Alert ("This operation is not supported at this time."); }    //You can also use if else to implement

JavaScript's Process Control statement switch

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.