break 语句是否从 switch/select 中断? [英] Does a break statement break from a switch/select?

查看:35
本文介绍了break 语句是否从 switch/select 中断?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我知道 switch/select 语句在每种情况下都会自动中断.我想知道,在以下代码中:

I know that switch/select statements break automatically after every case. I am wondering, in the following code:

for {
    switch sometest() {
    case 0:
        dosomething()
    case 1:
        break
    default:
        dosomethingelse()
    }
}

break 语句是退出 for 循环还是仅退出 switch 块?

Does the break statement exit the for loop or just the switch block?

推荐答案

Break 语句,Go 编程语言规范.

break"语句终止最内层for"的执行,切换"或选择"语句.

A "break" statement terminates execution of the innermost "for", "switch" or "select" statement.

BreakStmt = "break" [ Label ] .

如果有标签,它必须是一个封闭的for",switch"或select"语句,即执行终止的语句(§For 语句,§Switch 语句,§Select 语句).

If there is a label, it must be that of an enclosing "for", "switch" or "select" statement, and that is the one whose execution terminates (§For statements, §Switch statements, §Select statements).

L:
  for i < n {
      switch i {
      case 5:
          break L
      }
  }

因此,示例中的 break 语句终止了 switch 语句,即最里面"的语句.

Therefore, the break statement in your example terminates the switch statement, the "innermost" statement.

这篇关于break 语句是否从 switch/select 中断?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