PL/I programming Tutorials – Part 3

Continuing PL/I Programming Basics Part 3 which includes String Attributes and other functions related to PL/I variables.

Basics of Programming with Strings ...
Basics of Programming with Strings in Python (Video 07)
PL/I Programming
Photo by Christina Morillo on Pexels.com

STRING DATA ATTRIBUTES in PL/I:

Logical data items in PL/I is referred as string data item. A string is a sequence of characters or bits that is treated as a single data item. In referring the string data item the term length is used.The length is no. of characters or bits a data item is to contain.

BIT string Example :

  •             DCL  MORE_RECS  BIT(1);
  •             DCL  NO                    BIT(1) INIT(‘0’B);
  •             DCL  YES                  BIT(1) INIT(‘1’B);
  •             ……
  •             MORE_RECS = NO
  •             DO WHILE (MORE_RECORDS);
  •             ….
  •             ON ERROR(INFILE)
  •                         MORE_RECS = NO;
  •             MORE_RECS = YES;
  •             END;
    • CHAR string Example :   DCL ITEM_DESC     CHAR(20);

Note :

Character string data items are padded with blanks on the right if the assigned constant is shorter than the declared length and If the constants is longer than the declared length, then truncation to the right of the data occurs

Similarly the Bit string constants are padded with zeroes on the right, if the assigned constant is shorter than the declared length and If the constants is longer than the declared one, the truncation on the right also occurs.

VARYING ATTRIBUTE in PL/I :

This attribute can be used, when a smaller character string is assigned to larger character string fields, when there is requirement padding on the right with blanks.

Syntax :

            DCL {id-name} {data-type} {data-length} [VARYING];

Examples :

            DCL NAME CHAR(20) VARYING;

CONTROLLED ATTRIBUTE :

This attribute is used to control one variable by another.

DEFINED ATTRIBUTE :

This attribute can be used along only with DCL statement and allows to equate two or more different names to the same storage area.

Examples :

            DCL NAME CHAR(20) INITIAL (‘ANAND BALASUBRAMANIAN’);

            DCL FIRST_NAME CHAR(5) DEFINED NAME;

Now the output of the FIRST_NAME will be ANAND. NAME is the base identifier. This function in PL/I is called Overlay Defining. But Base Identifier should be >= the Overlay Identifier variable.

POSITION ATRIBUTE in PL/I :

Used to identify the starting position of the Overlay Identifier variable on Base Identifier.

Examples:

            DCL A                        CHAR(40),

                     B            CHAR(10) DEFINED A,

                     C            CHAR(20) DEFINED A POSITION (21),

                     D                        CHAR(10) DEFINED A POSITION (11);

            B         –           1st 10 character of A.

            C         –           21st thru 40 th character of A.

            D         –           11th thru 20 th character of A.

INITIAL ATTRIBUTE :

Used to pass a Initial value to the Constant, no other exp. are allowed. Must be enclosed in parenthesis.

Syntax :

            DCL {data item name} {data characteristics} [INTIAL/INIT] [STATIC];

Examples :

            DCL AMT FIXED DECIMAL (7,2) INIT (24.50),

                     DATE CHAR (10) INIT (‘27.11.2000’);

To maintain good programming practice,

            For Program Constants, Use meaningful data names and init values in the DCL statement.

            For Program Variables and indicators, init the values through assignment statement.

PL/I DATA ATTRIBUTES :

FIXED DECIMAL

DATA FORMAT NAME      : PACKED DECIMAL

TYPE OF DATA                    : CODED ARITHMETIC

DEFAULT PRECISION       : 5 DECIMAL DIGITS

MAXIMUM PRECISION     : 15 DECIMAL DIGITS

Examples:

DECLARE     NET_PAY      FIXED DEC(7);

DECLARE     COMM           FIXED DEC(9,2);

DECLARE     NET_PRO      FIXED DEC;   /* DEFAULT IS DECIMAL (5) */

Note:

It is more efficient to specify the precision of fixed decimal point data as an Odd number of digits. An even no. requires the same amount of storage as the next higher no of digits, but the PL/I compiler will have to generate additional code to ensure the left most digit always contains zero.

FIXED BINARY

DATA FORMAT NAME      : FIXED POINT

TYPE OF DATE                    : CODED ARITHMETIC

