breakforthflow-control

How exit a BEGIN loop in Forth?


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?


Solution

  • Typically, the break statement 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":

    : x ( -- )
      10 0 ?do
        i .
        i 5 = if ." break " leave then
      loop ."  end "
    ;
    

    To terminate a begin loop, you can use while — but without any nesting, and without specifying any action on termination:

    : x ( -- )
      0 begin
        dup .
        dup 5 = dup if ." break " then 0=
      while
        1+
      repeat drop
      ." end "
    ;
    

    Another option is to use a quotation and exit inside it:

    : x ( -- )
      0 [: begin
        dup .
        dup 5 = if ." break " exit then
        1+
      again ;] execute drop
      ." end "
    ;
    

    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:

    : x ( -- )
      0 repeat{
        dup .
        dup 5 = if-break{ ." break " }
        1+
      }repeat drop ." end "
    ;
    

    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.