In Go, is the break statement executed from the / select switch?

I know that switch / select switch automatically interrupted after each case. I am wondering in the following code:

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

Does the break statement break a for loop, or just a switch block?

+70
select switch-statement go break
Jun 19 '12 at 15:16
source share
7 answers

Break statements, Go programming language specification .

The break statement completes the innermost for, switch, or select statement.

 BreakStmt = "break" [ Label ] . 

If there is a label, it must be enclosed in β€œfor”, β€œswitch”, or β€œselect”, and this is the one whose execution is completed (Β§ For operators, Β§ Switching operators, Β§ Select statements).

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

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

+103
Jun 19 '12 at 16:41
source share

Hope an illustrative example:

 loop: for { switch expr { case foo: if condA { doA() break // like 'goto A' } if condB { doB() break loop // like 'goto B' } doC() case bar: // ... } A: doX() // ... } B: doY() // .... 
+29
Jun 19 '12 at 15:38
source share

Just from the switching unit. You can find a few examples in the Golang code (compare the internal gap with the external gap ).

+6
Jun 19 2018-12-18T00:
source share

Yes, break breaks the internal switch . Another copy / paste for the playground:

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. 
+3
Sep 04 '16 at 17:54 on
source share

this should explain it.

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

Works forever

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

It starts forever again

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 ... okay?

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

+1
May 02 '14 at 3:56
source share

He just exits the switch block.

0
Jun 19 2018-12-12T00:
source share

Another use of break for a switch is in conjunction with a fallthrough statement. It may take some creativity to use it correctly in the right situation, but it is probably a convenient combination.

Here is a 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") } 
0
Nov 22 '17 at 12:59 on
source share



All Articles