|REBOL 3 Docs||Guide||Concepts||Functions||Datatypes||Errors|
|TOC < Back Next >||Updated: 17-Aug-2010 Edit History|
Breaks out of a loop, while, until, repeat, foreach, etc.
/return - Forces the loop function to return a value
The break function stops loop functions.
repeat n 5 [ print n if n > 2 [break] ] 1 2 3
The current loop is immediately terminated and evaluation resumes after the repeat function.
The break /return refinement will return a value from a loop. It is commonly used to return a specific value or pass to a conditional expression when the loop is terminated early.
Here's an example:
print repeat n 5 [ if n > pi [break/return n] none ] 4
An example using foreach :
values: [8:30 breakfast 12:00 lunch 5:00 dinner] meal: foreach [time event] [ if time > 14:00 [break/return event] none ] probe meal dinner
The break function acts immediately on the "closest block".
Although break can be placed anywhere within the block being repeated, even within a sub-block or function, because break is a function that is not directly bound to the loop, it will break the closest loop, not necessarily the intended loop. This does not affect most programs but could affect custom-made loop functions.
a-loop: func [count block] [loop count block] repeat a 3 [ print a a-loop 4 [break] ] 1 2 3
|TOC < Back Next >||REBOL.com - WIP Wiki||Feedback Admin|