Does a "break" Statement Exit a Switch/Select Block or the Loop?
The "break" statement in Go is used to terminate the execution of the innermost enclosing "for", "switch", or "select" statement.
In the provided code snippet:
for { switch sometest() { case 0: dosomething() case 1: break default: dosomethingelse() } }
The "break" statement will only exit the innermost enclosing "switch" block. This is because the "switch" statement is the innermost enclosing statement containing the "break."
According to the Go Programming Language Specification:
A "break" statement terminates execution of the innermost "for", "switch" or "select" statement.
Therefore, the "break" statement will exit the "switch" block, not the outer "for" loop.
The above is the detailed content of Does `break` Exit a Switch or the Surrounding Loop in Go?. For more information, please follow other related articles on the PHP Chinese website!