Revised - Intro. To PL1

Download as ppt, pdf, or txt
Download as ppt, pdf, or txt
You are on page 1of 106
At a glance
Powered by AI
The document discusses the content, methodology and duration of a PL/I programming course. It also provides an overview of different computer languages and features of the PL/I language.

The main topics covered in the PL/I course include introduction to PL/I features, subroutines and functions, built-in functions, file handling, storage classes, and sorting and merging data.

The main storage classes in PL/I are automatic, static, based and controlled. Automatic storage is allocated dynamically during procedure execution. Static storage remains allocated throughout program execution. Based storage uses a pointer variable to determine the address. Controlled storage provides persistent data storage.

1

TITLE : PL/I Language

OBJECTIVES :
Understand the PL/I language features Coding ,
debugging and testing the PL/I programs
PREREQUISITES :
The participants should be familiar with
IBM Main Frame skills like JCL, VSAM etc.
TARGET AUDIENCE : ELTP Trainees
METHODOLOGY :
Usage of slides
Exercises to be solved in class room
Exercises to be solved in Lab
Assignments and Quizzes
2
COURSE CONTENTS :
Unit Wise / Day Wise

1) Introduction to PL/I:
Features of PL/I
Comparison with other Languages.
Structured Programming
Character Set
Data Structures
Data Types & Data Manipulation

2) Subroutines & Functions:


Subroutine procedures
Arguments & Parameters
Built-in Function Arguments
Dummy Arguments
Function procedures
Recursive procedures 3
3) Built-in Functions
List of built-in functions
DO s and Arrays
CONDITIONS & ON-UNITS
4) File Handling
File Declarations
Stream I/O & Record I/O
Structures, Pictures
Introduction to VSAM
Data Organisations in VSAM
ESDS, KSDS and RRDS
Environment Attributes
Sample File Declarations using
most of the discussed attributes

4
5) Storage Classes
Automatic Storage
Prologue, Epilogue
Static Storage
Based Storage
ADDR Built-in Function
Pointers
Processing Data in Buffers
Move and Locate mode & Stack Concept
Controlled Storage
6) Sorting & Merging Data
PLISORTX Built-in Subroutine
DURATION : 6 Days @ 3 hours per day
TOTAL : 36 hours
THEORY : 18 hours
EXERCISES : 15
5
LAB : 18 hours
Survey of some widely used computer languages:
Language
Approximate date of Introduction
General Application Areas

1. FORTRAN 1957
Numerically oriented language (NOL) Scientific,
Mathematical & Statistical very widely used.
2.ALGOL 1960
Also NOL, but with new language features widely
used in Europe.

3. COBOL 1960
Most widely used Business-oriented language. 6
4. LIST 1961
.Special purpose language developed primarily
for list processing & symbolic manipulation
widely used in the area of AI.
5. SNOBOL 1962
Special Purpose language developed primarily
for character string processing. This includes
applications like TEXT editors, language
processors and bibliographic work
6. BASIC 1965
A simple interactive language widely used to 7

teach programming in schools & colleges


PL/I PROGRAMMING LANGUAGE:
* Can be used for both Business and Scientific
software development.
* Can process sequential and keyed Direct Access
files(IBM ISAM & VSAM files)
* Extensive error handling facilities.
* Can handle both Float binary and Float Decimal
data

8
*POINTER data allows list data structures to be built
during program run-time.
*STRUCTURES, Arrays of Structures, Structures of
Arrays can be declared in the program.
*PL/I can handle bit strings.
*EXTINSIVE set of Built-in functions.
*SUPPORTS Structured and Modular programming.
*It is single language useful for both scientific and
business-type problems.
*It is more “English-like” and thus easier to learn
9
*It is a “commonsense” language. If, for instance, the
programmer fails to get explicit instructions, the
compiler will make a reasonable decision on
programmer’s intentions.
*It is richer, more powerful, and better suited for
solution of all types problems than all the three
previous languages combined.
*It is modular. This means that a programmer can
write useful programs knowing just a fraction(that is,
small module) of the total language.
*It is a forerunner of a new generation of languages:
the PL/II, the PL/III and other such languages. 10
INTRODUCTION TO PL/I

PL/I (Programming Language/1) is a general


purpose language supporting scientific, data
processing, text processing, systems
programming applications.

Supports features of COBOL, PASCAL and


ALGOL.

The first version of PL/I was APL (Application


Programming Language). In 1965 IBM
implemented APL in Main Frame that version under
the name PL/I. 11
INTRODUCTION TO PL/I (Contd..)

PL/I compilers have been developed for a


number of computer systems, including the
following :

* BURROUGHS
* CDC
* DIGITAL
* HONEY WELL
* IBM
12
STRUCTURED PROGRAMMING

13
DATA TYPES AND STRUCTURES

Data in PL/I may be classified as


Coded Numeric data
Packed Decimal FIXED DECIMAL
Fixed Point FIXED
BINARY
Floating Point FLOAT
DECIMAL
FLOAT BINARY
Numeric Character data
Zoned Decimal PICTURE
Logical data
Character
14
CHARACTER/PICTURE
Bit BIT can be deleted
Arrays
Structures
Pointers
Files

Implied Data types


The implied data type for a data item
starting with I thru N is FIXED BINARY.
The implied data type for other data items is
short -form floating point 15
DECLARE STATEMENT

Syntax:
DECLARE name attributes

Base attribute
Function To specify the base type of an identifier
Options BINARY, DECIMAL
Default Depends on the prefix of an
identifier and scale attribute
Example DCL EX_1 DECIMAL FIXED(5,2);
16
Scale attribute
Function To specify the scale of an identifier
Options FIXED, FLOAT
Default Depends on the prefix of an identifier
and base attribute
Example DCL EX_1 DECIMAL FIXED(5,2);

Precision attribute
Function: To specify the no.of significant digits and /or the
decimal or binary point alignment of an identifier
Options (p,q) or (p)
Default Depends on the base and scale
attributes
Example DCL EX_1 DECIMAL FIXED(5,2);
Note For floating point data declare only the no.of
significant digits 17
e.g. DCL EX_1 FLOAT DEC(9,6) ; /* INVALID */
Default attributes

