多个案例执行切换

huangapple go评论60阅读模式
英文:

Switch multiple case execution

问题

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;
}
英文:

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 &gt;= 20):
          console.log(&quot;code for case 1&quot;);
          break;
        case (num == 31) :
          console.log(&quot;code for case 2&quot;);
          break;
        case (num &lt; 50):
          console.log(&quot;code for case 3&quot;);
          break;
        case (num == 10 + 20):
          console.log(&quot;code for case 4&quot;);
          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

答案1

得分: 2

switch语句通常用于选择一个选项。然而,它有一种“穿透”行为。

switch(x) {
    case 1:
    case 2:
        做某事();
        // 这里没有break!
    case 3:
        做其他事情();
        break;
    case 4:
        // ...
}

在上面的代码中,如果 x 是 1 或 2,做某事()做其他事情() 都会发生,而如果 x 是 3,那么只会发生 做其他事情()

如果你想要使用条件进行一些更复杂的操作组合,if 语句可能更好。

英文:

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.

答案2

得分: 0

不合理,不是正确的工具。请改用一系列if语句:

let num = 30;
if (num >= 20) {
    console.log("case 1 的代码");
}
if (num == 31)  {
    console.log("case 2 的代码");
}
if (num < 50) {
    console.log("case 3 的代码");
}
if (num == 10 + 20) {
    console.log("case 4 的代码");
}
英文:

> 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:

<!-- begin snippet: js hide: false console: true babel: false -->

<!-- language: lang-js -->

let num = 30;
if (num &gt;= 20) {
    console.log(&quot;code for case 1&quot;);
}
if (num == 31)  {
    console.log(&quot;code for case 2&quot;);
}
if (num &lt; 50) {
    console.log(&quot;code for case 3&quot;);
}
if (num == 10 + 20) {
    console.log(&quot;code for case 4&quot;);
}

<!-- end snippet -->

答案3

得分: 0

一旦你在switch语句的一个case子句中使用了break,就无法再次进入switch语句。

甚至穿透,就像一组没有breakcase一样,也不能解决你的问题,无法进行多次检查并执行相同数量的操作。

一个解决办法是使用if进行单一检查,然后继续使用另一个if

if (condition1) console.log('condition 1');
if (condition2) console.log('condition 2');
// 等等
英文:

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(&#39;condition 1&#39;);
if (condition2) console.log(&#39;condition 2&#39;);
// and so on

huangapple
  • 本文由 发表于 2023年3月7日 01:54:02
  • 转载请务必保留本文链接:https://go.coder-hub.com/75654200.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定