In Go, does a break statement break from a switch/

2019-01-30 02:15发布

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?

7条回答
地球回转人心会变
2楼-- · 2019-01-30 02:20

Just from a switch block. There's plenty of examples in Golang own code you can examine (compare inner break with outer break).

查看更多
Viruses.
3楼-- · 2019-01-30 02:20

It only exits the switch block.

查看更多
干净又极端
4楼-- · 2019-01-30 02:23

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.

查看更多
等我变得足够好
5楼-- · 2019-01-30 02:33

this should explain it.

for{
    x := 1
    switch {
    case x >0:
        fmt.Println("sjus")
    case x == 1:
        fmt.Println("GFVjk")
    default:
        fmt.Println("daslkjh")
    }
}
}

Runs forever

for{
    x := 1
    switch {
    case x >0:
        fmt.Println("sjus")
        break
    case x == 1:
        fmt.Println("GFVjk")
    default:
        fmt.Println("daslkjh")
    }
}
}

Again, runs forever

BUT

package main

import "fmt"

func main() {
d:
for{
x := 1
    switch {
    case x >0:
        fmt.Println("sjus")
        break d
    case x == 1:
        fmt.Println("GFVjk")
    default:
        fmt.Println("daslkjh")
    }
}
}

will print sjus ... clear ?

http://play.golang.org/p/GOvnfI67ih

查看更多
闹够了就滚
6楼-- · 2019-01-30 02:34

Yes, break breaks the inner switch.

https://play.golang.org/p/SZdDuVjic4

package main

import "fmt"

func main() {

    myloop:for x := 0; x < 7; x++ {
        fmt.Printf("%d", x)
        switch {
        case x == 1:
            fmt.Println("start")
        case x == 5:
            fmt.Println("stop")
            break myloop
        case x > 2:
            fmt.Println("crunching..")
            break
        default:
            fmt.Println("idling..")
        }
    }
}
0idling..
1start
2idling..
3crunching..
4crunching..
5stop

Program exited.
查看更多
We Are One
7楼-- · 2019-01-30 02:35

Another use of break for switch is in combination with fallthrough statement. It might need some creativity to use it properly in correct situation, but it is possibly a usable combination.

Here is simple example:

a := 25
fallThrough := true

switch {
case a > 10 :
    fmt.Println("a>10")
    if fallThrough != true {
        break
    }
    fallthrough
case a > 20:
    fmt.Println("a>20")
}
查看更多
登录 后发表回答