Possible Duplicate:
Multiple Cases in Switch:
Is it possible to do a multiple constant-expression switch statement like
switch (i) {
case "run","notrun", "runfaster": //Something like this.
DoRun();
break;
case "save":
DoSave();
break;
default:
InvalidCommand(command);
break;
}
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.
Switch Case The switch cases must be unique constant values. It can be bool, char, string, integer, enum, or corresponding nullable type.
constant1 , constant2 and so on following the case keywords must be of integer type (like int , long int etc ) or character type. It can also be an expression which yields an integer value. Each case statement must have only one constant.
Yes, it is. You can use multiple case labels for the same section:
switch (i) { case "run": case "notrun": case "runfaster": DoRun(); break; case "save": DoSave(); break; default: InvalidCommand(command); break; }
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With