if  ( fl -- )

published: 28 January 2023 / updated 28 January 2023

Lire cette page en français

 

vocabulary: forth

The word IF is of immediate execution.

IF marks the beginning of a control structure of type IF..THEN or IF..ELSE..THEN.

At runtime, the definition part between IF and THEN or between IF and ELSE is executed if the boolean flag at the top of the data stack is true (f<>0).

Otherwise, if the boolean flag is false (f=0), the definition part located between ELSE and THEN will be executed. If there is no ELSE, execution continues after THEN.

Example:

: Nice? ( fl ---) 
    if 
        ." Nice weather " 
    else 
        ." Cloudy weather " 
    then 
  ; 
1 Nice?    \ display: Nice weather 
0 Nice?    \ display: Cloudy weather