Example of Defining Literals
Example of Defining Literals
Examples of literals used to initialize fields
- 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.
- DateField is a Date field defined with the default format for the module, *ISO. It is initialized with a Date literal.
- 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.
- CharField is an alphanumeric field with 10 characters. It is initialized with a character literal.
- 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.
- DateConst, NumConst, and CharConst are names for the literals specified by the CONST keyword.
- The literal for the named constant can be specified with or without the CONST keyword.
- The literal can be continued on the next line if necessary. See Continuation Rules.
- 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
- The literal is used in an assignment.
- 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
- The enumeration is not qualified.
- The constant is specified with the name followed by the literal.
- The constant is specified with the name followed by the CONST keyword.
- The constant is specified with the
DCL-Coperation code followed by the name followed by the literal. TheDCL-Coperation code is only required if the name is the name of an operation code allowed in a free-form calculation. - The enumeration is qualified.
- 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.
- The TRUE_FALSE enumeration is not qualified, so the constant name TRUE is specified without specifying the enumeration name.
- 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.