CONSTANTS AND LITERAL VALUES


Constants are verbatim values placed in the program. It's often advantageous—for example, when you compare the balance due for an account to zero—to specify a value as a literal or constant.

The length of literal values can be as long as is permitted by the specification position where they are being used. When a literal value is assigned a name, it is referred to as a named constant and is often shortened to the simpler constant. However, the length of a character constant cannot exceed 1,024 characters; numeric constants cannot exceed 33 characters (31 digits plus the decimal notation and sign); and hexadecimal literal values cannot exceed 2,051 positions (2,048 hexadecimal characters plus the leading capital letter X and two apostrophes). There are three types of constants:

  • Figurative constants.

  • Named constants.

  • Literal values.

Figurative constants represent compiler-defined values. Named constants and literal values, however, can be one of the following:

  • Character literal value.

  • Numeric literal value.

  • Hexadecimal literal value.

Character literal values must be enclosed in apostrophes. For example:

    'This is a literal value' 

Numeric literal values are not enclosed in apostrophes, and may contain decimal notation and a sign. If a sign is specified, it must appear to the left of the numeric value. For example:

    –3.1415962 

When the actual literal value includes an apostrophe, as in the literal O'clock, two consecutive apostrophes must be specified and the entire literal must be enclosed in apostrophes. For example, the literal value O'clock would be specified as 'O''clock'. By doubling the apostrophe O''clock and enclosing the literal value in apostrophes, the resulting value specified in the program should be 'O''clock'.

Numeric constants referenced for their numeric value can contain a decimal notation character. In the United States, the period (.) is used; in other countries the comma (,) is used for decimal notation. The DECFMT keyword, specified on the header specification, is used to specify the decimal notation character.

The sign for all numeric constants can precede the constant. For example -12 is a valid negative numeric constant, and +12 is a valid positive numeric constant. The plus sign (+) for positive numeric values (i.e., unary plus) is implicit and, therefore, seldom specified.

Hexadecimal literal values begin with a capital letter X and must be enclosed in apostrophes. There must be an even number of hexadecimal characters enclosed in apostrophes. The hexadecimal characters can be A, B, C, D, E, F; or a, b, c, d, e, f; or 0, 1, 2, 3, 4, 5, 6, 7, 8, 9. For example:

    X'1Face2Face' 

The examples listed in Table 1.9 illustrate several valid and invalid literal values.

Table 1.9: Examples of Literal Values

Literal Value

Status

Description

123456.78

Valid

Eight-digit numeral with two decimal positions.

-55555

Valid

Five-digit negative numeral.

'O''clock'

Valid

Seven-position character constant O'clock.

'1'

Valid

One-position character constant 1.

'Banyan'

Valid

Six-position character constant Banyan.

X'F100B1'

Valid

Hexadecimal literal.

O'Clock

Invalid

Missing apostrophes. Should be 'O''clock'.

100.00-

Invalid

Invalid sign. Should be -100.00.

Deforestation

Invalid

Missing apostrophes. Should be 'Deforestation'.

32,767.00

Invalid

Thousands notation is not allowed in numeric literal values.

Figurative Constants

Figurative constants are special, built-in names that have a predefined value associated with them. For example, *BLANKS can be used in place of quoted blanks and *ZEROS can be used in place of zeros. All figurative constant names begin with an asterisk (*).

Figurative constants can be specified in factor 1 and factor 2 of calculation specifications. The value that a figurative constant represents is implied and equal to that of the complementary field. For example, if *ZEROS is used with a seven-digit numeric field, its implied value is 0000000. If *ZEROS is used with a five-position character field, its implied value is '00000'. The implied value is unique to the program statement using the figurative constant. A description of each figurative constant is listed in Table 1.10.

Table 1.10: Figurative Constants

Figurative Value

Type of Value

Description

*ALL'...'

Figurative Constant

