PLI
22 July 01:43
Here is the [http://en.wikipedia.org/wiki/PL/I Wikipedia entry].
PL/I is a abounding procedural language.
The program has its access point defined as MAIN, in a action statement, as in:
tree: action options (main);
An appointment example:
a = bIn this numeric appointment statemnet, d is subtracted from c, and the aftereffect assorted by the amount of b, afore autumn in capricious a.
s = t || u;
In this archetype cord assignment, cord u is abutting to the end of cord t, and the blended cord is stored in cord capricious s.
Every animadversion commences with the brace / /
Declarations are not compulsory, admitting alotof PL/I compilers accommodate a compiler advantage to force declarations, while some of the subset compilers assert that every capricious be declared.
To acknowledge i as an integer:
acknowledge i anchored binary;
To acknowledge i as an accumulation and accord it an antecedent amount of 0
acknowledge i anchored bifold antecedent (0);
To acknowledge a capricious as decimal,
acknowledge D anchored decimal;
acknowledge E anchored decimal (10,4);
The first acknowledgment is of an accumulation D stored in decimal format, while the additional is of a capricious E able of autumn ten digits accepting 4 digits afterwards the decimal point. To initialize E, write:
acknowledge E anchored decimal (10,4) antecedent (123456.7891);
Note that ethics such as 0.1 are stored exactly.
To ascertain a floating-point variable:
acknowledge f float;
that uses individual attention hardware; and bifold precision:
acknowledge float decimal (15);
and continued precsion:
acknowledge float decimal (18);
(typical declarations for the PC). The numbers 15 and 18 announce the amount of decimal digits that will be stored. (Although the ethics are infact captivated in binary, the decimal precisions are added acceptable to anamnesis than allegorical the precisions in binary.) To specify a circuitous variable, artlessly cover the chat Circuitous in the declaration, thus:
Acknowledge C FLOAT DECIMAL (15) COMPLEX;
C = 5.2+8i;
The appointment includes the circuitous connected 5.2+8i area 5.2 is the absolute allotment and 8 is the abstract part.
To acknowledge a varible accepting the account attribute:
acknowledge p account (9999v.999);
describes capricious P as accepting as absolute of 7 decimal digits, with three digits afterwards the decimal point. The decimal point is captivated in storage. The letter V indicates the position of the absolute decimal point. For Europe, the blueprint may cover a comma:
acknowledge p account (9999v,999);
To acknowledge capricious S as a appearance string:
acknowledge S character(10);
String S can abundance absolutely 10 characters.
To acknowledge capricious V as a appearance cord able of autumn up to 100 characters:
acknowledge V appearance (100) varying;
A simple assignment:
V= abc;
V holds the three characters abc.
V = friendly;
V holds the 8 characters friendly.
PL/I aswell permits bit strings:
acknowledge b bit(10);
permits bit strings to be stored, of breadth ten. The appointment statement:
b = 1011110010b;
stores the $.25 in the accustomed order. Such bit strings can be searched, etc., just like appearance strings.
Data structures, or aggregates, may be declared:
acknowledge 1 name,
2 first_name appearance (10),
2 surname appearance (15);
Arrays are declared by one of the aloft means, forth with ambit information:
acknowledge x(10) float;
specifies an arrangement of ten elements, x(1) through x(10). The absence lower apprenticed is unity. To specify a altered lower bound:
acknowledge y(0:15) float;
declares a 16-element arrangement Y(0) through Y(15) of floating-point elements.
To handle dates accustomed as 2-digit years and as 4-digit years, possibly intermixed in the aforementioned program , the DATE aspect may be used. acknowledge d date (YY); [more to come]
Describe how methods/functions/procedures are declared and implemented. The afterward archetype is of a complete PL/I program absolute a function.
/ acknowledge (b, c, angle) float;
put account (Today is , date());
put skip account (Please blazon the lengths of two sides, and an angle);
get account (b, c, angle);
put skip account (The two abandon are, b, c, and the bend is, angle, degrees);
put account (Area of triangle=, area(b, c, angle));
area: action (b, c, angle) allotment (float);
acknowledge (b, c, angle) float;
acknowledgment (b end area;
end d;
The aloft program contains some archetypal ascribe and achievement statements of the list-directed form.
The first PUT account prints the date.
The additional requests the clairvoyant to blazon three ethics apery the ambit of some triangle.
The third echos the inputs as confirmation.
The endure PUT account contains a action reference, area(b, c, angle) which invokes the function. If it has been computed, the accepted PUT account prints that area.
The analogue of the action Breadth includes a action account that lists the ambit appropriate from the calling program , namely, b, c, and area. These ambit are authentic in the next account (DECLARE).
The action account aswell states the blazon of the aftereffect to be alternate to the calling program , namely, a floating-point value. This is adumbrated by Allotment (FLOAT).
The Acknowledgment account contains the blueprint to be acclimated in artful the breadth of the triangle. It then allotment ascendancy to the calling program . A Acknowledgment account about includes the name of a variable, but may cover an announcement (as is the case here).
The ambit of PL/I variables is bedfast to procedures in which they are declared. The ambit extends into any procedures independent aural that action (except of advance any procedures in which a new acknowledgment of the aforementioned name occurs).
In the accident that a capricious is not declared, then the ambit of that capricious is the accomplished program . Again, if a acknowledgment (for a capricious of the aforementioned name) appears in a block that is accessory to the one(s) area the capricious is (are) used, then the ambit is afar from the block absolute the declaration.
PL/I has two kinds of blocks. One is the action block that is acclimated for subroutines and functions. The additional is the Activate block (BEGIN; ... END;). The Activate block behaves as a action block, except that a Activate block has no ambit nor does it accept to accept a label. It is entered in accustomed consecutive execution, admitting a action block can be accomplished alone by active a Alarm account or action reference.
Consider the two examples of condiional statements:
if a = b then c = d;
if a > b then c = d; abroad c = e;
The first archetype illustrates one optionally-executed account c = d;
The additional archetype shows two optionally-executed statements, the first of which is accomplished if a > b, and the additional of which is accomplished if a <= b;
When assorted statements are to be accomplished conditionally, account brackets do and end are used.
if a > b then
do; p = q; r = s; end;
The basal anatomy of looping account is:
do k = 1 to 10;
<<body>>
end;
The physique of the bend is accomplished ten times, with ascendancy capricious k demography the ethics from 1 through 10. On bend exit, the amount of k is one accession more, namely 11. The bend extends to the next END statement. Variations of looping statements are:
do k = 1 to n by 2;
do k = 20 to 1 by -1;
Multiple accepted blueprint may be used:
do k = 1 to 10, 20 to 100 by 5, 120 to 200 by 20;
in which the bend will be accomplished with ascendancy capricious k demography the ethics 1 through 10, then 20, 25, 30, 35, ... 100, 120, 140, 160, 180, and 200.
A bend that is conditionally concluded uses the WHILE construct:
do while (a > b);
Such a bend is accomplished aught or added times, the analysis getting performed at the alpha of the loop.
do until (a > b);
The bend defined by the aloft account is accomplished at atomic once, the analysis getting performed at the end of the loop.
In the following, a Echo is acclimated to specify an abnormal change in the amount of the ascendancy variable.
do k = 1 echo 2In this case, the ascendancy capricious k takes on the ethics 1, 2, 4, 8, 16, and so on, until K becomes greater than 1000.
The Echo form:
do next = arch echo next > hotlink until (link = null);
may be acclimated to bisect a affiliated list.
put account (Hello world!);
put skip account (Hello world!);
The first archetype writes Accost world! on the accepted achievement (a awning or printer). The words are printed afterwards any achievement already on that line. The additional archetype starts a new band (the chat SKIP) afore printing.
PL/I provides absurdity apprehension and absurdity handling.
Errors that are detected include
The user can admittance the arrangement to address the absurdity and accomplish a absence action , or the user can specify a computational activity to be accomplished if a accustomed absurdity is detected.
Events (including errors) are termed conditions.
For a action to be detected, it haveto first be enabled. If the action is enabled, and the agnate action occurs, the agnate ON-unit (if supplied by the user) is executed.
Consider the afterward code:
on overflow breeze go to restart;
a = b ...
restart:
In the accident that the artefact b
The keyword Breeze causes the active program to book an absurdity message, forth with the account amount area the absurdity occurred.
A added accepted archetype follows:
(SUBSCRIPTRANGE):
a: procedure;
acknowledge x(10) float;
acknowledge (i, k) anchored binary;
ON SUBSCRIPTRANGE Breeze BEGIN; PUT Data (K); STOP; END;
get account (k);
do i = 1 to k;
x(i) = 0;
end;
end a;
In this example, the absurdity apprehension and administration locations are apparent in high case.
Subscript blockage is enabled by the action prefix SUBSCRIPTRANGE (parenthesised afore the action statement).
Before any statements are executed, the ON account haveto be executed, which establishes the activity to be agitated out in the accident of a subscript error.
The next account accomplished is the ascribe account GET, which reads in a value, say 12.
The bend is entered, and initializes ten ethics of x. On attempting to initialize x(11), the SUBSCRIPTRANGE action is raised, and causes the ON assemblage BEGIN; PUT Data (K); STOP; END; to be executed. Breeze causes an absurdity bulletin to be printed forth with the account number, and then statements aural the Activate block are executed, namely the PUT Data statement, which prints K = 11. The program then STOPs. Some specific altitude are:
OVERFLOW exponent overflow
UNDERFLOW exponent underflow
FIXEDOVERFLOW fixed-point overflow
SIZE fixed-point overflow
ZERODIVIDE division by zero
SUBSCRIPTRANGE subscript apprenticed error
STRINGRANGE substring error
STRINGSIZE truncation of a string
ENDFILE end of file
ENDPAGE end of page
ATTENTION request from keyboard
Some altitude are provided to handle accustomed events, for example, end of book and end of page. For example, to book a page amount on the top of anniversary page:
ON ENDPAGE PUT Page LIST(Page, Page_no);
To ascertain the end of the ascribe book IN, and to alteration to a called statement:
ON ENDFILE(IN) go to next;
The program mer can ad-lib his own conditions, and can arresting them.
The Arresting account is provided to analysis a accustomed ON-unit (error-handler) during program testing. For example, Arresting ZERODIVIDE;
To accession a user-defined action alleged RANGE, the account Arresting CONDITION(RANGE); would be used.
Here is the [http://en.wikipedia.org/wiki/PL/I Wikipedia entry].
PL/I is a abounding procedural language.
The program has its access point defined as MAIN, in a action statement, as in:
tree: action options (main);
An appointment example:
a = bIn this numeric appointment statemnet, d is subtracted from c, and the aftereffect assorted by the amount of b, afore autumn in capricious a.
s = t || u;
In this archetype cord assignment, cord u is abutting to the end of cord t, and the blended cord is stored in cord capricious s.
Every animadversion commences with the brace / /
Declarations are not compulsory, admitting alotof PL/I compilers accommodate a compiler advantage to force declarations, while some of the subset compilers assert that every capricious be declared.
To acknowledge i as an integer:
acknowledge i anchored binary;
To acknowledge i as an accumulation and accord it an antecedent amount of 0
acknowledge i anchored bifold antecedent (0);
To acknowledge a capricious as decimal,
acknowledge D anchored decimal;
acknowledge E anchored decimal (10,4);
The first acknowledgment is of an accumulation D stored in decimal format, while the additional is of a capricious E able of autumn ten digits accepting 4 digits afterwards the decimal point. To initialize E, write:
acknowledge E anchored decimal (10,4) antecedent (123456.7891);
Note that ethics such as 0.1 are stored exactly.
To ascertain a floating-point variable:
acknowledge f float;
that uses individual attention hardware; and bifold precision:
acknowledge float decimal (15);
and continued precsion:
acknowledge float decimal (18);
(typical declarations for the PC). The numbers 15 and 18 announce the amount of decimal digits that will be stored. (Although the ethics are infact captivated in binary, the decimal precisions are added acceptable to anamnesis than allegorical the precisions in binary.) To specify a circuitous variable, artlessly cover the chat Circuitous in the declaration, thus:
Acknowledge C FLOAT DECIMAL (15) COMPLEX;
C = 5.2+8i;
The appointment includes the circuitous connected 5.2+8i area 5.2 is the absolute allotment and 8 is the abstract part.
To acknowledge a varible accepting the account attribute:
acknowledge p account (9999v.999);
describes capricious P as accepting as absolute of 7 decimal digits, with three digits afterwards the decimal point. The decimal point is captivated in storage. The letter V indicates the position of the absolute decimal point. For Europe, the blueprint may cover a comma:
acknowledge p account (9999v,999);
To acknowledge capricious S as a appearance string:
acknowledge S character(10);
String S can abundance absolutely 10 characters.
To acknowledge capricious V as a appearance cord able of autumn up to 100 characters:
acknowledge V appearance (100) varying;
A simple assignment:
V= abc;
V holds the three characters abc.
V = friendly;
V holds the 8 characters friendly.
PL/I aswell permits bit strings:
acknowledge b bit(10);
permits bit strings to be stored, of breadth ten. The appointment statement:
b = 1011110010b;
stores the $.25 in the accustomed order. Such bit strings can be searched, etc., just like appearance strings.
Data structures, or aggregates, may be declared:
acknowledge 1 name,
2 first_name appearance (10),
2 surname appearance (15);
Arrays are declared by one of the aloft means, forth with ambit information:
acknowledge x(10) float;
specifies an arrangement of ten elements, x(1) through x(10). The absence lower apprenticed is unity. To specify a altered lower bound:
acknowledge y(0:15) float;
declares a 16-element arrangement Y(0) through Y(15) of floating-point elements.
To handle dates accustomed as 2-digit years and as 4-digit years, possibly intermixed in the aforementioned program , the DATE aspect may be used. acknowledge d date (YY); [more to come]
Describe how methods/functions/procedures are declared and implemented. The afterward archetype is of a complete PL/I program absolute a function.
/ acknowledge (b, c, angle) float;
put account (Today is , date());
put skip account (Please blazon the lengths of two sides, and an angle);
get account (b, c, angle);
put skip account (The two abandon are, b, c, and the bend is, angle, degrees);
put account (Area of triangle=, area(b, c, angle));
area: action (b, c, angle) allotment (float);
acknowledge (b, c, angle) float;
acknowledgment (b end area;
end d;
The aloft program contains some archetypal ascribe and achievement statements of the list-directed form.
The first PUT account prints the date.
The additional requests the clairvoyant to blazon three ethics apery the ambit of some triangle.
The third echos the inputs as confirmation.
The endure PUT account contains a action reference, area(b, c, angle) which invokes the function. If it has been computed, the accepted PUT account prints that area.
The analogue of the action Breadth includes a action account that lists the ambit appropriate from the calling program , namely, b, c, and area. These ambit are authentic in the next account (DECLARE).
The action account aswell states the blazon of the aftereffect to be alternate to the calling program , namely, a floating-point value. This is adumbrated by Allotment (FLOAT).
The Acknowledgment account contains the blueprint to be acclimated in artful the breadth of the triangle. It then allotment ascendancy to the calling program . A Acknowledgment account about includes the name of a variable, but may cover an announcement (as is the case here).
The ambit of PL/I variables is bedfast to procedures in which they are declared. The ambit extends into any procedures independent aural that action (except of advance any procedures in which a new acknowledgment of the aforementioned name occurs).
In the accident that a capricious is not declared, then the ambit of that capricious is the accomplished program . Again, if a acknowledgment (for a capricious of the aforementioned name) appears in a block that is accessory to the one(s) area the capricious is (are) used, then the ambit is afar from the block absolute the declaration.
PL/I has two kinds of blocks. One is the action block that is acclimated for subroutines and functions. The additional is the Activate block (BEGIN; ... END;). The Activate block behaves as a action block, except that a Activate block has no ambit nor does it accept to accept a label. It is entered in accustomed consecutive execution, admitting a action block can be accomplished alone by active a Alarm account or action reference.
Consider the two examples of condiional statements:
if a = b then c = d;
if a > b then c = d; abroad c = e;
The first archetype illustrates one optionally-executed account c = d;
The additional archetype shows two optionally-executed statements, the first of which is accomplished if a > b, and the additional of which is accomplished if a <= b;
When assorted statements are to be accomplished conditionally, account brackets do and end are used.
if a > b then
do; p = q; r = s; end;
The basal anatomy of looping account is:
do k = 1 to 10;
<<body>>
end;
The physique of the bend is accomplished ten times, with ascendancy capricious k demography the ethics from 1 through 10. On bend exit, the amount of k is one accession more, namely 11. The bend extends to the next END statement. Variations of looping statements are:
do k = 1 to n by 2;
do k = 20 to 1 by -1;
Multiple accepted blueprint may be used:
do k = 1 to 10, 20 to 100 by 5, 120 to 200 by 20;
in which the bend will be accomplished with ascendancy capricious k demography the ethics 1 through 10, then 20, 25, 30, 35, ... 100, 120, 140, 160, 180, and 200.
A bend that is conditionally concluded uses the WHILE construct:
do while (a > b);
Such a bend is accomplished aught or added times, the analysis getting performed at the alpha of the loop.
do until (a > b);
The bend defined by the aloft account is accomplished at atomic once, the analysis getting performed at the end of the loop.
In the following, a Echo is acclimated to specify an abnormal change in the amount of the ascendancy variable.
do k = 1 echo 2In this case, the ascendancy capricious k takes on the ethics 1, 2, 4, 8, 16, and so on, until K becomes greater than 1000.
The Echo form:
do next = arch echo next > hotlink until (link = null);
may be acclimated to bisect a affiliated list.
put account (Hello world!);
put skip account (Hello world!);
The first archetype writes Accost world! on the accepted achievement (a awning or printer). The words are printed afterwards any achievement already on that line. The additional archetype starts a new band (the chat SKIP) afore printing.
PL/I provides absurdity apprehension and absurdity handling.
Errors that are detected include
The user can admittance the arrangement to address the absurdity and accomplish a absence action , or the user can specify a computational activity to be accomplished if a accustomed absurdity is detected.
Events (including errors) are termed conditions.
For a action to be detected, it haveto first be enabled. If the action is enabled, and the agnate action occurs, the agnate ON-unit (if supplied by the user) is executed.
Consider the afterward code:
on overflow breeze go to restart;
a = b ...
restart:
In the accident that the artefact b
The keyword Breeze causes the active program to book an absurdity message, forth with the account amount area the absurdity occurred.
A added accepted archetype follows:
(SUBSCRIPTRANGE):
a: procedure;
acknowledge x(10) float;
acknowledge (i, k) anchored binary;
ON SUBSCRIPTRANGE Breeze BEGIN; PUT Data (K); STOP; END;
get account (k);
do i = 1 to k;
x(i) = 0;
end;
end a;
In this example, the absurdity apprehension and administration locations are apparent in high case.
Subscript blockage is enabled by the action prefix SUBSCRIPTRANGE (parenthesised afore the action statement).
Before any statements are executed, the ON account haveto be executed, which establishes the activity to be agitated out in the accident of a subscript error.
The next account accomplished is the ascribe account GET, which reads in a value, say 12.
The bend is entered, and initializes ten ethics of x. On attempting to initialize x(11), the SUBSCRIPTRANGE action is raised, and causes the ON assemblage BEGIN; PUT Data (K); STOP; END; to be executed. Breeze causes an absurdity bulletin to be printed forth with the account number, and then statements aural the Activate block are executed, namely the PUT Data statement, which prints K = 11. The program then STOPs. Some specific altitude are:
OVERFLOW exponent overflow
UNDERFLOW exponent underflow
FIXEDOVERFLOW fixed-point overflow
SIZE fixed-point overflow
ZERODIVIDE division by zero
SUBSCRIPTRANGE subscript apprenticed error
STRINGRANGE substring error
STRINGSIZE truncation of a string
ENDFILE end of file
ENDPAGE end of page
ATTENTION request from keyboard
Some altitude are provided to handle accustomed events, for example, end of book and end of page. For example, to book a page amount on the top of anniversary page:
ON ENDPAGE PUT Page LIST(Page, Page_no);
To ascertain the end of the ascribe book IN, and to alteration to a called statement:
ON ENDFILE(IN) go to next;
The program mer can ad-lib his own conditions, and can arresting them.
The Arresting account is provided to analysis a accustomed ON-unit (error-handler) during program testing. For example, Arresting ZERODIVIDE;
To accession a user-defined action alleged RANGE, the account Arresting CONDITION(RANGE); would be used.
Any debris accumulating is automatic.
The libraries of the accepted functions are all automatically provided; it is accidental to do annihilation to access admission to them, such as to specify directories or to use Cover statements or the like.
For an exact equivalant, use the aspect VARYINGZ, in which strings are zero-terminated.
Without the aspect VARYING, strings are of anchored length. Such strings consistently abundance the defined amount of characters. Thus,
Acknowledge S CHARACTER(8);
S = abc;
stores eight characters, namely, abc followed by 5 blanks, whereas
acknowledge V character(8) varying;
V = abc;
stores alone three characters. There is no added with blanks. This closing affection makes accessible the assignment
V = V || dog;
which appends the chat dog to what is already stored in V (up to a best of 8 characters in this example, of course).
acknowledge i anchored binary;
is agnate to Cs int;
acknowledge i anchored bifold (31);
is agnate to Cs continued int.
acknowledge i anchored bifold (63);
is agnate to Cs continued continued int.
The congenital functions ADD, SUBTRACT, MULTIPLY, and Bisect are provided to accredit the programmer to specify the attention of anniversary result. Appropriately they advice to abstain overflow. For decimal fixed-point division, it is acerb recommended that the Bisect action be used. Thus, to bisect A by B, area A has attention (10,5), and B has attention (6), the recommended anatomy is:
acknowledge A anchored decimal (10,5), B anchored decimal(6), C anchored decimal (10,5);
A = 12345.67891; b = 98765;
C = divide(A, B, 10, 5);
In the Bisect reference, the argments 10 and 5 absolute that the aftereffect A/B has a absolute of ten digits, with 5 digits afterwards the decimal point, respectively. This amount is then stored in C. IBM PL/I permits up to 31 decimal digits for fixed-point working.
(SIZE, STRINGRANGE, STRINGSIZE, SUBSCRIPTRANGE):
trial: action options (main);
This enables blockage for fixed-point overflow, and range-checking and truncation for strings and for subscripts.
|
Tags: program, angle, three, point, example, assignment, block, character, complex, characters, procedures, includes, contains, calling, statements, value, stored, float, strings, procedure declare, decimal, variable, statement, float, angle, procedure, string, program, block, point, character, declared, assignment, declaration, digits, example, function, executed, fixed, stored, complex, procedures, strings, characters, parameters, binary, scope, second, calling, statements, return, returns, triangle, contains, having, integer, declarations, storing, value, initial, values, picture, includes, floating, three, , decimal point, calling program, procedure block, bit strings, floating point, fixed decimal, procedure statement, float decimal, calling program namely, declare float decimal, |
Also see ...
PermalinkArticle In : Reference & Education - Book