(Declared Attributes) (Default Attributes) <-- delete this line


Attribute Name Default Value
DECIMAL FIXED (5,0)
DECIMAL FLOAT (6)
BINARY FIXED (15,0)
BINARY FLOAT (21)
DECIMAL FLOAT(6)
BINARY FLOAT(21)
FIXED DECIMAL(5,0)
FLOAT DECIMAL(6)
None-initial I-N BINARY FIXED(15)
None-other than I-N,#,@,$ DECIMAL FLOAT(6)
18
Mode attribute
Function To specify the type of an identifier i.e
whether it is a REAL or COMPLEX
Options REAL or COMPLEX
Default REAL
Example DCL EX_1 DECIMAL FLOAT(5)
COMPLEX;

String data attributes


Function To specify the string data
Options BIT, CHAR
Default depends upon option <-- Delete
this line
Example DCL EX_1 CHAR(5) ;
DCL EX_2 BIT(10); 19
VARYING attribute
Function To declare variable length strings
Requirement CHAR/BIT
Default depends upon the requirement <-- Delete line
Example DCL EX_1 CHAR(5) VARYING;
DCL EX_2 BIT(10) VARYING;

DEFINED attribute (overlay definition)


Function To equate or share two or more variables
to the same storage area
Requirement Base identifier must be defined before
reference
Example DCL EX_1 CHAR(5);
DCL EX_2 DEFINED EX_1;
20
POSITION attribute
Function To overlay specific part of base identifier
Requirement DEFINED attribute must be present
Example DCL EX_1 CHAR(5);
DCL EX_2 DEFINED EX_1 POSITION(3);

INITIAL attribute
Function To set an identifier to an initial value
Example DCL EX_1 CHAR(14) INIT(‘PL/I
TRAINING’);

FIXED DECIMAL
Data Format Packed Decimal
Type of Data Coded Arithmetic
Default Precision 5 decimal digits
Max. Precision 15 decimal digits
21
Examples
DECLARE EXAMPLE_1 FIXED DEC(7);
DECLARE EXAMPLE_2 FIXED DEC(9,2) INIT
(24.00);
DECLARE EXAMPLE_3 FIXED DEC;

FIXED BINARY
Data format Fixed-point
Type of Data Coded arithmetic
Default Precision 15bits(32,767 in decimal)
Max. Precision 31bits(2,147,483,647 in
decimal)
Examples
DCL EXAMPLE_1 FIXED BIN(15);
DCL EXAMPLE_2 FIXED BIN(31) INIT(2147483647);
DCL EXAMPLE_3 FIXED BIN(31,6);
22
FLOAT DECIMAL
Data Format Floating-point
Type of Data Coded arithmetic
Default Precision 6 decimal digits
Max Precision 16 decimal digits
-76 +75
Range of Exponent 10 to 10
Examples
DCL EX_1 FLOAT DEC(6);
DCL EX_2 DEC(6);
DCL EX_3 FLOAT(6);
DCL EX_4 FLOAT DEC(16) INIT(6E+12);
DCL EX_5 FLOAT DEC INIT(5280);

23
SUBROUTINES AND FUNCTIONS :
There are four types of procedures:
1. Main procedures
2. Subroutine Procedures
3. Function Procedures
4. Recursive Procedures (earlier not mentioned here)
Main Procedures
Every PL/I program starts with a line
PROCEDURE OPTIONS(MAIN);
indicating the beginning of the PL/I program.
The PROCEDURE statement must always be labelled.
A subroutine procedure is invoked by a call.
CALL SUMPROC (A,B,C);
SUMPROC ==> Subroutine Name
A,B,C ==> Arguments List
24
Parameters must be declared in a parenthesised list
in the procedure statement of invoked procedure.
For e.g., SUMPROC : PROC(X,Y,Z);
CALL VALID(NAME,ADDRESS,ERROR);
IF ERROR THEN
PUT LIST(‘ERROR IN THE DATA’);

TIMEX : PROC RETURNS (CHAR(14));


DCL CONVERTED_TIME CHAR(14) INIT(‘ :’);
DCL CURRENT_TIME PIC ‘(9)9’;
DCL HOURS PIC ‘Z9’;
DCL MINUETS PIC ‘99’;
DCL SUBSTR BUILT-IN;
DCL TIME BUILT-IN;

