In many languages there is the BREAK statement, which allows you to escape an iterative cycle.
I can't find a word to escape a BEGIN-AGAIN or BEGIN-UNTIL cycle in Forth
I want to implement this code
def x():
for i in range(10):
print(i, end=' ')
if i == 5:
print("break", end=' ')
break
print("end")
that produces
0 1 2 3 4 5 break end
Try it using EXIT (gforth 0.7.3)
: x 0 begin
dup dup .
5 = if
."break" exit
then
1+
0
until
." end" ;
but it never prints "end":
x 0 1 2 3 4 5 break ok
I understand that EXIT is really a RETURN in disguise, so one alternative is to put the BEGIN-UNTIL segment in a separate word, but I prefer to maintain the code together.
How do you escape a BEGIN cycle?
Typically, the
breakstatement can be used in any level of control-flow nesting and it terminates the nearest enclosing loop (if labels are not used).In standard Forth, only DO-LOOP loop have a similar statement "
leave":To terminate a
beginloop, you can usewhile— but without any nesting, and without specifying any action on termination:Another option is to use a quotation and
exitinside it:Also, in Forth, other control-flow construct can be defined using the standard ones. For example, see my control-flow-curly.fth. It allows to specify actions on termination the loop as follows:
Once this file is downloaded, it can be included into a Forth program or system as
include control-flow-curly.fth(specify an absolute or relative path, if needed), or via command line.