有一个开关语句检查多个情况,而无需穿透。

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

Have a switch statement check multiple cases without fall-through

问题

以下是您要翻译的内容:

有没有办法使开关语句检查并可能分别执行每个案例?

switch (true) {
    case (condition1): // this could be true or not
      // Do thing 1...

    case (condition2): // this could be true or not
      // Do thing 2...

    default: // do this if all others are false
      // Do thing 3...
      break;
}

我希望能够执行thing 1,thing 2,两者或两者都不执行(默认执行thing 3)。

在每个情况下使用break不起作用,因为它无法执行thing 1和thing 2。贯穿不起作用,因为即使只有一个为真,它也会执行所有情况。

开关语句是否有办法实现这一点?是否有更好的方法来做到这一点?

英文:

Is there a way to have a switch statement check and possibly execute each case individually?

switch (true) {
    case (condition1): // this could be true or not
      // Do thing 1...

    case (condition2): // this could be true or not
      // Do thing 2...

    default: // do this if all others are false
      // Do thing 3...
      break;
}

I want to be able to do thing 1, thing 2, both, or neither (default to thing 3).

Using break in each case doesn't work because it can't do thing 1 and thing 2. Fall-through doesn't work because it does all of them even if only one is true.

Is there a way for switch statements to do this? Is there a better way to do this?

答案1

得分: 1

以下是您要翻译的内容:

You could take a series of if statements and call things.

A not recommended use of switch. Just add a break in the last clause of the group.

If you like to have a more dynamic approach, you could add a case clause with a value not equal to the switch value, like false, NaN or Symbol().

case new Symbol:
break

The last two expressions are not equal to any other (not stored) value.

英文:

You could take a series of if statements and call things.

<!-- begin snippet: js hide: false console: true babel: false -->
<!-- language: lang-js -->
const
f = (condition1, condition2) => {
if (condition1) console.log('thing 1');
if (condition1 || condition2) console.log('thing 2');
else console.log('thing 3');
console.log('---');
};

f(false, false); // thing3
f(false, true ); // thing2
f(true,  false); // thing1 thing 2
f(true,  true ); // thing1 thing 2

<!-- language: lang-css -->
.as-console-wrapper { max-height: 100% !important; top: 0; }
<!-- end snippet -->

A not recommended use of switch. Just add a break in the last clause of the group.

If you like to have a more dynamic approach, you could add a case clause with a value not equal to the switch value, like false, NaN or Symbol().

case new Symbol:
    break

The last two expressions are not equal to any other (not stored) value.

<!-- begin snippet: js hide: false console: true babel: false -->
<!-- language: lang-js -->
const
f = (condition1, condition2) => {
switch (true) {
case condition1:
console.log('thing 1');

            case condition2:
                console.log(&#39;thing 2&#39;);
                break;                  // stops falling into default clause

            default:
                console.log(&#39;thing 3&#39;);
        }
        console.log(&#39;---&#39;);
    };
    
f(false, false); // thing3
f(false, true ); // thing2
f(true,  false); // thing1 thing 2
f(true,  true ); // thing1 thing 2

<!-- language: lang-css -->
.as-console-wrapper { max-height: 100% !important; top: 0; }
<!-- end snippet -->

huangapple
  • 本文由 发表于 2023年5月11日 04:17:44
  • 转载请务必保留本文链接:https://go.coder-hub.com/76222268.html
匿名

发表评论

匿名网友

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

确定