cpan320-lec3

advertisement
CPAN 320 COBOL
Lecture 3
WORKING-STORAGE SECTION of DATA DIVISION and PROCEDURE
DIVISION:
WORKING-STORAGE SECTION:
This section used to defined and describe any field that is not part of an input
file or output file. The rules of coding WORKING-STORAGE SECTTION are:
 When coded it must follow the FILE SECTION of DATA DIVISION.
 The WORKING-STORAGE SECTION must be coded on a separate
line started in area A and end with a period.
 After that a group item that can be divided into sub item representing
the memory area needed for processing the data that are not part of
the input or the output file.
 The names associated with the group and elementary items must
confirm to defining data items rules.
 Each elementary item must contain a PIC clause.
 Each elementary item may be initialized using VALUE
clause(explained later).
For example:
A
B
WORKING-STORAGE SECTION.
01
WS-STORAGE-AREAS.
05 ARE-THERE-MORE-RECORDS PIC X(3) VALUE ‘YES’.
Variables and Constants:
All the items that are defined in the FILE SECTION that are either group
or elementary items are considered variables because the data the may
contain will vary from one record to another that the program will either
read from input file or write to output file.
On the other hand a constant is a data item used in the program that
never change through the program execution. Constant coded directly in
the program either directly in the PROCEDURE DIVISION or in the
WORKING-STORAG SECTION
For example we may have the following lien of code in the PROCEDURE
DIVISION
MULTIPLY INCOME-IN by .85
GIVING NET-INCOME-OUT.
Where INCOME-IN and NET-INCOME-OUT are defined in the FILE
SECTION of DATA DIVISION.
Instead of coding .85 directly into the PROCEDURE DIVISION we can
define a constant in the WORKING-STORAGE SECTION as follow:
1
A
B
WORKING-STORAGE SECTION.
01
WS-AREAS.
05 TAX-RATE
PIC 9V99
or
A
B
WORKING-STORAGE SECTION.
01
TAX-RATE
PIC 9V99
VALUE .85.
VALUE .85.
And then use it in the procedure division as follows:
MULTIPLY INCOME-IN by TAX-RATE
GIVING NET-INCOME-OUT.
There are three types of constants in COBOL:
 Numeric literal:
Numeric constant used in the arithmetic operations (usually used in
PROCEDRUE DIVISION). The rules for forming a numeric literal
are:
1. 1-16 digits
2. + or – sign may be used, they must appears on the left side
of the number.
3. A decimal point is permitted, but it can’t be the rightmost
character of the number.
The following are correct numeric literals:
+234.6
-.5
22
The following are incorrect numeric literal for the reasons explained
in front of each.
9,999.00
comma is not permitted.
32.
decimal point can’t be the rightmost character of a
number.
$5000
Dollar sign is not permitted.
145.6+ or – sign can’t appear on the right side of a
number.

Nonnumeric literal:
It is a constant that is used in PROCEDURE DIVISION for nonarithmetic operations. Following are the forming rules for
nonnumeric literals:
1. The literal must be enclosed in quotation marks.
2. 1-160 characters length (120 in COBOL 74).
3. Any character is permitted in a nonnumeric literal except a
quotation mark.
The following are correct nonnumeric literals:
‘YES’
‘1234.99’
‘REPORT TITLE’
for example:
2
A
B
WORKING-STORAGE SECTION.
01
WS-STORAGE-AREAS.
05 TITLE PIC X(25).
In the procedure division we can have the following code:
MOVE ‘ COMPUTER PROGRAMMER ’ TO TITLE

