break
详情
break 语句用于跳出一层 for 、 while 或 do-while 循环。在嵌套循环中,break 仅跳出其所在的最内层循环。
例子
def printloop(a,b){
for(s in a:b){
print "outer "+string(s)
for(t in a:b){
print "inner "+string(t)
if (mod(t,10)==1){
break
}
}
}
};
printloop(9,15);
// 当t=11时跳出循环内层循环,外层循环继续执行,共执行了6次
while 循环中使用 break 提前退出。
x = 0
while(true) {
x += 1
if (x >= 5) break
}
print x
// output: 5
// while 嵌套循环中 break 仅跳出内层循环
i = 1
while (i <= 3) {
j = 1
while (j <= 5) {
if (j == 3) break // 仅跳出内层 while 循环
print "i=" + i + ", j=" + j
j += 1
}
i += 1
}
// output:
// i=1, j=1
// i=1, j=2
// i=2, j=1
// i=2, j=2
// i=3, j=1
// i=3, j=2