Repeating pattern. Automatically adjusts to the size of the field to which it is being compared or moved. For example: *ALL'abcd' moves 'abcdabcd', etc., to the result field for the length of the result field.

*ALLX'...'

Figurative Constant

Repeating hexadecimal pattern. Automatically adjusts to the size of the field to which it is being compared or moved. For example: *ALLX'00' moves binary zeros to the result field for the length of the result field.

*ALLG'SO...SI'

Figurative Constant

Repeating graphic character set (DBCS) pattern. Automatically adjusts to the size of the corresponding DBCS field.

*ALLU'XXXXYYYY'

Figurative Constant

Repeating USC-2 pattern. Repeats the USC-2 literal, automatically adjusting the length to match that of the field to which it is being compared or moved.

*BLANK

*BLANKS

Figurative Constant

Blanks. Automatically adjusts to the size of the field to which it is being compared or moved.

*HIVAL

Figurative Constant

Represents the highest possible valid value for the data type to which it is being compared or moved.

*LOVAL

Figurative Constant

Represents the lowest possible valid value for the data type to which it is being compared or moved.

*NULL

Figurative Constant

Use to set or compare pointer data types to "no value." Currently, RPG supports *NULL with the pointer data type.

*OFF

Figurative Constant

Logical off ('0'). Functionally similar to *ALL'0'. Typically, *OFF is used with the IFxx and MOVE operations to test or set the status of an indicator.

*ON

Figurative Constant

Logical on ('1'). Functionally similar to *ALL'1'. Typically, *ON is used with the IFxx and MOVE operations to test or set the status of an indicator.

*OMIT

Figurative Constant

Use this value on the result field of a parameter for a CALLB operation or a parameter of a CALLP operation to indicate that the parameter is omitted from the parameter list. To test for a *OMIT value, compare the parameter's address to *NULL.

*ZERO

*ZEROS

Figurative Constant

Use to represent a repeating pattern of zeros. Can be used with any data type except pointer, date, time, and timestamp variables.

Named Constants

Named constants are programmer-defined literal values with a unique name assigned to them. The name allows long literal values to be used in places like factor 1 and factor 2 of the calculation specification (where only 14 positions are available) or when a value is used repeatedly throughout the program. The value represented by a named constant cannot be changed when the program is run; the value is constant. The name assigned to the constant is specified on the definition specification. The definition type (positions 24 and 25) must contain the letter C. The constant's value is specified by the CONST keyword in the function/keyword area of the definition specification.

Named constants allow a name to be assigned to literal character values and numeric values. Character named constants up to 1,024 characters and numeric named constants of up to 33 positions (31 digits, a sign, and a decimal point) are supported. Named constants can be specified in factor 1 and factor 2 of calculation specifications as an output field or as an output constant. Named constants also can be used on several definition specification keywords, including INZ, DIM, and OCCURS.

Named constants are not restricted to the length of the location where the constant is used. For example, a named constant representing a 1,024-character literal can be used in factor 1 or factor 2 of the calculation specifications.

In the example featured in Figure 1.10, six named constants are defined. Line 1 defines the MESSAGE named constant. Line 2 defines the named constant NEGNUMBER (negative number), which includes a negative sign and decimal notation. Line 3 defines a simple seven-digit numeral with two decimal places. Line 4 defines a hexadecimal value named REVERSEIMG (reverse image). Line 5 defines the named constant LONGNUMBER (long number), which contains decimal notation. And, lines 7 to 9 define the long named constant LONGMSG (long message).