Figurative literal:
Is a COBOL reserved word, the most used are: ZEROS and
SPACES. ZERO and SPACE can be interchangeable used instead
consecutively. ZEROS is used to fill a numeric field with zeros
where as SPACES is used to fill a nonnumeric field with spaces.
These words can be used either in WORKING-STORAGE section
with VALUE clause, or in the PROCEDURE DIVISION.
The following example shows how to use them in PROCEDURE
DIVISION section
MOVE SPACES TO TITLE
MOVE ZEROS TO AMOUNT
This means that TITLE will be filled with spaces and AMOUTN will
be filled with zeros.
Or instead we can initialize them in the WORKING-STORAGE SECTION
as follow:
A
B
WORKING-STORAGE SECTION.
01
WS-STORAGE-AREAS.
05 TITLE
PIC X(20)
VALUE SPACES.
05 AMOUNT
PIC 99999.99 VALUE ZEROS.
Value clause :
It is used to initialized elementary item and constant literals defined in the
WORKING-STORAGE section.
Continuation of non-numeric literals in value clauses:
Continuation character (_) is used when a nonnumeric literal used with
VALUE clause exceed one line.
The following example shows how to code nonnumeric literal on more
than one line
78
12
WORKING-STORAGE SECTION.
01
WS-STORAGE-AREAS.
05 HEADING PIC X(30)
_
‘ NT REPORT’ .
72
VALUE ‘ EMPLOYEE PAYME
Quotation mark used here
3
Notice that the there is no quotation mark at the end of the first line, but one is
added to the beginning of the second line plus to the one used at the end to
terminate the literal. A continuation character is placed at column 7 of the
second line.
PROCEDURE DIVISION:
PROCEDURE DIVISION is the most significant division in any COBOL
program. It is divided into PARAGRAPHS, each paragraph is an independent
module or routine consist of set of instructions that perform specific task.
PARAGRAPH names are coded in Area A and they must end with a period.
Naming rules are the same as for data items, but PARAGRAPH names in the
procedure division can be all digits. They must be unique and they may not be
a reserved word or an existing data item name.
Each paragraph consists of statements. In COBOL 85 only the last statement
in the paragraph ends with a period where as in COBOL 74 each statement
ends with a period. Statements that end with s period are called sentences.
All statements are coded in area B, and they begin either with a verb (like
MOVE, READ, ACCEPT, DISPLAY, or WRITE) or a condition.
Through the remaining of the course we will be focusing on the PROCEDURE
DIVISION.
Some of the statement coded in the main modules of COBOL batch
programs:


OPEN Statement:
Access the files in the program and indicate which are inputs and
which are outputs. Before you can read from or write to a file, an
OPEN statement must be used to make this file accessible. The
general form of this statement is:
OPEN INPUT
filename-1…
OUTPUT
file-name-2 …
Where {} brackets indicate that items enclosed are required.
PERFORM UNTIL …END-PERFORM Statement:
Used in COBOL 85 only and it executes statements within the
PERFORM UNTIL …END-PERFORM loop until the condition
specified in UNTIL clause is met, then the control is returned to the
statement that directly follows the PERFORM. The general form of
this statement is:
PERFORM
UNTIL condition-1
.
.
.
[END-PERFORM]
4
Where [] indicate that items includes are optional.


READ Statement:
Used after the input file in opened and it transmit data from the
internal file (the input device) assigned in the INPUT-OUTPUT
SECTION of the ENVIROENMENT DIVISION to the storage area
(data items) defined in FILE SECTION of the DATA DIVISION. The
general form of this statement that is used in COBOL 85 is :
READ file-name-1
AT END stsement-1
[NOT AT END stsement-2 ..]
[END READ]
CLOSE Statement:
Used after processing all the records in the program to release the
files and deactivate the devices. The general form of this statement
is:
CLOSE file-name1…

STOP RUN Statement:
Instruct the computer to terminate the program. In COBOL 85
STOP RUN statement will close any opened file before terminating
the program, but you would use it for documentation and
debugging purposes.

Simplified MOVE Statement:
Used to move fields in the main memory to other fields. The basic
form is:
MOVE identifier-1 TO identifier-2

WRITE Statement:
Used to transmit data from output area fields defined in the FIEL
SECTION of DATA DIVISION to the internal output file (the output
device).
Reference: Textbook Ch3, Ch4.
5
Download