本文介绍了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 语句是否退出 循环还是 switch 块?

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

推荐答案

break语句终止执行最里面的 for,
switch或 select语句。

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

BreakStmt = "break" [ Label ] .

如果有标签,则必须为 for, switch $的标签b $ b或 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 语句终止开关语句,即最里面的语句。

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

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

08-24 02:45