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