英文:
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 >= 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
答案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 >= 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");
}
<!-- end snippet -->
答案3
得分: 0
一旦你在switch语句的一个case子句中使用了break,就无法再次进入switch语句。
甚至穿透,就像一组没有break的case一样,也不能解决你的问题,无法进行多次检查并执行相同数量的操作。
一个解决办法是使用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('condition 1');
if (condition2) console.log('condition 2');
// and so on
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。


评论