Sign in

docs RPG Reference

Conditional Directives Within a Free-Form Statement

Conditional Directives Within a Free-Form Statement

You can use the /IF, /ELSEIF, /ELSE, and /ENDIF directives within any free-form statement other than a free-form calculation statement.

However, the following rules apply:

  • If a statement begins after an /IF, /ELSEIF, or /ELSE directive, the final semicolon for the statement must be specified before the next directive.

    The following code is not valid. The DSPLY statement begins after the /IF directive, so the semicolon for the DSPLY statement must appear before the /ELSE directive.

       /IF DEFINED(TRUE)
           DSPLY
       /ELSE
           print
       /ENDIF
             ('start');

    The following code is valid. The DSPLY statement begins after the /IF directive, and the semicolon for the DSPLY statement appears before the /ELSE directive, so the entire DSPLY statement is specified between the /IF and /ELSE directives. Similarly, the entire call to print is specified between the /ELSE and /ENDIF directives.

       /IF DEFINED(TRUE)
           DSPLY ('start');
       /ELSE
           print ('start');
       /ENDIF
  • When the /IF for a conditional group begins within a statement, the /ENDIF must be specified before the final semicolon for the statement.

The following is not valid because the /IF directive is specified after the DCL-S statement begins, and the /ENDIF directive appears after the final semicolon for the DCL-S statement.

   DCL-S name
     /IF DEFINED(TRUE)
       CHAR(10);
     /ELSE
       VARCHAR(10);
     /ENDIF

The following is valid because the entire conditional group is within the statement. The semicolon for the statement appears after the /ENDIF.

   DCL-S name
     /IF DEFINED(TRUE)
       CHAR(10)
     /ELSE
       VARCHAR(10)
     /ENDIF
    ;