Sign in

docs RPG Reference

Example of Defining Literals

Example of Defining Literals

Examples of literals used to initialize fields

  1. The date format for the module is set to *ISO using the DATFMT on a Control statement. All date literals must be specified with this format.
  2. DateField is a Date field defined with the default format for the module, *ISO. It is initialized with a Date literal.
  3. NumField is a numeric field with Packed format. It has 5 digits and 1 decimal place. It is initialized with a numeric literal, 5.2, which only has 2 digits with 1 decimal place. The variable is actually initialized with the value 0005.2.
  4. CharField is an alphanumeric field with 10 characters. It is initialized with a character literal.
  5. YmdDate is a Date field defined with *YMD format, YY/MM/DD. Although the date field is defined with a 2-digit year, the initialization value must be defined with a 4-digit year in *ISO format, since all literals must be specified in the date format specified on the control specification.
CTL-OPT DATFMT(*ISO);                        //  1 

DCL-S DateField DATE INZ(D'1988-09-03');     //  2 
DCL-S Numfield PACKED(5:1) INZ(5.2);         //  3 
DCL-S CharField CHAR(10) INZ('abcdefghij');  //  4 
DCL-S YmdDate DATE(*YMD) INZ(D'2001-01-13'); //  5 

Defining and using named constants

A named constant is a symbolic name assigned to a literal.

  1. DateConst, NumConst, and CharConst are names for the literals specified by the CONST keyword.
  2. The literal for the named constant can be specified with or without the CONST keyword.
  3. The literal can be continued on the next line if necessary. See Continuation Rules.
  4. Several variables are initialized using named constants.
DCL-C DateConst CONST(D'1988-09-03');     //  1 
DCL-C NumConst CONST(5.2);                //  1 
DCL-C CharConst CONST('abcdefghij');      //  1 

DCL-C Upper 'ABCDEFGHIJKLMNOPQRSTUVWXYZ'; //  2 

DCL-C Lower 'abcdefghijklmn+
             opqrstuvwxyz';               //  3 

DCL-S DateField DATE INZ(DateConst);      //  4 
DCL-S Numfield PACKED(5:1) INZ(NumConst;  //  4 
DCL-S CharField CHAR(10) INZ(CharConst);  //  4 

Examples of literals used in operations

  1. The literal is used in an assignment.
  2. The literals are used in built-in functions.
CharField = 'abc';                                //  1 
IF DateField > %DATE('2020-12-25') + %DAYS(6); //  2 
   DateField = D'2021-12-25';                     //  1 
ENDIF;

Examples of literals used in enumerations

  1. The enumeration is not qualified.
  2. The constant is specified with the name followed by the literal.
  3. The constant is specified with the name followed by the CONST keyword.
  4. The constant is specified with the DCL-C operation code followed by the name followed by the literal. The DCL-C operation code is only required if the name is the name of an operation code allowed in a free-form calculation.
  5. The enumeration is qualified.
  6. The name UNKNOWN is the name of one of the constants in the TRUE_FALSE enumeration, but there is no confusion becaues the STATE enumeration is qualified.
  7. The TRUE_FALSE enumeration is not qualified, so the constant name TRUE is specified without specifying the enumeration name.
  8. The STATE enumeration is qualified, so the constant name CLOSED is qualified by the enumeration name.
DCL-ENUM true_false;         //  1 
   true '1';                 //  2 
   false CONST('0');         //  3 
   DCL-C unknown '?';        //  4 
END-ENUM;

DCL-ENUM state QUALIFIED;    //  5 
   open    0;
   closed  1;
   active  2;
   unknown 100;              //  6 
END-ENUM;
DCL-F myOrderF;
DCL-DS order LIKEREC(myOrderFmt);

if %open(myfile) = true;     //  7 
   ...
endif;

order.status = state.closed; //  8 

Fr more examples of literals in enumerations, see Enumerations and Typed Enumerations.