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