Professional Documents
Culture Documents
PLI Training Day 1
PLI Training Day 1
Day 1
Basics
Variables, Data types and Constants
Expressions, Assignments and
Operators
Program Flow and Control
Day 2
Arrays
Structures
Procedures
Day 3
I/O
Bit Data
Condition and Error Handling
Built-in Functions
Day 4
Addresses and Pointers
Preprocessor Facilities
Other Features
Storage Control
PL/I Training
DAY 1
Preface
Such were the changes necessary that it was not possible to introduce the
new features needed to bring FORTRAN up-to-date, and to be compatible
with existing programs, that it was decided to introduce a new language that
incorporated the best features of the then main languages FORTRAN,
COBOL, and Algol.
The new language contained features not then seen before in a generalpurpose programming language -- including interrupt handling, array
operations, list processing, and a macro pre-processor.
PL/I Training
Basics
Basics
Character Sets
Statement Elements
Statements
Groups
PL/I programs can contain characters from either the single-byte character set or
the double-byte character set or both. Languages that require large character
sets, such as Japanese, use DBCS.
SBCS
Alphanumeric
Alphabetic
Special
Digits
Alphabetic Characters
A-Z (lower and upper allowed) and the three extralingual characters $, # and @
Digits
0-9
Meaning
Character
Meaning
Character
Meaning
Blank
Right
Parenthesis
NOT
&
AND
Equal
Plus
Comma
OR
Minus
Point
>
Greater than
Asterisk
Single Quote
Slash
<
Less than
Left
Parenthesis
Percent
Break
Semicolon
Colon
Question
mark
A statement consists of
Identifiers
Delimiters
and
Operators
Constants
iSubs
10
Basics - Identifiers
An identifier is a string of characters, not contained in a comment or constant,
and preceded and followed by a delimiter.
The first character of the identifier must be one of the 29 alphabetic characters.
The others, if any, can be extended alphabetic, numeric, or the break
character.
Identifier
Key Word
Range Specification
11
Basics - Identifiers
In the PL/I language, the compiler can determine from context if an identifier is
a keyword.
Hence you can use any identifier as a programmer-defined name.
There are no reserved words in PL/I.
12
In a PL/I program, names are given to variables and program control data.
There are also built-in names, condition names, and generic names.
In creating a name, you must observe the syntax rules for creating an identifier.
Any identifier can be used as a name.
e.g. of valid programmer names
GROSS_PAY
FILE2
A
#32
RATE_OF_PAY
LOOP_3
13
Basics - Keywords
A keyword is an identifier that, when used in the proper context, has a specific
meaning to the compiler.
Keywords can specify such things as the action to be taken or the attributes of
data.
For example, READ, DECIMAL, and ENDFILE are keywords when used in
proper context.
Some keywords can be abbreviated.
14
15
Basics - Delimiters
Name
Delimiter Use
Comment
/* */
Comma
Period
Semicolon
Terminates statements
Assignment
Indicates assignment
Colon
Blank
Parentheses
()
Pointer
->
Percent Symbol
Asterisk
Single Quote
16
Basics - Delimiters
Inadvertent omission of certain symbols can give rise to errors that are difficult
to trace.
Common errors are unbalanced quotes, unmatched parentheses, unmatched
comment delimiters (for example, "/* instead of "*/" when closing a comment),
and missing semicolons.
17
Basics - Operators
Operator Type
Characters
Description
Arithmetic
Arithmetic
Arithmetic
Multiplication
Arithmetic
Division
Arithmetic
**
Exponentiation
Comparison
>
Greater Than
Comparison
>
Comparison
>=
Comparison
Equal To
Comparison
Not Equal To
Comparison
<=
Comparison
<
Less Than
Comparison
<
Bit Operators
Not
Bit Operators
&
And
Bit Operators
Or
String Operator
||
Concatenation
18
Basics - Blanks
AB+BC is equivalent to AB + BC
19
Basics - Comments
A comment
*/
End of
Start of
Text
20
Basics - Comments
5;
21
Basics - Statements
22
23
24
25
Statement Body
Simple Statement Body
26
27
/* keyword statement */
A = B + C;
/* assignment statement */
LABEL:;
%SKIP(2);
/* keyword statement */
28
Compound statements are all keyword statements; each begins with a keyword
which indicates the function of the statement.
A compound statement contains one or more simple or compound statements
as a part of its statement body.
There are four compound statements: IF, ON, WHEN, and OTHERWISE.
A compound statement is terminated by the semicolon that also terminates the
final statement in the statement body of the compound statement.
29
30
% Statements
Some PL/I statements begin with a percent symbol, (%).
These statements direct the operation of the compiler (controlling compiler
listings, or including program source text from a library.)
The preprocessor, also uses statements beginning with a % symbol.
31
Statement
The *PROCESS statement is the only statement that begins with an asterisk
(*).
The *PROCESS statement is a synonym for the %PROCESS statement and is
used to override compile-time options.
32
Basics - Groups
33
Exercise 1
34
PL/I Training
35
Variables
A data item is the value of either a variable or a constant (these terms are not
exactly the same as in general mathematical usage).
Data items can be single items, called scalars, or they can be a collection of
items called data aggregates.
A variable has a value or values that can change during execution of a
program. A variable is introduced by a declaration, which declares the
name and certain attributes of the variable.
36
Variables
A constant has a value that cannot change. Constants for problem data are
referred to by stating the value of the constant. Constants for program control
data are referred to by name.
37
Variables
Problem data
Used to represent values to be processed by a program. It consists of
the coded arithmetic and string data types.
38
Variables - Examples
PI = 3.1416;
AREA = RADIUS**2*PI;
GET LIST(B);
LOOP: A=2*B;
C=B+6;
Variables
PI
AREA
RADIUS
Variables
Constants
2
2, 6
A, B, C
Constants
LOOP
LOOP represents a constant; the
value of the constant is the address
of the A=2*B statement
Pl/I Training - Day 1
39
The attributes of a variable or a program control data constant are not apparent
in the name.
Since PL/I must know these attributes, you can use keywords and expressions
to specify the attributes explicitly in a DECLARE statement.
Attributes can also be set contextually or by programmer-defined or languagespecified defaults.
40
Classification of Attributes.doc
41
42
Arithmetic data items have the data attributes of base, scale, precision, and
mode.
43
DECIMAL
BINARY
Abbreviations
BIN for BINARY
DEC for DECIMAL
44
45
Variables Precision
For fixed-point data items, the precision attribute can also specify the
scaling factor (the assumed position of the decimal or binary point), relative
to the rightmost digit of the number.
(number of digits, scaling factor or 0)
46
47
48
Constant Precision
3.1416
(5,4)
455.3
(4,1)
732
(3,0)
003
(3,0)
5280
(4,0)
.0012
(4,4)
49
50
51
52
Constant Precision
10110B
(5,0)
11111B
(5,0)
101B
(3,0)
1011.111B
(7,3)
53
54
55
56
Constant Precision
15E-23
(2)
15E23
(2)
4E-3
(1)
1.96E+07
(3)
438E0
(3)
3141593E-6
(7)
.003141593E3
(9)
57
58
59
60
Constant Precision
101101E5B
(6)
101.101E2B
(6)
11101E-28B
(5)
11.01E+42B
(4)
61
62
63
64
65
66
67
68
69
70
71
When declared with the CHARACTER attribute, a character value can include
any digit, letter, special character, blank, or any other of the 256 EBCDIC
codes.
When declared with the PICTURE attribute, the character value assigned must
match the picture-specification. Each character of a character value occupies 1
byte of storage.
72
73
Constant Length
'LOGARITHM TABLE'
'PAGE 5'
15
6
'SHAKESPEARE''S ''HAMLET'''
'AC438-19'
22
27
(2)'WALLA '
12
74
75
'D7D361C9'X
(2)'C1'X
(2)'C1'X
'7D'X
''X
is
is
is
is
is
the
the
the
the
the
same
same
same
same
same
as
as
as
as
as
'PL/I'
(2)'A'
'AA'
''''
''
76
When declared with the BIT attribute, a bit value can contain bits 0 and 1.
A collection of 8 or less unaligned bits occupy 1 byte of storage.
77
78
Constant Length
'1'B
'11111010110001'B 14
(64)'0'B
''B
64
0
79
80
'CA'B4
'80'B4
1'B4
(2)'F'B4
(2)'F'B4
''B4
is
is
is
is
is
is
the
the
the
the
the
the
same
same
same
same
same
same
as
as
as
as
as
as
'11001010'B
'10000000'B
'0001'B
'11111111'B
(2)'1111'B
''B
81
area
entry
event
file
label
pointer
offset
task
82
Variables Alignment
ALIGNED specifies that the data element is aligned on the storage boundary
corresponding to its data-type requirement.
Alignment Requirements.doc
UNALIGNED specifies that each data element is mapped on the next byte
boundary, except for fixed-length bit strings, which are mapped on the next bit.
83
DECLARE 1 S,
2 X BIT(2),
2 A ALIGNED,
3 B,
3 C UNALIGNED,
/*
/*
/*
/*
UNALIGNED BY DEFAULT */
ALIGNED EXPLICITLY */
ALIGNED FROM A */
UNALIGNED EXPLICITLY */
4 D,
/* UNALIGNED FROM C */
4 E ALIGNED,
/* ALIGNED EXPLICITLY */
4 F,
/* UNALIGNED FROM C */
3 G,
/* ALIGNED FROM A */
2 H; /* ALIGNED BY DEFAULT */
84
Exercise
85
PL/I Training
86
Expressions - Assignment
A = B;
Target
Assignment
Operator
The results of an expression evaluation or of a conversion are assigned to a
target.
87
Expressions - Pseudovariables
88
89
Operational Expression
Arithmetic Operation
Bit Operation
Comparison Operation
Concatenation Operation
90
Add
Subtract
Multiply
Divide
Exponentiation
The plus sign and the minus sign can appear as prefix operators or as infix
operators.
All other arithmetic operators can appear only as infix operators.
91
92
93
94
&
|
Not
Or
End
The first operator, the not symbol, can be used as a prefix operator only.
The second and third operators, the and symbol and the or symbol, can be
used as infix operators only.
(The operators have the same function as in Boolean algebra).
95
A&B
A|B
96
The
The
The
The
value
value
value
value
of
of
of
of
operand
operand
operand
operand
A
C
C & B
A | B
C | B
A | (C)
((C)|(B))
SUBSTR(A,1,1)|(D=5)
A
B
C
D
is
is
is
is
yields
yields
yields
yields
yields
yields
yields
yields
'010111'B
'111111'B
'110'B
5
'101000'B
'001'B
'110000'B
'111111'B
'111111'B
'011111'B
'110111'B
'1'B
97
98
99
IF A = B THEN
action-if-true;
ELSE action-if-false;
The evaluation of the expression A = B yields either '1'B, for true, or '0'B, for
false.
X = A <= B;
The value '1'B is assigned to X if A is less than B; otherwise, the value '0'B is
Assigned.
100
101
value
value
value
value
of
of
of
of
operand
operand
operand
operand
A||B
yields
A||A||B
C||D
yields
D||C
yields
B||D
yields
A
B
C
D
is
is
is
is
'010111'B
'101'B
'XY,Z'
'AA/BB'
'010111101'B
yields '010111010111101'B
'XY,ZAA/BB'
'AA/BBXY,Z'
'101AA/BB'
102
Variables - Exercise
103
Variables - Exercise
104
Priority of Operators.doc
105
PL/I Training
106
Program Organization
107
Program Organization
108
Blocks
Procedure Blocks
Or
Procedures
Begin Blocks
109
Begin-blocks and procedures can contain declarations that are treated as local
definitions of names.
This is done to determine the scope of the declaration of names and to limit
the allocation of automatic variables.
These declarations are not known outside their own block, and the names
cannot be referred to in the containing block.
Automatic storage is allocated upon entry to the block where the storage is
declared. The storage is freed upon exit from the block.
110
Although the begin-block and the procedure play the same role in the allocation
and freeing of storage and in delimiting the scope of names, they differ in the
way they are activated and executed:
Except for the main procedure, external and internal procedures contained
in a program are activated only when they are invoked by a procedure
reference.
111
During block-activation:
Currently active blocks known to the procedure are identified, so that the
correct generations of automatic storage are accessible, and the correct
ON-units can be entered.
112
113
A: PROCEDURE;
.
B: BEGIN;
.
END B;
.
C: PROCEDURE;
.
D: BEGIN;
.
E: PROCEDURE;
.
END E;
.
END D;
END C; .
END A;
External Procedure
Begin-block
Internal Procedure
Begin Block
Internal Procedure
114
NAME: A: PROCEDURE;
.
.
.
END NAME;
The leftmost label of the PROCEDURE statement represents the primary entry
point of the procedure. Optionally, additional labels define secondary entry
points. The ENTRY statement also defines secondary entry points.
For example:
B: ENTRY;
Pl/I Training - Day 1
115
Any ENTRY statements encountered during sequential flow are not executed;
control flows around the ENTRY statement.
Label need not appear after the keyword END in the END statement, but if one
does appear, it must match the label of the PROCEDURE statement to which
the END statement corresponds.
116
117
Different types of DO
DO;
DO WHILE(exp1) UNTIL(exp2);
DO WHILE(exp1) UNTIL(exp2);
DO reference = exp1 TO exp2 BY exp3 WHILE(exp4) UNTIL(exp5);
DO reference = exp1 BY exp3 TO exp2 WHILE(exp4) UNTIL(exp5);
DO REPEAT exp6 WHILE(exp4) UNTIL(exp5);
118
119
120
121
DO I = 2 TO 10 BY 2;
The do-group is executed five times, with the value of I equal to 2, 4, 6, 8, and
10.
DO I = 10 TO 1 BY -1;
This do-group is executed 10 times with the control variable being decremented
from 10 to 1.
122
DO I = 1 REPEAT 2*I;
.
.
END;
This do-group is executed with I equal to 1, 2, 4, 8, 16, and so on.
123
I=2;
DO I=1 TO I+3 BY I;
.
.
END;
The do-group is executed with I equal to 1, 3, 5
And is equivalent to
DO I=1 TO 5 BY 2;
.
.
END;
124
DO WHILE (A=B);
.
.
END;
DO UNTIL (A=B);
.
.
END;
In the absence of other options, a do-group headed by a DO UNTIL statement
is executed at least once, but a do-group headed by a DO WHILE statement
might not be executed at all. That is, the statements DO WHILE (A=B) and DO
UNTIL (A=B) are not equivalent.
125
DO WHILE(A=B) UNTIL(X=10);
If, in the above example, when the DO statement is first encountered, A=B,
the do-group is not executed at all.
If, however, A=B, the do-group is executed.
If, after an execution of the do-group, X=10, no further executions are
performed.
Otherwise, a further execution is performed provided that A is still equal to B.
126
DO I=1 TO 10 UNTIL(Y=1);
In this example, the do-group is executed at least once, with I equal to 1.
If, after an execution of the do-group, Y=1, no further executions are
performed.
Otherwise, the default increment (BY 1) is added to I, and the new
value of I is compared with 10.
If I is greater than 10, no further executions are performed. Otherwise, a new
execution commences.
127
128
DO NAME = 'TOM','DICK','HARRY';
This DO statement specifies that the do-group executes once with the value of
NAME set equal to the value of 'TOM', once with the value of NAME set equal
to the value of 'DICK', and once with the value of NAME set equal to the value
of 'HARRY'.
DO I = 1 TO 10, 13 TO 15;
This do-group executes a total of thirteen times--ten times with the value of I
equal to 1 through 10, and three times with the value of I equal to 13 through
15
129
Conditional Statements
IF
SELECT
130
131
132
IF A = B THEN
C = D;
ELSE
C = E;
133
134
135
All single statements are considered valid and executable except DECLARE,
DEFAULT, END, ENTRY, FORMAT, PROCEDURE, or a % statement.
If a non-executable statement is used, the result can be unpredictable.
OTHER is an alias for OTHERWISE.
136
SELECT;
WHEN (A>B) CALL BIGGER;
WHEN (A=B) CALL SAME;
OTHERWISE CALL SMALLER;
END;
In this example there is no expression following the SELECT.
137
138