next | previous | forward | backward | up | index | toc

# break -- break from a loop

## Synopsis

• Usage:
break x
• Consequences:
• this interrupts execution of a loop controlled by for, while, apply, or scan, returning x as the value of the loop currently being evaluated.
• Alternatively, as a top level command in the debugger, it leaves the debugger, returning the user to top level.

## Description

Omitting x, and executing break, interrupts execution of a loop as above, returning null as the value of the function currently being evaluated, except, in the case of a for loop or a while loop with a list clause, the list accumulated so far is returned as the value.

 i1 : for i from 1 to 10 do if i == 7 then break 12345 o1 = 12345

Warning: trying to break from a loop controlled by table will probably not do what you expect, since table is implemented by two nested loops controlled by apply, and only the inner one will stop, as in the following example.

 i2 : table(3,3,(i,j) -> if i == 1 then break 3 else "hi") o2 = {{hi, hi, hi}, 3, {hi, hi, hi}} o2 : List i3 : table(3,3,(i,j) -> if j == 1 then break 3 else "hi") o3 = {3, 3, 3} o3 : List

Here is an example as a debugger command.

 i4 : load "Macaulay2Doc/demo1.m2" i5 : g 2 /usr/local/share/Macaulay2/Macaulay2Doc/demo1.m2:8:12:(3):[2]: error: division by zero /usr/local/share/Macaulay2/Macaulay2Doc/demo1.m2:8:12:(3):[2]: --entering debugger (type help to see debugger commands) /usr/local/share/Macaulay2/Macaulay2Doc/demo1.m2:8 :10-8:12: --source code: b := 1/x; ii6 : x oo6 = 0 ii7 : break i8 : g 3 7 o8 = - 2 o8 : QQ