DEFAULT PRECISION       : 15 BITS /* 32767 IN DECIMAL DIGITS

MAXIMUM PRECISION     : 31 BITS /* 2,147,483,647 DECIMAL DIGITS

Examples :

DECLARE     ORD_QTY     FIXED BIN (15);

DECLARE     MAC_VAL    FIXED BIN (31);

DECLARE     TOT_VAL      FIXED BIN(31,6);

Note :

Generally the instructions that perform arithmetic operations on FIXED BIN data have a faster execution time than other type of data.

FLOAT DECIMAL

DATA FORMAT NAME      : FLOATING POINT

TYPE OF DATE                    : CODED ARITHMETIC

DEFAULT PRECISION       : 6 DECIMAL DIGITS

MAXIMUM PRECISION     : 16 DECIMAL DIGITS

RANGE OF EXPONENT     : 10^-78 TO 10^+75

Examples :

DECLARE     ORD_QTY     FLOAT DEC (6);

DECLARE     MAC_VAL    FLOAT DEC (6);

DECLARE     TOT_VAL      FLOAT DEC (16);

FLOAT BINARY

DATA FORMAT NAME      : FLOATING POINT

TYPE OF DATE                    : CODED ARITHMETIC

DEFAULT PRECISION       : 21 BITS

MAXIMUM PRECISION     : 53 BITS

RANGE OF EXPONENT     : 2^-260 TO 2^+260

Examples :

DECLARE     ORD_QTY     FLOAT BIN (6);

DECLARE     MAC_VAL    FLOAT BIN (12) INIT 10E+7;

DECLARE     TOT_VAL      FLOAT BIN (21);

BIT

DATA FORMAT NAME      : BIT

TYPE OF DATE                    : LOGICAL

DEFAULT PRECISION       : NONE

MAXIMUM PRECISION     : 8000 BITS FOR CONSTANTS

RANGE OF EXPONENT     : 32767 BITS FOR VARIABLES

Examples :

DECLARE     ITEM              INIT(‘11110001’B);

DECLARE     PATTERN      INIT((8)’10’B);

CHARACTER

DATA FORMAT NAME      : CHARACTER

TYPE OF DATE                    : ALPHANUMERIC

DEFAULT PRECISION       : NONE

MAXIMUM PRECISION     : 1000 BITS FOR CONSTANTS

RANGE OF EXPONENT     : 32767 BITS FOR VARIABLES

Examples :

DECLARE     ITEM              CHAR(20);

DECLARE     PATTERN      CHAR(25)  INIT(‘BOOK STATUS REPORT’);

ARITHMETIC FUNCTIONS :

ABS:

To have the absolute value of the identifier. It eliminates the sign field of the identifier irrespectively whether it is +ve or -ve.

Syntax :

ABS(identifier);

Examples :

X=-2.83;                                                    X=+3.6999;

Y=ABS(X);                                              Y=ABS(X);

From the above two examples, The output will be 2.83 and 3.6999, by eliminating -ve sign and +ve sign of X respectively.

CEIL 

To have the next integer value to the value of the identifier.

Syntax :

CEIL(identifier);

Examples :

X=4.56;                                         X= -4.78;

Y=CEIL(X);                                 Y=CEIL(X);

The results of the above examples will be, 5.00 and -4.00 respectively.

FLOOR

To have the Prior integer value to the value of the identifier.

Syntax :

FLOOR(identifier);

Examples :

X=4.56;                                         X= -4.78;

Y=FLOOR(X);                             Y=FLOOR(X);

The results of the above examples will be, 4.00 and -5.00 respectively.

MIN   

To find the minimum value of the identifier from specified identifier list.

Syntax :

MIN (Id1, Id2 …. Idn);

Examples :

X=24;

Y=60;

Z=20;

B=MIN(X,Y,Z);

The result will be 20;

MAX 

To find the maximum value of the identifier from specified identifier list.

Syntax :

MAX (Id1, Id2 …. Idn);

Examples :

                  X=24;

Y=60;

Z=20;

B=MAX(X,Y,Z);

The result will be 60;

TRUNC:

Used to truncate the decimal portion of the identifier. i.e. This function will truncate the value, which is after decimal point.

Syntax :  TRUNC (Identifier);

Examples :

                  X=23.46789;

                  Y= -345.678890;

                  A=TRUNC(X);

                  B=TRUNC(Y);

                  The results of the above said examples are A=23; & B=-345;

SIGN

Used to find the value of the sign of the specified identifier.

Examples :

                  X=12.987;

                  Y= -6.789613;

                  Z=0;

                  A=SIGN(X);      B=SIGN(Y);       C=SIGN(Z);

                  The results of the above said examples are A = 1; B = -1; & C = 0;

ROUND

Used to rounding-off the value of the identifier to the next integer or decimal part, based on the arguments.

Syntax :

ROUND(m,n)

*          m argument is the identifier, which has the value to be rounded. If m is -ve then the absolute value will be rounded, but the sign remained unchanged. The m argument may be an element expr. or array name representing values or decimal data item.

*          n argument specifies the digit at which m is to be rounded.

Examples :

DCL X FIXED DEC (7,4);

   Y FIXED DEC (7,4);

X=123.7261;

                        Y= ROUND(X,3); /* The result will be 123.7260 */

                        Y= ROUND(X,2); /* The result will be 123.7300 */

                        Y= ROUND(X,1); /* The result will be 123.7000 */

X= -123.7261;

                        Y= ROUND(X,2); /* The result will be -123.7300 */

                        Y= ROUND(X,0); /* The result will be -124.0000 */

                        Y= ROUND(X,-1); /* The result will be -120.0000 */

MOD 

Extracts the remainder of the expr. The value returned is the smallest no.   

that must be subtracted from m to make it.

Syntax :

  • MOD(m.n)
  • m and n are the arguments passed the value to be divided and produced MOD      value.
  • If n argument is zero, then it is error.

Examples :

                        MOD(29,6), the result will be 5.

                        MOD(-29,6), the result will be 1.

MATHEMATICAL FUNCTIONS :

COS                TAN                ACOS             ATAN

COSD             TAND             ACOSD          ATAND

COSH             TANH             ACOSH          ATANH

SIN                 LOG                ASIN              EXP

SIND              LOG2              ASIND           SQRT

SINH              LOG10            ASINH

  These functions are operated on arguments in floating point scale, so the Arg. is converted to floating point unless so. For the invalid arg. error msg with the proper identification code will be printed along with the program termination. Arg to mathematical built-in functions may be single value, exp. or array name with has the floating point value.

OTHER BUILT-IN FUNCTIONS :

  • DATE & TIME: Both of them has to be declared explicitly.
  • DCL (DATE, TIME) BUILT-IN;

From the above example DATE returns the current date in the form YYMMDD, which is the character string of length 6. And TIME returns current time in the form HHMMSSTTT, which is a character string of length 9.

Leave a Comment