在Go中,断开语句是否从开关/选择中断开? [英] In Go, does a break statement break from a switch/select?

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

问题描述

我知道 switch / 选择语句会在每个案例后自动中断。我想知道,在以下代码中:

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

break 语句是否退出 code> loop或只是开关块?

解决方案


断言,The Go Programming Language Specification。



break语句终止执行最内层的for,
switch或select语句。

  BreakStmt =break[Label]。 

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

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


因此,示例中的 break 语句终止开关语句,即最内层语句。


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()
    }
}

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

解决方案

Break statements, The Go Programming Language Specification.

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

BreakStmt = "break" [ Label ] .

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
      }
  }

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

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

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