Next: breakall Up: MacAnova Help File Previous: boxplot()   Contents

break

Usage:
for(i,run(n)){if(x[i] < 0){break} .... }
for(i,run(n)){for(j,run(m)){if(x[i,j] < 0){break 2} .... }}



Keywords: control, syntax
'break' and 'break n' are used to exit prematurely from one or more
enclosing loop, perhaps because an error has been found.

'break', in a 'for' or 'while' loop, exits the loop, skipping any
remaining commands in the loop and resuming execution immediately after
the '}' terminating the loop.  When more than one loop "encloses"
'break', only the innermost one is exited.

'break n', where n is a positive integer, exits from n enclosing 'for'
or 'while' loops.  For example, 'break 1' is equivalent to 'break' and
will exit the current loop; 'break 2' will exit the current loop and the
loop enclosing it; and so on.  n must be a literal integer ('1', '2',
...)  and not a variable with integer value.

It is an error to use 'break' outside of a loop or to use 'break n' when
not enclosed in at least n loops.

In an evaluated string or out-of-line macro, 'break' and 'break n' can
be used only to exit from a loop that started in the macro or evaluated
string.  It is an error to try to exit from a loop that started outside
the macro or evaluated string.  See evaluate() and 'macros'.

Using 'break' in an in-line macro to exit from a loop that started
outside the macro will work, but is a bad programming practice.

Inside a macro, break n with the appropriate value of n should always be
used instead of breakall.

Examples:
  for(i,run(100)){... compute x ...;if(x<0){print("x < 0");break;};...;}
If x ever becomes negative, the 'for' loop is terminated.
  for(i,run(10)){for(j,run(5)){...;if(x<0){break 2}}}
If x ever becomes negative, both 'for' loops are terminated.

See also topics 'if', 'for', 'while', 'breakall', 'next', batch().


Gary Oehlert 2003-01-15