📅  最后修改于: 2021-01-02 09:07:00             🧑  作者: Mango
break语句用于中断发生它的最内部结构。它可以用于for循环(计数器,条件等)以及开关中。在该结构的结尾}之后继续执行。
句法:-
break;
中断声明示例:
package main
import "fmt"
func main() {
var a int = 1
for a < 10{
fmt.Print("Value of a is ",a,"\n")
a++;
if a > 5{
/* terminate the loop using break statement */
break;
}
}
}
输出:
Value of a is 1
Value of a is 2
Value of a is 3
Value of a is 4
Value of a is 5
Break语句也可以应用在内部循环中,并且控制流爆发到外部循环中。
带有内循环的中断声明:
package main
import "fmt"
func main() {
var a int
var b int
for a = 1; a <= 3; a++ {
for b = 1; b <= 3; b++ {
if (a == 2 && b == 2) {
break;
}
fmt.Print(a, " ", b, "\n")
}
}
}
输出:
1 1
1 2
1 3
2 1
3 1
3 2
3 3