break语句用于中断发生它的最内部结构。它可以用于for循环(计数器, 条件等)以及开关中。在该结构的结尾}之后继续执行。
句法:-
break;
Go Break语句示例:
package mainimport "fmt"func main() {vara int = 1for a <
10{fmt.Print("Value of a is ", a, "\n")a++;
if a >
5{/* terminate the loop using break statement */break;
}}}
【Go break语句】输出:
Value of a is 1Value of a is 2Value of a is 3Value of a is 4Value of a is 5
Break语句也可以应用在内部循环中, 并且控制流爆发到外部循环中。
带有内循环的中断声明:
package mainimport "fmt"func main() {var a intvar b intfor a = 1;
a <
= 3;
a++ {for b = 1;
b <
= 3;
b++ {if (a == 2 &
&
b == 2) {break;
}fmt.Print(a, " ", b, "\n")}}}
输出:
1 11 21 32 13 13 23 3