5.8.4 Arbitrary control structures

ANS Forth permits and supports using control structures in a non-nested
way. Information about incomplete control structures is stored on the
control-flow stack. This stack may be implemented on the Forth data
stack, and this is what we have done in Gforth.

An orig entry represents an unresolved forward branch, a dest
entry represents a backward branch target. A few words are the basis for
building any control structure possible (except control structures that
need storage, like calls, coroutines, and backtracking).

The Standard words CS-PICK and CS-ROLL allow you to
manipulate the control-flow stack in a portable way. Without them, you
would need to know how many stack items are occupied by a control-flow
entry (many systems use one cell. In Gforth they currently take three,
but this may change in the future).

Some standard control structure words are built from these words:

ELSEcompilation orig1 – orig2 ; run-time – core “ELSE”

WHILEcompilation dest – orig dest ; run-time f – core “WHILE”

REPEATcompilation orig dest – ; run-time – core “REPEAT”

Gforth adds some more control-structure words:

ENDIFcompilation orig – ; run-time – gforth “ENDIF”

?DUP-IFcompilation – orig ; run-time n – n| gforth “question-dupe-if”

This is the preferred alternative to the idiom "?DUP IF", since it can be
better handled by tools like stack checkers. Besides, it's faster.

The standard does not allow using CS-PICK and CS-ROLL on
do-sys. Gforth allows it, but it's your job to ensure that for
every ?DO etc. there is exactly one UNLOOP on any path
through the definition (LOOP etc. compile an UNLOOP on the
fall-through path). Also, you have to ensure that all LEAVEs are
resolved (by using one of the loop-ending words or DONE).

5.8.4.1 Programming Style

In order to ensure readability we recommend that you do not create
arbitrary control structures directly, but define new control structure
words for the control structure you want and use these words in your
program. For example, instead of writing: