Go to the first, previous, next, last section, table of contents.


Selection

flag
IF
  code
ENDIF

or

flag
IF
  code1
ELSE
  code2
ENDIF

You can use THEN instead of ENDIF. Indeed, THEN is standard, and ENDIF is not, although it is quite popular. We recommend using ENDIF, because it is less confusing for people who also know other languages (and is not prone to reinforcing negative prejudices against Forth in these people). Adding ENDIF to a system that only supplies THEN is simple:

: endif   POSTPONE then ; immediate

[According to Webster's New Encyclopedic Dictionary, then (adv.) has the following meanings:

... 2b: following next after in order ... 3d: as a necessary consequence (if you were there, then you saw them).

Forth's THEN has the meaning 2b, whereas THEN in Pascal and many other programming languages has the meaning 3d.]

Gforth also provides the words ?dup-if and ?dup-0=-if, so you can avoid using ?dup. Using these alternatives is also more efficient than using ?dup. Definitions in plain standard Forth for ENDIF, ?DUP-IF and ?DUP-0=-IF are provided in `compat/control.fs'.

n
CASE
  n1 OF code1 ENDOF
  n2 OF code2 ENDOF
  ...
ENDCASE

Executes the first codei, where the ni is equal to n. A default case can be added by simply writing the code after the last ENDOF. It may use n, which is on top of the stack, but must not consume it.


Go to the first, previous, next, last section, table of contents.