简体   繁体   English

内切换开关替代

[英]Switch inside switch alternative

I have this pseudocode: 我有这个伪代码:

switch (true) {
  case (cond_1 < 0):
    return 'c1 < 0';
  case (cond_1 === 0):
    switch (true) {
      case (cond_2 < 0):
        return 'c2 < 0'
      case (cond_2 === 0):
        return 'c2 === 0'
      default:
        return 'c2 > 0'
    }
  default:
    return 'c1 > 0';
}

Is there a better way to write this code, without using switch inside other switch statement? 是否有更好的方式编写此代码,而无需在其他switch语句中使用switch?

These if/else conditions are more compact, and the flow is easier to read: 这些if/else条件更加紧凑,流程更易于阅读:

if (cond_1 < 0) return 'c1 < 0';
if (cond_1 === 0) {
  if (cond_2 < 0) return 'c2 < 0';
  if (cond_2 === 0) return 'c2 === 0';
  if (cond_2 > 0) return 'c2 > 0';
}
return 'c1 > 0';

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM