在Go中,断开语句是否从开关/选择中断开? [英] In Go, does a break statement break from a switch/select?
问题描述
我知道 switch
/ 选择
语句会在每个案例后自动中断。我想知道,在以下代码中:
pre $ for $ {
switch sometest(){
case 0 :
dosomething()
case 1:
break
default:
dosomethingelse()
}
}
开关
块?
断言,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屋!