start figure

 .....DName+++++++++++ETDsFrom+++To/L+++IDc.Functions++++++++++++++++++++++++++++ 0001 D Message         C                   Const('This is a short one') 0002 D NegNumber       C                   Const(-99999.99) 0003 D Short#          C                   Const(38) 0004 D ReverseImg      C                   Const(X'21') 0005 D LongNumber      C                   Const(123456789012345.1234567890123- 0006 D                                     45) 0007 D LongMsg         C                   Const('This is a long named const- 0008 D                                     ant. It spans several lines. It + 0009 D                                        has quotes, and it''s very long.') 0010 D Value           S             7P 2 0011 D BigNumber       S            30P15  .....CSRn01Factor1+++++++OpCode(ex)Factor2+++++++Result++++++++Len++DcHiLoEq.... 0012 C                   Z-ADD     NegNumber     VALUE 0013 C                   Z-ADD     LONGNumber    BigNumber 0014 C    Message        DSPLY       * .       * The program continues... .....OFormat++++DAddn01n02n03Except++++SpbSpaSkbSka 0015 OQPrint E 1 .....O..............n01n02n03Field+++++++++YB?End++PConstant/Editword+++++++++++ 0016 O short# J +2 '$' 0017 O +2 LongMsg 

end figure

Figure 1.10: Examples of named constants

In the preceding example, the Z-ADD operation is used to move the named constant NEGNUMBER to the field VALUE (line 12). The named constant LONG# is moved to the field BIGNUMBER, which is a 30-position numeric field with 15 decimal digits (line 13). The named constant MESSAGE is used as factor 1 of the DSPLY (display) operation (line 14). The named constant SHORT# is used as output with numeric editing (line 16). And the named constant LONGMSG is used as an output constant (line 17).

Reserved Names

Reserved field names are predefined fields that contain specific data such as the date. Some reserved fields begin with an asterisk and some do not. Reserved fields cannot be modified. Table 1.11 lists descriptions of each reserved field name.

Table 1.11: Reserved Field Names

Reserved Field

Description

PAGE

Page number. This four-digit numeric field is used as a page counter. It is incremented each time it is output.

PAGEn

Additional page counters. These four-digit numeric fields are used as additional page counters. PAGEn, where n can be 1 to 7, offers seven additional page counters for a total of eight available page counters.

UDATE[1]

Session date. This numeric field is initialized to the run date when the program is started.

UDAY

Session day. This two-digit numeric field contains the day of the month.

UMONTH

Session month. This two-digit numeric field contains the month.

UYEAR

Session year. This two-digit numeric field contains the year.

[1]Also see *DATE , *MONTH, *DAY, and *YEAR reserved words in Table 1.12.

Reserved Words

There are a number of reserved words in RPG. These reserved words are referred to as special names. All special names begin with a unique symbol (such as an asterisk or percent sign). The exceptions to this are the UDATE, UDAY, UMONTH, and UYEAR fields. The PAGE field, however, while technically a reserved word, can be declared with an alternative length and manipulated like any other field. There are many different classifications of reserved words in RPG, including:

  • Reserved Fields. Special field names containing a specific value. Typically, these values can be changed at runtime.

  • Figurative Constants. Special field names whose value is constant or is established at pre-runtime. The content of these fields cannot be changed.

  • Control Values. Special values that control operation function or output specification results.

  • Routines. Special routines within the RPG cycle.

  • Built-in Functions. Special routines that provide additional string, math, and expression functions.

Each of these types of reserved words is defined in Table 1.12.

Table 1.12: Reserved Words

Reserved Field

Description

*DATE

Used to retrieve the session date. This date value represents the date with eight positions. For example, if the date format is *MDY, *DATE will contain mmddccyy, where cc=century, yy=year, mm=month, and dd=day. The format of this date is based on the format specified in the header specification for the program

*MONTH

Used to retrieve the session month. This date value represents the month in mm format, where mm=month.

*YEAR

Used to retrieve the session century and year. This date value represents the year in ccyy format, where cc=century and yy=year.

*DAY

Used to retrieve the session day. This date value represents the day of the month in dd format, where dd=day of the month.




The Modern RPG IV Language
The Modern RPG IV Language
ISBN: 1583470646
EAN: 2147483647
Year: 2003
Pages: 156
Authors: Robert Cozzi

flylib.com © 2008-2017.
If you may any questions please contact us: flylib@qtcs.net