[Next][Prev] [Right] [____] [Up] [Index] [Root]

The break statement

The statement

> break;

inside an iterative statement (loop) causes immediate exit from the statement. If several loops are nested then execution jumps out of the innermost enclosing loop.

In a for-statement nested inside another for-statement, the statement

> break IDENTIFIER;

causes an exit from the for-loop whose loop identifier is the given identifier. This allows an exit from a loop other than the innermost one.

The break-statement should be carefully distinguished from the continue-statement (see The continue statement).

Example

> // (inefficient) search for the next perfect number above n
> n := 1000;
> repeat
repeat> n +:= 1;
repeat> if n gt 5000 then
repeat|if> print "exceeded limit";
repeat|if> break;
repeat|if> end if;
repeat> until n eq &+Prune(Divisors(n));
exceeded limit

> p := 10037;
> for x in [1..100] do
for> for y in [1..100] do
for|for> if x^2 + y^2 eq p then
for|for|if> print x, y;
for|for|if> break;
for|for|if> end if;
for|for> end for;
for> end for;
46 89
89 46

> p := 10037;
> for x in [1..100] do
for> for y in [1..100] do
for|for> if x^2 + y^2 eq p then
for|for|if> print x, y;
for|for|if> break x;
for|for|if> end if;
for|for> end for;
for> end for;
46 89

 [Next][Prev] [Right] [____] [Up] [Index] [Root]