25
CURRENT_TIME = TIME;
HOURS = SUBSTR(CURRENT_TIME,1,2);
MINUETS = SUBSTR(CURRENT_TIME,3,2);
IF HOURS > 0 & HOURS < 12 THEN
SUBSTR(CONVERTED_TIME,7,2) = ‘AM’;
ELSE
IF HOURS > 12 & HOURS < 24 THEN
DO;
SUBSTR(CONVERTED_TIME,7,2) = ‘PM’;
HOURS = HOURS - 12;
END
ELSE
IF HOURS = 0 | HOURS = 24 THEN
DO;
IF MINUETS = 0 THEN 26
SUBSTR(CONVERTED_TIME,7,8_ = ‘MIDNIGHT’;
ELSE
SUBSTR(CONVERTED_TIME,7,2) = ‘AM’
HOURS = 12;
END
ELSE
IF HOURS = 12 THEN
IF MINUETS = 0 THEN
SUBSTR(CONVERTED_TIME,7,4) = ‘NOON’;
ELSE

SUBSTR(CONVERTED_TIME,7,2) = ‘PM’;
SUBSTR(CONVERTED_TIME,1,2) = HOURS;
SUBSTR(CONVERTED_TIME,4,2)
= MINUETS;
RETURN(CONVERTED_TIME);
END TIMEX;

27
FUNCTION PROCEDURES:
A function is a procedure that returns a single value to the
invoking procedure.

RETURN statement is always needed for an function subprogram.

Z = CALC(X,Y);
CALC ===> A function procedure name.
X,Y ====> Argument List
Result is assigned to Z.

IF CALC(X,Y) < 0 THEN


CALL NEGATIVE_RT;
Functions Should not be invoked by a Call.
Subroutines Should not be invoked by Function
Reference.

CALC : PROC(A,B,C) RETURNS(FIXED DEC(7));


RETURNS(A+B+C);
28
END CALC;
RECURSIVE PROCEDURES :
When a procedure invoked itself, it is called as
Recursive Procedure. To specify a procedure as
Recursive we need to give Recursive option with
Procedure statement.

I = 4;
RUSULT = N_FACTORIAL(I);
N_FACTORIAL : PROCEDURE(N);
K = 1;
DO I = 1 TO N;
K = K * I;
END;
RUTURNS(K);
END N_FACTORIAL; 29
Example :-
N_FACTORIAL : PROC(N) RECURSIVE;
K = N -1;
IF K = 1 THEN
I = N;
ELSE
I = N * N_FACTORIAL(K);
RUTURNS(I);
END N_FACTORIAL;

SCOPE OF AN EXPLICIT DECLARATION


P1 = PROC
DCL A,B;
P2 : PROC;
DCL C,D;
Y = C * D;
END P2;
END P1;
30
SUBROUTINES AND FUNCTIONS

Advantages
Saves coding effort
Reduces total programming time
Improves program readability
Facilitates program maintenance
Types
Main procedures
Subroutine procedures
Function Procedures

31
SUBROUTINE PROCEDURES

A subroutine procedure is invoked by a CALL.


Arguments are passed by means of an argument
list.
Subroutine procedures that are separately
compiled from the invoking procedure are called
external procedures.
e.g. CALL SUBR(X,Y,Z)

32
ARGUMENTS AND PARAMETERS

An argument is a value passed to the invoked procedure, each


argument corresponds to a parameter in the parameter list, and this
correspondence is from is from left to right.
A parameter is a name used within the invoked procedure to
represent another name or expression that is passed to the
procedure as an argument.
e.g. MAINPR: PROCEDURE OPTIONS(MAIN);
DCL(X,Y,Z) FIXED DEC(7,2);
DCL SUBRT ENTRY;
.........
CALL SUBRT(X,Y,Z);
.........
END MAINPR;
SUBRT: PROCEDURE (A,B,C);
DCL (A,B,C) FIXED DEC(7,2);
.................
END SUBRT; 33
MAINPR invoking procedure
SUBRT invoked procedure / sub
program
X,Y,Z arguments
A,B,C parameters

An argument include


Variable
Constant
Expression
Array name
Array Expression
Major Structure name
Minor Structure name
Structure Expression
Built-in function name
Entry name
File name
34
Label
ENTRY STATEMENT
Allows us to direct the compiler to generate code to
conform the attributes of the parameters (passed through
programs), should arguments and their corresponding
parameters have different attributes. <--- Rephrased the
definition to give more clarity

e.g. DCL SUBRT ENTRY (FIXED DEC (7,2),FIXED


DEC (7,2),FIXED DEC(7,2));
DCL (X,Y,Z) FLOAT DEC(6);
................
CALL SUBRT(X,Y,Z);

When the subroutine SUBRT is invoked X,Y,Z are 35


converted to FIXED DEC(7,2).
DUMMY ARGUMENTS
A dummy argument is always created for the following
cases
If an argument is a constant
If an argument is an expression involving operators
If an argument is an expression in parenthesis
If an argument is a variable whose attributes are
different
If an argument is itself a function reference
containing arguments

36
PSEUDO VARIABLES :
A pseudo variable is a built in function name appearing
as a receiving field.

Example :
DCL EDIT_DATE CHAR(14) INIT(‘DATE:
03/14/88’);
SUBSTR(EDIT_DATE,7,2) = ‘12’ /*
CHANGE MONTH 03 TO 12 */

Here the SUBSTR is used as a receiving variable field.

A : PROC REORDER ;

Optimization specification allows compiler to reorder


the evaluation of expressions for optimum speed. 37
CARRIAGE CONTROL CHARACTERS :

Character Code Resulting


Carriage Control Operation
1 Begin printing at the top of the
next page
(blank) Space one line before printing
0 Space two lines before printing
- Space three lines before printing
+ Suppress space before printing

38
CONDITIONS AND ON-UNITS
Syntax: ON condition on-unit;

Types of Conditions

I/O Conditions
ENDFILE(filename)
ENDPAGE(filename)
RECORD(filename)
TRANSMIT(filename)
CONVERSION
SIZE
Arithmetic Conditions
CONVERSION
FIXEDOVERFLOW
OVERFLOW
UNDERFLOW
ZERODIVIDE 39
SIZE
Types of Actions
System Action
Program specified Action
Null Action (e.g. ON ENDPAGE (PRINTR); )

40
Condition When it Raises Example
ENDFILE
Raises during GET or READ operation. It is caused by reading the past of
the last record
ON ENDFILE(TRAN)
MORE_REC = NO;

ENDPAGE
Raises when a PUT stmt results in an attempt to start a new line beyond the
limit specified for PAGESIZE
ON ENDPAGE(PRINTR)
BEGIN ......
END;

TRANSMIT
Raises when an input or output device did not transmit data correctly

41
ARITHMETIC CONDITIONS
Condition When it raises Example

CONVERSION
Occurs whenever a conversion is attempted on char data containing chars that are
invalid
DCL X BIT(4);
X=‘10A1’;

FIXED OVERFLOW
Occurs whenever a result exceeds the fixed point limit declared
DCL (A,B,C) FIXED DEC (15);
A=40000000;
B=80000000;
C=A*B;

OVERFLOW
Occurs when the magnitude of a floating point number exceeds the maximum
A=55E71;B=23E11;
C=A*B; /* THE MAX IS 75 */ 42
Condition When it raises Example

UNDERFLOW
Occurs when the magnitude of a floating point number less than permitted
minimum
A=23E-71;B=3E-9;
C=A*B; /* THE MIN IS -78 */

ZERODIVIDE
Occurs when an attempt is made to divide by zero A=10;
B=0;
C=A/B;
Note:
1. Conditions are enabled or disabled through prefix. The
prefix NO is used for disable a condition.
e.g. (NOOVERFLOW):PROG1:PROC OPTIONS(MAIN)
2. We can simulate the occurrence of a condition through the
use of SIGNAL statement. 43
Syntax: SIGNAL condition;
BUILT-IN FUNCTIONS FOR ON-UNITS
Function Purpose Example
ONCODE
To determine the type of interrupt that caused the on-unit to become
active
ON ERROR
PUT LIST (ONCODE);

ONLOC
To determine the entry point of the procedure in which the condition was
raised

ONCHAR
Extracts the char that caused the CONVERSION condition to be raised
CHAR=ONCHAR;

ONSOURCE
Extracts the contents of the field that was being processed when a
CONVERSION was raised 44
SOURCE=ONSOURCE;
REPETITIVE STRUCTURES

Types:

DO-WHILE

DO-UNTIL

DO-LOOP or iterative DO

45
DO-WHILE

Syntax: DO WHILE(expression)
...........
END;
Termination When the expression becomes false
Example ON ENDFILE(INFILE)
MORE_RECORDS=NO;
READ FILE(INFILE) INTO(IN_REC);
DO WHILE (MORE_RECORDS)
READ FILE(INFILE) INTO(IN_REC);
END;

46
DO-UNTIL

Syntax: DO UNTIL(expression)
..........
END;
Termination When the expression becomes true
Example ON ENDFILE(INFILE)
EOF=YES;
READ FILE(INFILE) INTO(IN_REC);
IF EOF=NO
CALL NO_REC_IN_FILE;
ELSE
DO UNTIL (EOF)
CALL PROCESS_DATA;
READ FILE(INFILE) INTO(IN_REC);
47
END;
PROB6 : PROC OPTIONS(MAIN);
/* DETERMINE THE TYPE OF A TRIANGLE */
/* EQUILATERAL, ISOSCELES, OR SCALENE */
DCL (S1, S2, S3) FIXED;
ON ENDFILE(SYSIN) STOP;
READ3 : GET LIST(S1, S2, S3);
PUT SKIP(3) LIST(S1, S2, S3);
/* NESTED IF USED TO SELECT ONE CASE */
IF (S1 = S2) & (S2 = S3)
THEN PUT LIST(‘EQUILATERIAL’);
ELSE IF ((S1 = S2) | (S2= S3) | (S1 = S3))
THEN PUT LIST(‘ISOSCELES’);
ELSE PUT LIST(‘SCALENE’);
GO TO READ3;
END PROB6;
48
BUILT-IN - FUNCTIONS :

TYPES :
Arithmetic
Mathematical
String Handling
Array Handling
Condition Handling
Storage Control
Other Functions

49
ARITHMETIC BUILT-IN FUNCTIONS :

Function Purpose Example(s)


ABS
Finds the absolute value of a given quantity
X= - 3.714; Y=ABS(X); /* Y=3.714 */

CEIL
Finds smallest integer greater than or equal to argument
X=3.32; Y=CEIL(X); /* Y=4.00 */

FLOOR
Finds largest integer that does not exceed the argument
X=3.32;Y=FLOOR(X); /* Y=3.00 */

MAX
Finds the largest value from two or more arguments
X=100;Y=32.76;Z= -8;
Z=MAX(X,Y,Z); /* Z = 100 */ 50
Function Purpose Example(s)

MIN
Finds the smallest value from two or more arguments
X=100;Y=32.76;Z= -8;
Z=MIN(X,Y,Z); /* Z = -8 */

MOD(m,n)
Extracts the remainder resulting from the division of the
first argument by second argument
X=29;
Y=MOD(X,6);/* Y=5 */

51
Function Purpose Example(s)

ROUND(m,n)
Rounds a given value at a specified digit and pads spare digit positions
with zeros
X=123.7261; Y=ROUND(X,3);/* Y=123.7260 */
Y=ROUND(X,1);/* Y=123.7000 */

SIGN
Determines the sign of a value and returns a result of
1 for positive
0 for zero
-1 for negative
X=123; I=SIGN(X); /* I=1 */

TRUNC
Changes fractional part of an argument to zero
X=3.32;Y=TRUNC(X); /* Y=3.00 */
52
MATHEMATICAL BUILT-IN FUNCTIONS:
ACOS(X)
ASIN(X)
ATAN(X)
ATAN(X,Y)
ATAND(X)
ATAND(X,Y)
ATANH(X)
COS(X)
COSD(X)
COSH(X)
ERF(X)
ERFC(X)
EXP.(X)
LOG(X)
LOG10(X)
LOG2(X)
SIN(X)
SIND(X)
SINH(X)
SQRT(X)
TAN(X)
TAND(X)
TANH(X) 53
STRING HANDLING FUNCTIONS:
Function Purpose Example(s)
1.CHAR
2.CHAR(x,n)
n is the length of the result Converts a given value to a
character string
DCL A FIXED DEC (5);
DCL B CHAR(5);
A=175;
B=CHAR(A); /* B=‘ 175’ */

BIT
Converts a coded arithmetic data item or char string to a bit-
string
X=BIT(NUMBER);
54
/* X=BIT STRING */
Function Purpose Example(s)

INDEX
Searches a string for a specified bit or char string
NAME=‘PL/I LANG’;
IND=INDEX(NAME,’PL/I’); /* IND=1 */

LENGTH
Finds the length of a given char string or bit string DCL
NAME CHAR(20) VARYING;
NAME = ‘PL/I’;
LTH=LENGTH(NAME); /* LTH=4 */

REPEAT(m,n)
Concatenates the given string itself a specified no. of times
55
NAME=REPEAT(‘PL/I’,2); /* NAME=‘PL/IPL/I’ */
Function Purpose Example(s)

SUBSTR(NAME,I,J)
To manipulate smaller part of larger char or bit string X=‘PL/I
LANGUAGE’;
Y=SUBSTR(X,1,4); /* Y=‘PL/I’ */

TRANSLATE(s,r,p)
Substitutes one char with another
NAME=‘ABCD’;
NAME=TRANSLATE(NAME,’X’,1);
/* NAME=‘XBCD’ */

VERIFY
Examines two strings to verify that each char or bit of the first string is
represented in the second string DIGITS=‘0123456789’;
X=‘01234’;
RESULT=VERIFY(X,DIGITS); /* RESULT = 0 */
56
DATE and TIME BUILT-IN FUNCTIONS:

Returns Current Date and Current Time


e.g. CURR_DATE=DATE;
CURR_TIME=TIME;

Format
DATE YYMMDD
TIME HHMMSSTTT

When these functions are used , they should be


explicitly declared to have the BUILT-IN attribute.
e.g. DCL (DATE , TIME) BUILT-IN;

57
ARRAYS:
An array is a table of data items in which each item
has the same attribute as every other item in the
array.

e. g.,
DCL EX_1(365) FIXED DEC(4,1);
DCL EX_2(0:11) FIXED DEC(5);
DCL EX_3(-5 : +5) FLOAT DEC(6);
DCL EX_4(-2:6) FIXED BIN(15,0)
INIT(10,20,30,40,50,60,70,80);
-2 is lower bound and 6 is upper bound of
that array.
58
DIMENSIONS :

The number of sets upper and lower bounds


specifies the number of dimensions in an array.

e. g., DCL EX_1(6,2) FIXED DEC(5);


is two dimensional array having 6 rows and 2
columns.
DCL EX_2 (-3:3, -4:4) FLOAT DEC(6) INIT((63)0);

59
SUBSCRIPTS :

Used to refer an element of an array.


e.g., EX_1(1,2) is the element belongs to first row
and second column

Subscripts may be constants, variables , or


expressions.

e.g., I = 3;
J = 2;
T = EX_1(I,J); /* variable subscripts */
T = TEMP(I - J + K);
/* subscript expressions */
I = 3;
Z = X(Y(I)); /* Subscript Subscripts */ 60
ARRAY BUILT-IN FUNCTIONS:

Function Purpose
DIM Provides the current extent for a specified dimension
in a given array
LBOUND Finds the current lower boundary
HBOUND Finds the current higher boundary
SUM Finds the sum of all elements in a given array
PROD Finds the product of all elements in a given array
POLY Used to form a polynomial expansion from two
arguments
ANY Used to test any bit(s) of a given bit-string array
ALL Used to test all bits of a given bit-string array

61
ARRAY ASSIGNMENTS:

Scalar-to-Array:
Entire array is assigned a single scalar value
DCL EX_1(12) FIXED DEC(4,1); EX_1=0;

Array-to-Array:

One array may be moved to another array provided the


arrays have identical dimensions and bounds

DCL A(5,5) FLOAT DEC(6);


DCL B(5,5) FLOAT DEC(6);
A=B;
62
ARRAY EXPRESSIONS:

Prefix Operators
Each element is effected by this operation.
e.g. A = -A;
Infix Operators
Each element is effected by this operation
e.g. B = A * 5; B = A * A(1,2); A = A + B;
Cross section
A subscript may also be an asterisk , in which case it
specifies
the entire extent of the dimension.

e.g. DCL EX1 (3,4) FIXED DEC(3,2);


DCL EX2 (3) FIXED DEC(3,2);
DCL EX3 (3) FIXED DEC(3,2); 63
EX3 = EX1 (*,4) * EX2;
Example of Single dimension array
ARREX: PROCEDURE OPTIONS(MAIN);
DCL (X(1:100),I,N,SUM) FIXED,
AVERAGE FIXED(7,2);
GET LIST(N);
PUT LIST(N,'DATA VALUES');
SUM = 0;
NEWNUMB: DO I = 1 TO N;
GET LIST(X(I));
PUT SKIP LIST(X(I));
SUM = SUM + X(I);
END NEWNUMB;
AVERAGE = SUM / N;
PUT SKIP(3) DATA(AVERAGE);
END ARREX;
64
FILE HANDLING: STREAM I/O

Types:-

EDIT-DIRECTED I/O

DATA-DIRECTED I/O

LIST DIRECTED I/O

65
I/O PROGRAMMING STEPS :-

1. Define the file


DECLARE PAYROLL FILE.....
Specifies attributes to be associated with the file name.

2. Open the file


OPEN FILE(PAYROLL);
Completes the file declaration, if necessary associates the file
name with the physical device on which the file is located
with the physical device on which the file is located.

66
3. Process information in the file
READ or WRITE or REWRITE 4
I/O Statements for RECORD files
GET or PUT I/O Statements for STREAM files

4. Close the file


CLOSE FILE(PAYROLL);
Files are automatically closed when your program
terminates normally disassociates the file name with the
physical device where file was located.

67
EDIT-DIRECTED I/O:

Syntax GET EDIT (data list) (format list);


PUT EDIT (data list) (format list);

Example:
GET EDIT (EMP#,NAME,RATE,HOURS,DEDUCTIONS)
(COLUMN (1),A(6),A(20),F(4,2),F(3,1),F(5,2));

Format items
Data format items
Control format items (line,page,space control)
Remote format items
68
DATA FORMAT ITEMS:

A(w)
A
B(w)
B
C
E(w,d)
E(w,d,s)
F(w)
F(w,d)
F(w,d,p)
X(w)
P ‘picture specification’
69
w Total no of chars/digits
d No.of fractional decimal places
s No.of significant digits ( d + 1)
p Scaling factor
A Character notation
B Bit string notation
C Complex variables
E Floating point notation
F Fixed point notation
P Picture definition

70
CONTROL FORMAT ITEMS

COLUMN(n)
LINE(n)
PAGE
SKIP

71
DATA DIRECTED I/O

Syntax GET DATA (data list)


PUT DATA (data list)

e.g. GET DATA (A,B,C,D,E)


PUT DATA (A,B,C,D,E)

Input should be given with variable names .


e.g. A=12,B=12,C=13,D=14,E=15

Output will be printed like this


e.g A=12 B=12 C=13 D=14 E=15

72
COUNT BUILT-IN FUNCTION

Determines the number of data items that were transmitted


during last GET or PUT.

e.g. DCL INFILE FILE INPUT STREAM;


GET FILE (INFILE) DATA;
I=COUNT (INFILE);

73
FILE DECLARATION:

DCL INVEN FILE INPUT STREAM


ENV (Options);

FILE
This key word although optional must follow the file name if
specified. Other attributes, however, may be in any sequence
INPUT
Direction of Transmission
STREAM
Type of Transmission
ENVIRONMENT
May be abbreviated ENV
74
/* CREATE A NAME AND ADDRESS FILE AT
THE TERMINAL */
/* PROGRAM NAME : CREATE */
/* DESCRIPTION : This program creates a name
and address file in a disk file whose name is selected
by the data entry operator. Data is input from the
terminal and written to the disk file as well as the
printer in the form of an “ADDRESS LABEL”
suitable for mailing. */

CREATE : PROC OPTIONS(MAIN);


DCL SYSPRINT FILE OUTPUT STREAM;
DCL SYSIN FILE INPUT STREAM;
DCL DISKFILE FILE OUTPUT STREAM;
DCL LABEL FILE OUTPUT STREAM
PRINT; 75
DCL DATA_OK BIT(1);
DCL MORE_RECORDS BIT(1) INIT(‘1’B);
DCL NO BIT(1) INIT(‘0’B’);
DCL YES BIT(1) INIT(‘1’B’);
DCL FILE_NAME CHAR(14) VAR;
DCL NAME CHAR(30) VAR;
DCL ADDR CHAR(30) VAR;
DCL CITY_ST_ZIP CHAR(30) VAR;
DCL RESPONSE CHAR(1);

ON ENDFILE(SYSIN)
MORE_RECORDS = NO;
PUT SKIP FILE(SYSPRINT) LIST(‘ENTER
THE NAME OF THE OUTPUT FILE’);
GET FILE(SYSIN) LIST (FILE_NAME);
76
OPEN FILE(DISKFILE) TITLE(FILENAME);
OPEN FILE(LABEL) PAGESIZE(7) INESIZE(39);

DO WHILE(MORE_RECORDS);
PUT SKIP FILE(SYSPRINT) LIST(‘NAME:’);
GET FILE(SYSIN) LIST(NANE);
IF NAME = ‘EOF’ THEN
SIGNAL ENDFILE(SYSIN);
ELSE DO;
DATA_OK = ‘NO’;
DO UNTIL(DATA_OK);
PUT SKIP FILE(SYSPRINT) LIST(‘ ADDRESS:’);
GET FILE(SYSIN) LIST(ADDR);
PUT SKIP FILE(SYSPRINT)
LIST(‘CITY/STATE/ZIP:’);
GET FILE (SYSIN) LIST(CITY_ST_ZIP); 77
PUT SKIP FILE(SYSPRINT) LIST(‘IS DATA OK ? (Y/N):’);
GET FILE(SYSIN) LIST(RESPONSE);
IF RESPONSE = ‘Y’ THEN
DATA_OK = YES;
END;
PUT FILE(DISKFILE) LIST(NAME,ADDR,CITY_ST_ZIP);
PUT PAGE FILE(LABEL) LIST(NAME);
PUT SKIP FILE(LABEL) LIST(ADDR);
PUT SKIP FILE(LABEL) LIST(CITY_ST_ZIP);
END;
END;
CLOSE FILE(DISKFILE);
END CREATE;

78
/* GENERATE THE INVENTORY AUDIT REPORT */
/* PROGRAM NAME : AUDIT */
/* DESCRIPTION : This program reads the entire file of
inventorying records and computes the value of the items on
hand. */
/* INPUT : INVENTORY FILE */
/* OUTPUT : INVENTORY AUDIT REPORT */

AUDIT : PROCEDURE OPTIONS(MAIN);

DCL COUNT FIXED DECIMAL(5) INITIAL(0);


DCL TOT_COST FIXED DECIMAL(7,2);
DCL ITEM CHARACTER(4);
DCL MORE_RECORDS BIT(1) INITIAL(‘1’B);
DCL NO BIT(1) INITIAL(‘0’B);
DCL COST FIXED DECIMAL(5,2);
DCL QUANTITY FIXED DECIMAL(3);
DCL TOTAL FIXED DECIMAL(7,2) INITIAL(0);
79
ON ENDFILE(SYSIN)
MORE_RECORDS = NO;

/* PROGRAM NUCLEUS */

CALL PRINT_HEADING;
GET LIST(ITEM, COST, QUANTITY);

DO WHILE(MORE_RECORDS);
COUNT = COUNT + 1;
TOTAL_COST = COST + QUANTITY;
TOTAL = TOTAL + TOTAL_COST;
PUT SKIP LIST(ITEM,COST, QUANTITY,
TOTAL_COST);
GET LIST(ITEM, COST, QUANTITY);
END;

80
PUT SKIP(2) LIST(‘ ‘,’ TOTAL VALUE OF
INVENTORY : ‘, TOTAL);
PUT SKIP(2) LIST(‘ ‘,’NUMBER OF RECORDS
PROCESSED:’, COUNT);

/* PRINT REPORT HEADING */


PRINT_HEADING : PROCEDURE;

PUT LIST(18) ‘ ‘ || ‘ VALUE OF INVENTORY ‘ ||


‘ REPORT ‘);
PUT SKIP(2);
PUT SKIP LIST(‘PART NUMBER ‘.’ COST ‘.’
QUANTITY ‘.’ TOTAL_COST’);
PUT SKIP(2);
END PRINT_HEADING;
END_AUDIT;
81
File Attributes :

In the following example FILE is not explicitly


coded.

DCL BILLING INPUT STREAM ENV(F


BLKSIZE (150));

But for program readability, it is recommended


that FILE always be specified.

82
INPUT/OUTPUTATTRIBUTES: -

One of these attributes must be specified either in


the DECLARE statement or in the OPEN
statement.

Both attributes could not be specified. INPUT is


the default attribute unless the file has the PRINT
attribute (then OUTPUT is default).

83
STREAM Attribute :

This attribute specifies the data items are accessed by GET or


PUT Statements and that they are a continuous stream. There are
three forms of stream I/O.

LIST-directed GET LIST(A,B,C);


PUT LIST(A,B,C);

Edit-directed GET EDIT(A,B,C)(F(5),F(7,2),A(20));


PUT EDIT(A,B,C)(PAGE,
COL(5),F(5),E(15,2),X(10),A);

Data-directed GET DATA(A,B,C);


PUT DATA(A,B,C);

Default Pre defined TAB ===> 25 49 73 97

84
ENVIRONMENT Attribute :
For line printer output, the record type can be variable
(V instead of F)

F, BLKSIZE(133) might be the specification you


would write in the ENVIRONMENT section of the
file declaration statement for a printer.

PRINT Attribute :
Print attribute is added to the file declaration for
stream files associated with a line printer so that the
carriage control options such as PAGE and LINE
may be specified in the PUT statement. The PRINT
attribute applies only to files with the STREAM and
OUTPUT attributes.
85
DEFAULT or PREDEFINED FILES :

When file names are omitted from a GET or PUT,


default file names are assumed. SYSIN for the
standard input file and SYSPRINT for the output file .
Thus, the statements

GET LIST(A,B,C); PUT LIST(A,B,C);


are equal to
GET FILE(SYSIN) LIST(A,B,C);
PUT FILE(SYSPRINT) LIST(A,B,C);

List directed I/O statements where a file name is


explicitly coded, take this general form:

GET FILE(file name) LIST(data names);


86
PUT FILE(file name) LIST(data names);
The SYSIN and SYSPRINT file names and their attributes do not
have to be explicitly declared. The SYSIN file is referenced in the
END FILE statement.

For Example :
ON END FILE(SYSIN)
MORE_RECORDS = NO;
Thus we may take program specified action on the end-of-file
condition without having to declare explicitly the SYSIN file.

Here are the file attributes and options that apply to SYSIN and
SYSPRINT for IBM implementations of PL/I.

SYSIN FILE STREAM INPUT ENV (F BLKSIZE(80))


SYSPRINT FILE STREAM OUTPUT PRINT ENV(V
BLKSIZE(129))

LINESIZE = 120 Positions per line


PAGESIZE = 80 lines per page.
87
OPEN Statement :
Example:-
OPEN FILE(KEYBD), FILE(PRINTER);

Note: 1. File names 7 or 8 maximum


2. Comma is required between two files

When files are opened:


Attributes are merged (From JCL, DCL, OPEN)
Device readiness is checked
Labels are checked ( When a tape or disk data set is
created, a label is defined through job control
statements. This label contains the name by which
the data set is identified to the operating system).
88
e.g. GET EDIT(EMP#,NAME,RATE,HOURS,DEDUCTIONS)

GET EDIT (data list) (format list);


PUT EDIT (data list) (format list);

Indicates edit-directed data transmission.

GET FILE(file name) EDIT (data list) (format list);


PUT FILE(file name) EDIT (data list) (format list);

To input the data previously illustrated. The following statement


could be coded.

GET EDIT (EMP#,NAME,RATE,HOURS,DEDUCTIONS)


(COLUMN (1),A(6),A(20),F(4,2),F(3,1),F(5,2));

89
REMOTE FORMAT ITEM :

GET
EDIT(EMP#,NAME,RATE,HOURS,DEDUCTIONS)
(R(RFMT1));
...
...
...
RFMT1 : FORMAT
(COLUMN(1),A(6),A(20),F(4,2),F(3,1),F(5,2));

GET EDIT(data list)(format list)(data list)(format


list)..................;

90
RECORD I/O :
As a means of introducing the concepts of record
I/O programming, an 80/80 list program will be
illustrated.

ON ENDFILE more_records=no
more_records = yes
READ first record
DO WHILE more records
WRITE record
READ next record
END DO

91
The declaration may be coded as follows :

DCL DATA FILE INPUT RECORD ENV(F RECSIZE(80));


DCL PRINTER FILE OUTPUT RECORD ENV(F
RECSIZE(80));
DCL DATA_AREA CHAR(80);

Here is the recording I/O coding for the 80/80 list program:

ON ENDFILE (DATA)
MORE_RECORDS = NO;
MORE_RECORDS = YES;
READ FILE(DATA) INTO (DATA_AREA);
DO WHILE(MORE_RECORDS);
WRITE FILE(PRINTER) FROM(DATA_AREA);
READ FILE(DATA) INTO (DATA_AREA);
END;

92
/* SKIP TO A NEW PAGE */
/* PROGRAM NAME : LINECNT */
/* DESCRIPTION : This program reads an input file and lists
the data records on the printer. The line counter is checked and a
skip to a new page is forced with carriage control characters if the
line number exceeds 55. */
/* INPUT : DETAIN */
/* OUTPUT : PRINTER */

LINECNT : PROCEDURE OPTIONS(MAIN);


DCL DETAIN FILE INPUT RECORD ENV(F
RECSIZE(80));
DCL PRINTER FILE OUTPUT RECORD ENV(F
RECSIZE(81) CTLASA);
DCL DATA_AREA CHAR(80);
DCL LINE_COUNT FIXED(3);
DCL MORE_RECORDS BIT(1) INIT(‘1’B);
DCL NO BIT(1) INIT(‘0’B);
DCL PRINT_AREA CHAR(81);
93
ON ENDFILE(DATAIN)
MORE_RECORDS = NO;
OPEN FILE(DATAIN), FILE(PRINTER);
LINE_COUNT = 55;
READ FILE(DETAIN) INTO (DATA_AREA);
DO WHILE (MORE_RECORDS);
IF LINE_COUNT > 55 THEN
DO;
PRINT_AREA = ‘1’ || DATA_AREA;
LINE_COUNT = 0;
END;
ELSE
PRINT_AREA = ‘ ‘ || DATA_AREA;
WRITE FILE(PRINTER) FROM (PRINT_AREA);
READ FILE(DATAIN) INTO (DATA_AREA);
END;
END LINECNT;

94
A STRUCTURE is a collection of data items whose
locations relative to one another are critical. Usually
the data items that appear in a structure have a logical
relationship to each other. To describe the layout the
following structure could be coded:

DCL 1 NAME_AND_ADDR,
2 NAME CHAR(20),
2 STREET CHAR(25),
2 CITY_STATE CHAR(25),
2 ZIP CHAR(9),
2 REST_OF_RECORD CHAR(1);

95
RECORD STREAM
1. Respects physical record boundaries
1. Ignores physical boundaries. I/O is considered to be a continuous
stream.
2. Stores data in exactly same form as input, no conversion or validity
checks.
2. Converts character data in input stream to the attributes of the declared
identifiers.
3. Outputs data in exactly the same from as internally stored.
3. Converts and edits internally stored data to character format.
4. Input and output may be any data type(packed decimal, fixed point,
binary, etc.,)
4. Input and output are a stream of characters.
5. Keywords : READ, WRITE
5. Keywords : GET, PUT
6. May be used with any data set organisation(sequential, indexed
sequential, VSAM, direct)
6. May only be used for sequential data sets
96
A BUILT-IN FUNCTION FOR STRUCTURES:
The STRING function concatenates all the elements in an array or a
structure into a single character or bit string element.
EXAMPLES :
DCL 1 STRUCTURE,
2 ELEMENT1 CHAR(5) INIT(‘ABCDE’),
2 ELEMENT2 CHAR(3) INIT(‘123’),
2 ELEMENT3 CHAR(7) INIT(‘XYZXYZX’);
DCL ITEM CHAR(15);
ITEM = STRUCTURE; /* ILLEGAL MORE */
ITEM = STRING(STRUCTURE);
/* ITEM = ABCDE123XYZXYZX*/
STRUCTURE=ITEM;
/* STRUCTURE.ELEMENT1=‘ABCDE’*/
/* STRUCTURE.ELEMENT2=‘ABC’*/
/* STRUCTURE.ELEMENT3= ‘ABCDE12’*/
STRING(STRUCTURE)=ITEM;
/* STRUCTURE.ELEMENT1=‘ABCDE’*/
/* STRUCTURE.ELEMENT2=‘123’*/
/* TRUCTURE.ELEMENT3=‘XYZXYZX’*/
97
THE LIKE ATTRIBUTE :
DCL 1 BUDGET,
2 RENT FIXED DEC(5,2),
2 FOOD,
3 MEAT FIXED DEC(5,2),
3 DAIRY FIXED DEC(4,2),
3 PRODUCE FIXED DEC(5,2),
2 TRANSPORTATION FIXED DEC(7,2),
2 ENTERTAINMENT FIXED DEC(5,2);
DCL 1 COST_OF_LIVING LIKE BUDGET;
This declaration for COST_OF_LIVING is the same as if it
had been declared :
DCL 1 COST_OF_LIVING,
2 RENT FIXED DEC(5,2),
2 FOOD,
3 MEAT FIXED DEC(5,2),
3 DAIRY FIXED DEC(4,2),
3 PRODUCE FIXED DEC(5,2),
2 TRANSPORTATION FIXED DEC(7,2),
98
2 ENTERTAINMENT FIXED DEC(5,2);
STRUCTURE ASSIGNMENT:

Structure to Structure :
EXAMPLE1:
DCL 1 INPUT_REC,
2 KEY_FIELD CHAR(10),
2 OTHER CHAR(70);
DCL 1 DISK_REC,
2 DELETE_FLAG CHAR(1),
2 DISK_MASTER,
3 KEY CHAR(10),
3 OTHER CHAR(70);
DISK_MASTER = INPUT_REC;
/* STRUCTURE ASSIGNMENT */

99
EXAMPLE2 :
DCL 1 A,
2B FIXED DEC(5),
2C FIXED BIN(31),
2D CHAR(20),
2E FIXED BIN(15),
2F FLOAT DEC(5);
DCL 1 AA,
2 BB FIXED BIN(15),
2 CC FIXED DEC(5),
2 DD CHAR(10),
2 EE FIXED DEC(4),
2 FF FIXED DEC(6);
A = AA; /* STRUCTURE ASSIGNMENT */

100
STORAGE CLASSES

Types

AUTOMATIC
STATIC
BASED
CONTROLLED

101
AUTOMATIC STORAGE

Unless declared to have another storage class, most


variables will have the AUTOMATIC storage class.

Storage remains allocated as long as the procedure in


which it was allocated remains active.

Storage allocated dynamically during procedure


execution. It provides efficient use of main storage.
Automatic variables are reinitialised at each activation
of the block which they are declared.

The allocation of dynamic storage is performed by a


routine called prologue. The release of main storage
that has been allocated to AUTOMATIC variables is
102
handled by a routine epilogue.
STATIC STORAGE

Whenever the value of a variable must be saved


between different invocations of the same
procedure , storage for that variable has to be
allocated statically.

In this case storage allocated before execution of


the program and remains allocated throughout the
entire execution of the program. Defined with
attribute STATIC.

Static variables are initialised only once - before


execution of a program begins.

e.g. DCL TABLE(100) CHAR(10) STATIC; 103


BASED STORAGE

The storage class of a variable determines the way in


which the address of the variable is obtained. With
BASED storage the address the address is contained
in a pointer variable .

e.g. DCL P POINTER;


DCL A(100) FIXED DEC(5) BASED (P);

BASED (P) Indicates that the address of the A array


is determined by the contents of P.
i.e., Storage address found in P; the contents of P
have not been established yet.
104
POINTER VARIABLES

A pointer variable is a special type of variable you can


use to locate data - i.e. to ‘point’ to data in main
storage. Consequently, a pointer variable can be thought
of an address.

Before a reference can be made to a based variable , a


value must be given to the pointer . This can be done in
one of the following ways.

By assignment of the value returned by the ADDR


built-in function. This facilitates Overlay-defining of
identifiers that do not have same base, scale and
precision.
105
e.g. DCL VALUE1 BIT (32) BASED (P);
DCL VALUE2 FLOAT (6);
P=ADDR(VALUE2);

By assignment of the value of another pointer.


Facilitates POINTER values for subsequent use by the
program.
e.g. DCL (P,Q) POINTER;
P=ADDR(AREA);
Q=P;
By the SET option of a READ or LOCATE statement.
Facilitates processing of records in their buffers rather
than in storage work areas.
e.g. READ FILE(INPUT) SET(P);
LOCATE FILE (OUTPUT) SET(Q);
106

You might also like