简体   繁体   English

来自嵌套for循环的TypeScript中断开关/大小写

[英]TypeScript break switch/case from nested for loop

Essentially, I am looking for the reverse of this question : if I have a for loop nested inside of a switch/case statement, is there a way to break out of the case? 本质上,我正在寻找这个问题的反面:如果我在switch / case语句中嵌套了一个for循环,是否有办法break这种情况? Trivial example: 琐碎的例子:

switch (prompt('Left or right?')) {
  case 'left':
    for (let i = 0; i < 10; ++i) {
      if (/* some condition */) {
        break case 'left' // this isn't valid
      }
    }
    break
  // ...
}

Thanks 谢谢

This is what labels were created for. 这就是创建标签的目的。 Apply the label to what you want to break out of and break the named block. 将标签应用于您想突破并打破命名块的地方。 In this case, the named block is the switch statement: 在这种情况下,命名块是switch语句:

 direction: switch (prompt('Left or right?')) { case 'left': for (var i = 0; i < 10; ++i) { if (i==3) { break direction } } console.log('should not get here (remove "direction" above to test)') break default: break } 

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

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