86

Is there a way to assign two different case values to the same block of code without copy and pasting? For example, below 68 and 40 should execute the same code, while 30 is not related.

case 68:
   //Do something
break;

case 40:
   //Do the same thing
break;

case 30:
   //Do something different
break;

Is it incorrect to think something like this should work (even though it obviously doesn't)?

case 68 || 40:
   //Do something
break;

case 30:
   //Do something else
break;
1

6 Answers 6

243

Just put them right after each other without a break

switch (myVar) {
  case 68:
  case 40:
    // Do stuff
  break;

  case 30:
    // Do stuff
  break;
}
Sign up to request clarification or add additional context in comments.

5 Comments

Sonar finds this as innapropiate, ¿another option?
@IgnacioAra This question is targetted for Javascript. (although the same code works for C# too)
@Steven Yes, I know, Sonar is available for many languages (sonarqube.org)
@IgnacioAra I see, I looked up some information about cases and it seems fine using them as exception: sbforge.org/sonar/rules/show/squid:S128?layout=false
I see what you mean, I hope they'll fix it soon detecting it as an exception (in Sonar tools). Thanks!
24

Yes, you just put the related case statements next to each other, like this:

case 40:  // Fallthrough
case 68:
   // Do something
   break;

case 30:
   // Do something different
   break;

The Fallthrough comment is there for two reasons:

  • It reassures human readers that you're doing this deliberately
  • It silences warnings from Lint-like tools that issue warnings about possible accidental fallthrough.

2 Comments

will this be a "and" case or a "or case" if we add multiple cases like this
I don't know what you mean by "and case". The value can't be both 40 and 68 at the same time. If the value is 40 the code will "Do something". If the value is 68 it will "Do something". If the value is 30 it will "Do something different".
5
case 68:
case 40:
  // stuff
  break;

Comments

5

Cleaner way to do that 👌

if ([68, 48, 22, 53].indexOf(value) > -1)
    //Do something
else if ([44, 1, 0, 24, 22].indexOf(value) > -1)
    //Do another

You can do that for multiple values with the same result

Comments

0

Switch cases can be clubbed as shown in the dig.

Also, It is not limited to just two cases, you can extend it to any no. of cases.

1 Comment

While this link may answer the question, it is better to include the essential parts of the answer here and provide the link for reference. Link-only answers can become invalid if the linked page changes
-16

You should use:

switch condition {
  case 1,2,3:
    // do something
  case 4,5:
    // do something
  default:
    // do something
}

Cases should be comma-separated.

1 Comment

This is simply wrong. You can't have multiple values in a switch case

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.