break

Details

Terminate a loop.

The break statement is used to exit one level of for, while, or do-while loop. In nested loops, break exits only the innermost loop that contains it.

Examples

def printloop(a,b){
   for(s in a:b){
       print s
       if (mod(s,10)==1)
           break
   }
};

printloop(9,15);
/* output
9
10
11
*/

// It jumps out of the inner loop when t=11. But the outer loop continues executing, which has been executed 6 times totally.

Use break in a while loop to exit early.

x = 0
while(true) {
    x += 1
    if (x >= 5) break
}
print x
// output: 5

// In nested while loops, break exits only the inner loop
i = 1
while (i <= 3) {
    j = 1
    while (j <= 5) {
        if (j == 3) break  // Exit only the inner while loop
        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