First the data, then the code.
This Forth ground rule is also valid for conditional branching in programs.
A flag on the stack is the 'data' for
the 'operators'
IF or
UNTIL,
who decide if there is to be a jump or not.
In many programming languages it goes like this:
IF \ Decide flag value of 'cold'.
cold \ Formulate flag
THEN \ Leave construct when flag=false
coat-on \ Conditionally executed code
ENDIF \ End; could be named ENDTHEN
In Forth, deciding the flag value is not part of the construct.
Only the operator
IF and its closure
THEN are present.
The programmer is to make sure a flag is waiting:
( cold? \ flag on stack )
IF \ On 'false', jump to THEN
coat-on \ Conditionally executed code
THEN \ End; could be named ENDIF
The backward jump with
UNTIL has its counterpart in Forth:
BEGIN \ Begin
... warm? \ The programmer makes sure
\ that this code produces a flag.
UNTIL \ On 'false', jump back to BEGIN
( coat-off )
These constructs can be nested. When done cleanly, e.g.
... BEGIN ... IF ... THEN ... UNTIL ...
this is called structured programming.
In
... BEGIN ... IF ... UNTIL ... THEN ...
Forth will protest when encountering
UNTIL,
because the
IF has not finished yet.
In
... IF ... IF ... THEN ... THEN ...
the first
THEN attaches to the second
IF.