Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Switch case with multiple values for the same case

Tags:

I would like to know the syntax to set a multiple case statement in a switch / case.
For example :

String commentMark(int mark) {     switch (mark) {         case 0 : // Enter this block if mark == 0             return "Well that's bad" ;         case 1, 2, 3 : // Enter this block if mark == 1 or mark == 2 or mark == 3             return "Gods what happend" ;         // etc.         default :             return "At least you tried" ;     } } 

I cannot find the right syntax to set multiple case (the line case 1, 2, 3 :), is it even possible in Dart ?

I did not found any informations on pub.dev documentation, neither on dart.dev.

I tried :
case 1, 2, 3
case (1, 2, 3)
case (1 ; 2 ; 3)
case (1 : 2 : 3)
case 1 : 3
and more !

like image 605
Mathieu Avatar asked Nov 12 '19 11:11

Mathieu


People also ask

Can switch case have multiple values?

The switch can includes multiple cases where each case represents a particular value. Code under particular case will be executed when case value is equal to the return value of switch expression. If none of the cases match with switch expression value then the default case will be executed.

Can case in Java have multiple values?

The case values should be unique and can't be duplicated. If there is a duplicate value, then it is a compilation error. The data type of the values for a case must be the same as the data type of the variable in the switch test expression. The values for a case must be constant or literal types.

Can switch case have multiple conditions in C?

A switch statement—or simply a case statement—is a control flow mechanism that determines the execution of a program based on the value of a variable or an expression. Using a switch statement allows you to test multiple conditions and only execute a specific block if the condition is true.


1 Answers

Execution continues until it reaches a break;. Therefore, you can list cases one after the other to get the following code execute on either one of those cases.

String commentMark(int mark) {     switch (mark) {         case 0 : // Enter this block if mark == 0             return "mark is 0" ;         case 1:         case 2:         case 3: // Enter this block if mark == 1 or mark == 2 or mark == 3             return "mark is either 1, 2 or 3" ;         // etc.         default :             return "mark is not 0, 1, 2 or 3" ;     } }  

The return statements above serve to get out of the function. If you do not want to return, you have to use break; after each block, of course. This code below is equivalent to the one above.

String commentMark(int mark) {     String msg;     switch (mark) {         case 0 : // Enter this block if mark == 0             msg = "mark is 0" ;             break;         case 1:         case 2:         case 3: // Enter this block if mark == 1 or mark == 2 or mark == 3             msg = "mark is either 1, 2 or 3" ;             break;         // etc.         default:             msg = "mark is not 0, 1, 2 or 3" ;             break; // this is a good habit, in case you change default to something else later.     }     return msg; }  
like image 108
Gazihan Alankus Avatar answered Oct 21 '22 12:10

Gazihan Alankus