Does a break statement break from a switch/select?

后端 未结 7 1262
不知归路
不知归路 2020-12-04 07:57

I know that switch/select statements break automatically after every case. I am wondering, in the following code:

for {
    switch          


        
7条回答
  •  广开言路
    2020-12-04 08:15

    A hopefully 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()
    // ....
    

提交回复
热议问题