0

I try to execute more than one case in switch statement. I wrote this as a if statement but I read somewhere is the switch is more efficient than if. Now execute always only first correct.

      let num = 30;
      switch (true) {
        case (num >= 20):
          console.log("code for case 1");
          break;
        case (num == 31) :
          console.log("code for case 2");
          break;
        case (num < 50):
          console.log("code for case 3");
          break;
        case (num == 10 + 20):
          console.log("code for case 4");
          break;
      }

Can I execute all true cases? or stay with many if else?

code for case 1
code for case 3
code for case 4
Radeks
  • 13
  • 3
  • It's true that `switch` can be faster than `if-else`, but this is only relevant when the logic of the code is convertible between the two. Planes are faster than submarines, but sometimes you can't take a plane because of, well... complications. – nullromo Mar 06 '23 at 18:05
  • @nullromo - *"It's true that switch can be faster than if-else,"* Not really in JavaScript, and certainly not ones like the above. JavaScript's `switch` is just `if`/`else if`/`else` with different syntax. `case` labels are expressions (not just literals) as you can see in the question, so the C-like optimizations (jump tables, etc.) aren't available. (If the `case` labels **were** literals, then maybe the JavaScript engine could do something with it, but...) – T.J. Crowder Mar 06 '23 at 18:30
  • I see what you mean. I was going based on [this answer](https://stackoverflow.com/a/2923007/4476484). That's why I said "_can be_ faster." – nullromo Mar 06 '23 at 18:31

3 Answers3

2

Switch statements should normally be used when you want to choose one option. There is a "fallthrough" behavior, however.

switch(x) {
    case 1:
    case 2:
        doSomething();
        // no break here!
    case 3:
        doOtherThing();
        break;
    case 4:
        // ...
}

In the above code, if x is 1 or 2, both doSomething() and doOtherThing() will happen, whereas if x is 3, then only doOtherThing() will happen.

If you want to do some more complex combination of operations with conditions, if statements are probably better.

nullromo
  • 2,165
  • 2
  • 18
  • 39
0

Can I execute all true cases?

Not reasonably, no, and moreover it isn't the right tool for that job. Instead of switch, use a series of if statements:

let num = 30;
if (num >= 20) {
    console.log("code for case 1");
}
if (num == 31)  {
    console.log("code for case 2");
}
if (num < 50) {
    console.log("code for case 3");
}
if (num == 10 + 20) {
    console.log("code for case 4");
}
T.J. Crowder
  • 1,031,962
  • 187
  • 1,923
  • 1,875
0

Once you have left a case clause of a switch statement with break, you can not get into the switch statement again.

Not even a fall through, like a bundle of cases without break does not work for your problem to have multiple checks and the same amount of actions.

A solution is to use single check with if and go on with another if

if (condition1) console.log('condition 1');
if (condition2) console.log('condition 2');
// and so on
Nina Scholz
  • 376,160
  • 25
  • 347
  • 392