Module 3-Comprog3
Module 3-Comprog3
Overview:
This module provides the brief overview of the four divisions of COBOL program are
IDENTIFICATION DIVISION, ENVIRONMENT DIVISION, DATA DIVISION and PROCEDURE DIVISION. Each
division is discussed with examples for better understanding and learning of the COBOL program
Module Objectives:
Learn the basic character set, data types, constant and variables of the COBOL program.
Know and understand the basic syntax and rules of the 4 divisions of the program
Character set
In hierarchy 'Characters' are the lowest. They cannot divide further.
Characters Description
0 to 9 Numeric
+ Plus sign
* Asterisk
Space
- Hyphen or minus
, Comma
$ Currency sign
; Semicolon
( Left parenthesis
) Right parenthesis
“ Quotation marks
' Apostrophe
= Equal sign
: Colon
COBOL Data Types
Numeric and other data types we use with numeric data types
a) 9 –> Numeric – 0-9. Maximum length is 18. This holds actual memory space. Example PIC 9(2)
b) S –> Sign data type – It links a sign to a number. If this is present, the number is signed number.
If this is not present, the number is an unsigned number.
c) + sign -> Plus sign. Used to print (+) as sign
d) – Sign –> Minus sign. Used to print (-) as sign
e) V –> Implied Decimal. Does not hold any memory space. Because it is not used to display but it
is useful for computation.
Example: PIC 9(4)V99 –
If the Value of this variable is defined as 123456, then it is like 1234.56 but it will not be
displayed like 1234.56. If we use this in computation, the value will be 1234.56 and this will take
part in computation
f). (Dot)–> Actual decimal point. Used for display, not for calculation or an arithmetic operation
i) $ -> Dollar symbol – To insert a dollar sign at the first position. we normally use this for
currency
COBOL Variables
Variables in COBOL
A variable is an identifier to hold a particular value or data. It identifies a memory
location.
In COBOL variables are called as Data Names.
It can be a maximum length of 30 characters.
Variable must contain only digits(0-9), letters(A-Z), minus sign and Hyphens(-).
A Variable must not be a reserved word of COBOL.
A Variable should not contain any space in between, start or at end of the variable name.
Example of Variables
Examples of valid Variables are –
XYZ
ABC-123
A12
9ABC
Examples of in-valid Variables are –
123_ABC – It does not allow Hyphen
ACCEPT – This is a reserved word in COBOL
ABC) – It does not allow Special characters
MIK+*89
In the diagram which is shown below, we can see how variables are defined in COBOL.
Literals in COBOL
a) Literals are constants in COBOL. And it is directly hard-coded in the program
b) Literals are of 2 types-
Numeric literal
Numeric literal allows a maximum of 18 numbers. Valid values allowed are
0 to 9 (any number)
One sign only( either + or -) which has to be used in left side only
One decimal only(do not use at the end)
Example – 123
Non-Numeric( Alphanumeric) literal
Maximum 160 characters in length
Must start and end with quotes
Example – “I AM AN EXAMPLE OF NON-NUMERIC LITERAL”
‘123’
Figurative Constants in COBOL
In COBOL Figurative Constants is reserved words. And these constants are predefined(build in) in
COBOL.
CAUTION:
Don’t use HIGH-VALUE/HIGH-VALUES/LOW-VALUE/LOW-VALUES with numeric fields
Divisions of COBOL program
A COBOL program consists of four divisions.
Identification Division
It is the first and only mandatory division of every COBOL program. The programmer and the
compiler use this division to identify the program. In this division, PROGRAM-ID is the only
mandatory paragraph. PROGRAM-ID specifies the program name that can consist 1 to 30
characters.
Environment Division
Environment division is used to specify input and output files to the program. It consists of two
sections −
Configuration section provides information about the system on which the program is
written and executed. It consists of two paragraphs −
o Source computer − System used to compile the program.
o Object computer − System used to execute the program.
Input-Output section provides information about the files to be used in the program. It
consists of two paragraphs −
o File control − Provides information of external data sets used in the program.
o I-O control − Provides information of files used in the program.
Data Division
Data division is used to define the variables used in the program. It consists of four sections −
File section is used to define the record structure of the file.
Working-Storage section is used to declare temporary variables and file structures which
are used in the program.
Local-Storage section is similar to Working-Storage section. The only difference is that
the variables will be allocated and initialized every time a program starts execution.
Linkage section is used to describe the data names that are received from an external
program.
Procedure Division
Procedure division is used to include the logic of the program. It consists of executable
statements using variables defined in the data division. In this division, paragraph and section
names are user-defined.
There must be at least one statement in the procedure division. The last statement to end the
execution in this division is either STOP RUN which is used in the calling programs or EXIT
PROGRAM which is used in the called programs.
This module will follow specific format in typing program syntax. Rules are as follows:
A. Uppercase words are COBOL reserved words that have special meaning to the compiler.
B. Lowercase words are use-defined entries. They are within curly braces { }.
C. Underlined words are required in the paragraph.
D. If punctuation is specified in the format, it is required.
E. Brackets [ ] mean the clause or paragraph is optional.
F. The use of dots or ellipses (…) means that additional entries of the same type may be
included if desired.
IDENTIFICATION DIVISION is the first division of CPBOL program. The function of this
division is to supply information about the program to others who may use it as reference. It
describes the program to potential users. It states the name of the program and other optional
information such as information regarding author, the date the program was written, security.
Etc. its purpose is to identify program and its author and to provide other general information
about the program, such as the dates the program is written and compiled, any program security,
and so forth. They start in column-8 of the COBOL coding sheet. The only required paragraph in
this division is the PROGRAM-ID paragraph while the rest is optional.
TIP:
All paragraph-names start in column 8 and, as indicated above, are optional with
exception of the PROGRAM-ID. The compiler does not process what follows the COBOL words but
only prints the content. Thus, after the DATE-WRITTEN we could have written ANYTIME IN JUNE.
The compiler derives no more meaning from it, therefore, the programmer should be concerned
simply with choosing verbal descriptions that will be meaningful to the potential readers of the
program.
The DATE-COMPILED paragraph may be left blank. The compiler will insert the actual date
and the source listing will include that date.
B. ENVIRONMENT DIVISION
Syntax:
ENVIRONMENT DIVISION.
[CONFIGURATION SECTION.
SOURCE-COMPUTER. {computer name}
OBJECT-COMPUTER {computer name}
SPECIAL NAMES {computer name}
INPUT-OUTPUT SECTION.
FILE-CONTROL.
SELECT {filenamne-1}
ASSIGN TO {implementor-name-1}….]
ENVIRONMENT DIVISION is the second division of COBOL program. It describes the
“computing environment” of the program. The “computing environment” refers to the type of
computer hardware on which the program is written and run. It is the only machine-dependent
division of a COBOL program. It is the only machine-dependent division of a COBOL program. It
supplies information about the computer equipment to be used in the program. That is, the
entries in this division will be dependent in [1] the computer system and [2] the specific devices
or hardware used in the program. This division also briefly describes the data files required by the
program.
The ENVIRONMENT DIVISION is the only division of a COBOL program that will change
significantly if the program is to be run on a different computer. Since computers have various
models and equipment, each computer center will require division. The CONFIGURATION SECTION
and the INPUT-OUTPUT SECTION, which are again divided into paragraphs.
1. CONFIGURATION SECTION
Syntax
[CONFIGURATION SECTION.
SOURCE-COMPUTER. {computer-name}
OBJECT-COMPUTER. {computer-name}
SPECIAL-NAMES. {computer-name}
It supplies the information about the computer on which the COBOL program will be
compiled and executed. (It is optional in COBOL-85) Configuration section provides
documentation information which includes the computer manufacturer, computer number and
computer model number. The two sections are coded in column 8. Configuration section has
three paragraphs:
a. SOURCE-COMPUTER – The computer that will be used for compiling the program.
b. OBJECT-COMPUTER – The computer that will be used for executing or running the program.
c. SPECIAL-NAMES – A third paragraph that is optional in the configuration section. This
focuses on special devices used for reading or displaying data.
Each paragraph name is directly followed by a period and then a space. The designated
computer IS ALSO FOLLOWED BY A PERIOD. In the example, the source and object computers are
the same. In general, this will be the case, since compilation and execution are usually
performed on the same computer. If, however, the program will be compiled on one model
computer and executed, at some future time, on another model computer, these entries will
differ. This is also optional in COBOL program.
2. INPUT-OUTPUT SECTION
Syntax:
FILE-CONTROL.
SELECT {filename}
ASSIGN TO {implementor-name-1}
The INPUT-OUTPUT SECTION supplies the information about the specific devices used in
the program namely: terminals, printers, and disk drives. It is also optional but is being used most
of the time since programs use files. It supplies information concerning the input and output
devices used in the program. This is started by the FILE-CONTROL paragraph. The FILE-CONTROL
paragraph consists of SELECT statements. A SELECT statement defines a filename and assigns a
device to that file (A file is the major collection of data for a given application). Typically, we
have an input file and an output file. Input file is a collection of data which serves as a reference
for processing of data and the output file is a second collection of data which serves as an output
report. The implementor-name is not a COBOL reserved word nor user-defined word. It is
machine-dependent device specification provided by the computer center.
TIP:
A filename must correspond to the rules of forming user-defined names stated above (i.e.
maximum of 30 characters, letters, digits, and hyphen only, no embedded blanks, at least
one alphabetic character and not a reserved word).
It is more logical to define the input file first before the output file.
Write SELECT statement on two lines for better readability. The best format is as follows:
SELECT filename
ASSIGN TO device
A filename must be specific and meaningful. Avoid acronyms, person’s name and aliases.
Typically, a filename must be related to the subject of the program. If the program is
regarding sales, it can be regarded as SALES-INPUT on input file and SALES-OUTPUT on
output file.
Example:
It must be noted that PRINTER is commonly used in an output file and that there must be a
connected printed to it.
C. DATA DIVISION
Syntax
DATA DIVISION
FILE SECTION
FD filename
[LABEL RECORDS IS STANDARD]
[LABEL RECORDS ARE OMMITED]
[RECORD CONTAINS _______ CHARACTERS]
[DATA RECORD IS _________________]
01 record name
WORKING-STORAGE SECTION
{record description entry
variables description entry
77-level description entry
DATA DIVISION is the one concerned with the identification and description of storage fields and
data used by program. It describes the input and output files to specific devices in the INPUT-
OUTPUT SECTION of CONFIGURATION SECTION. It defines and describes fields, records, and files in
storage. Whatever variables and constant used in PROCEDURE DIVISION statements are declared
here. It consists of two sections: the FILE SECTION and the WORKING-STORAGE SECTION.
A. FILE SECTION – Defines all input and output files including the records needed.
Syntax:
DATA DIVISION.
FILE SECTION.
FD filename
[LABEL RECORD IS STANDARD]
1 RECORD NAME.
B. LABEL RECORD or LABEL RECORDS clause – These clauses are optional for COBOL 85.
This used to indicate if a header and a trailer record are to be created by system.
Both records provide file identification information about the file. The first record will
bear the header label while the last record will be a trailer label. These are created
on output files and checked on input files. If LABEL RECORDS is used, these records
are not created. This is typically used on files that are directed to a printer or screen
which do not require the header and trailer records.
C. RECORDS CONTAINS clause – clause that indicates the number of characters in record.
Data entered on a terminal is typically specified with the clause RECORD CONTAINS 80
CHARACTERS, although the number of character will depend on how many characters
can be stored on one line of the specific terminal.
For disk or tape files, the RECORD CONTAINS clause varies. One of the
advantages of storing data on the media is that records can be of any size.
D. BLOCK CONTAINS clause – indicates the blocking factor for disk or tape.
We have earlier defined record as a collection of related data items within a file.
After a file is declared in FD clause, the record description entries follow. Record
description specifies the format of a record. There are various level numbers in a record.
Record name takes the value of 01. this is considered as the highest level of data in a file.
Since record is divided in fields, all other subordinate field is coded with any level number
between 02 and 49. level 01 is coded in Area A and all other levels are coded in Area B.
SALE-NAME becomes a group data field and its subordinate an elementary field. Note that
declaration of data field length and type is coded on elementary field.
Although it is legal to choose any level numbers to describe the data fields that are at the
same level. The following example illustrate this point.
Fig.6 Sample of Record Entry with Invalid Elementary and Group Level
B. WORKING-STORAGE SECTION – this is where variables and constant which not part of input
but nonetheless required for processing are declared. These include counters, end-of-file
indicators, and work areas.
Syntax:
DATA DIVISION.
WORKING-STORAGE SECTION
01 [variable entry]
01 [variable entry]
01 [variable entry]
05 [variable entry]
05 [variable entry]
The WORKING-STORAGE SECTION immediately follows the FILE SECTION in the program.
This section provides for the storage of data items that are not part of any section, such as
intermediate calculations, report headings for printing and numeric constants for use in
calculations. The procedure for declaring an elementary and group item is still the same
however, in older version of COBOL, it uses a special level number 77 to describe all
elementary independent items. The practice is not common now since using level 77 and level
01 is just the same. It is also suggested to put a prefix of WS for every variable entry to
distinguish it from those in FILE SECTION.
RULES FOR USING WORKING-STORAGE SECTION.
The WORKING-STORAGE SECTION must succeed the FILE SECTION. It must not
be encoded before the FILE SECTION.
WORKING-STORAGE SECTION is coded on Area A and ended with a period.
Group item that will be submitted into other subordinate levels must be
defined PIC clause must appear in the elementary item.
Elementary item must contain a PIC clause. It may contain an initial value using
VALUE clause. VALUE clause may precede or succeed PIC clause.
VALUE clause is used only in the WORKING-STORAGE SECTION and not in the
FILE SECTION.
All items must follow the rules for forming data-names.
a. For storing intermediate results, counters and end-of-file indicators through the use of variable
data field.
There are item entries and clauses which define the type of data that will be used in
variable declaration.
After declaring the level numbers, the term PIC follows. PIC is short for PICTURE clause.
Each elementary data field must be defined by a PIC clause that provides information as to
its(numeric, alphabetic, or alphanumeric.) and its size. A specific picture character A gives a
specific type of data. It is also used to declare the size or length of the field (i.e. the number
enclosed in parentheses). Size or length of the fields pertains to a maximum number of characters
that will be accepted by the program. The PIC clause is written at the end of the data field
(leaving at lest one blank space after the data field name with the word PIC followed by at least
one blank space and the appropriate picture character (X or 9 or A). if a data field requires more
that one picture character, the number of characters to which the picture characters applies be
enclosed in parentheses following the picture character.
In fig. 10 AMOUNT is of numeric type with a maximum digit of 7. The numeric 9 indicates
that a storage position should only contain any of the numeric digits from 0 to 9. If the input data
is of numeric type, it cannot contain anything else other than numbers, not even a decimal point.
Leading spaces may however be allowed. Thus, a data value with decimal point as in 1200.50
cannot be directly stored in a numeric data field. The following examples illustrate how the letter
V works in PIC clauses.
Again, note that a numeric field can contain only the digits 0-9. Blanks are not numeric
characters. When a value does not fill a numeric 9 field completely, the value is justified to the
right, and the extra positions are filled with zeros. When entering data, you should be careful to
zero-fill a field with leading zeros, otherwise you may be in for some surprising results. Thus, in a
field of six positions the numeric value 143 should be entered as follows:
000143
The V character indicated the position of an assumed decimal point. This means that the
decimal point is not written as part of the field and therefore is not included as part of the field
size. This does not occupy a storage position. Instead, the information about decimal-point
location is stored elsewhere in the computer, so that any arithmetic computations can be done
correctly.
The X PICTURE character denotes that alphanumeric positions are contained in a field. Fig.
9 declares AREA-CODE as of alphanumeric type and of size 1, SALENO is alphanumeric and size 10,
SALE-NAME is alphanumeric and can contain a maximum of 25 characters. This means that it can
include alphabetic characters, numeric characters and special symbols. The PICTURE X is most
likely to use in variable NAME because there are names which contain special symbol as
“SHAQUILLE O’NEAL”. When the characters do not fill an X field completely they are left justified,
with blanks filling the remaining positions in the right.
The A PICTURE character is similar to the X character, except that it indicates only
alphabetic characters and blanks.
The S character is used to designate a numeric field that is signed (i.e., one that can be
negative in value.) In COBOL, all fields are considered positive unless the S has negative value.
Only one S character may be used in a field. It is coded in the leftmost character. The S is not
counted in the size of the filed, and therefore S999 is a field of three positions. In the following
examples, the negative sign in machine representation is as shown “-“ on top of the rightmost
digit, in order to preserve the concept that it does not take up an extra position.
FILLER is a COBOL reserved word used to define areas within a record that will not be
referenced individually during processing. This is a generic data name used extensively in the
DATA DIVISION with respect to data items that are not referenced specifically in the PROCEDURE
DIVISION.
Fig. 7 Example of Data Division using FILLER command.
This process of assigning an initial value to a data field is known as initialization and is
performed by using a VALUE clause. A VALUE clause consists of the word VALUE followed by the
initial value, and is placed immediately after the PICTURE clause. The initial value can be a
numeric literal, a nonnumeric literal, or a figurative constant. Rules for this paragraph Literal and
figurative constants were discussed earlier.
The purpose of editing is to increase readability of the report. Editing is associated with printing
data on the printer. All editing is accomplished by moving an elementary item to a report item
which contains appropriate edit symbols. The editing function involves a change in the form of
data. For example, we may suppress leading zeros, we may use commas to make long numeric
values more legible, we may insert a dollar sign in front of a value, and so forth. This module will
not tackle the $, +, -, DB, CR, * picture characters since these are not typically used.
Decimal (.) PICTURE character indicates the position of the decimal point and serves to
align the actual decimal values in the field. Only one decimal point must appear in a field while a
field may contain more than one comma if the size of the field conforms to it. Each of these
insertion characters is used to indicate the position of the indicated character in the storage
location. A field cannot contain both V and . PICTURE characters.
The B PICTURE character is used to insert blank in the data entry. This is used to increase
readability of the data entry. For example, 05 SALENAME PIC X (!0) BX (14) BX. The first 10
characters will be written first then a blank is inserted another 14 characters before another blank
is inserted.
The Zero insertion character is used to insert zeros in the designated position. This is best
used when we want to show the full value of digits. Let’s say, in thousands form. For example 05
WS-POPULATION PIC 9,999,000 will result in 2,500.00. Assuming it has value of 2500.
The / PICTURE character is used to insert a stroke or forward slash in the designated
position. This is best used is editing date of birth. For example,
Further processing as MOVE DATEOFBIRTH TO DOB will cause DOB to contain 01/15/91.
9 Numeric field
A Alphabetic field
X Alphanumeric field
P Decimal scaling
Z Zero suppression
* Check protection
. Decimal point
, Comma
+ Plus sign
- Minus sign
DB Debit sign
CR Credit sign
B Blank insertion
0 Zero insertion
/ Stroke insertion
D. Procedure Division
OPEN Statement
The OPEN statement used to link the actual physical file to a program file and prepare the
file to be processed. The file must be opened before data can be read from or written to a file.
The OPEN statement has the following format:
INPUT
I-O
EXTEND
The filename must be exactly the same as it is in the SELECT statement of the
ENVIRONMENT DIVISION and the FILE SECTION of the DATA DIVISION.
1. INPUT
2. OUTPUT
3. I-O
4. EXTEND
When the program needs to read data from a file, the file must be opened for INPUT. A file is
opened for OUTPUT when the program will write data to the file. Only direct access files can be
opened for I-O and therefore the file should be stored in a direct access storage medium such as
magnetic disks. A file is opened in EXTEND mode when additional data are to be added to the end
of the file. To use a file in EXTEND mode, the file must already exist.
Examples:
OPEN INPUT STUDENT-FILE.
OPEN OUTPUT REPORT-FILE.
OPEN I-O EMPLOYEE-FILE.
OPEN EXTEND SALES-FILE.
In the first statement the file STUDENT-FILE must be opened for INPUT function only and
the second statement, REPORT-FILE is opened for OUTPUT function only. The third statement
opened EMPLOYEE-FILE as I-O, which means it can be used as an INPUT and at the same time as
OUTPUT. In the fourth statement, SALES-FILE was opened using the EXTEND option which means
that any new record written to it will be appended to the end of the file.
A single OPEN statement may be used to open more than one file.
Each filename must be preceded by the keyword INPUT or OUTPUT. A period is placed at the end
of the last file name only.
Example:
A single OPEN statement can also be used to open several input and output files.
Example:
READ Statement
The READ statement reads data from the input file. The data are read one record at a
time. When processing a sequential input file, the READ statement reads records in sequence.
The first READ statement reads the first record, the next record reads the second record, and so
on. The READ statement has the following format.
The READ statement refers to the name of the input file defined in the INPUT-OUTPUT
SECTION of the ENVIRONMENT DIVISION.
The AT END clause of the READ statement tests whether the end of the file has been
reached. If the READ statement provided with the AT END clause execution time or a run time
error will result.
Example
MOVE Statement
The MOVE statement moves a literal or the contents of a data field to another data
field. The data field receiving the value is called the receiving data field. The data field from
which the data are copied is the sending data field. There are two types of the MOVE
statements: the direct MOVE in which a value is directly moved to a data field, and the indirect
MOVE, in which a value contained in data field is moved to another data field. The general format
of the direct MOVE statement is
Examples:
MOVE 1 TO EOF-SW.
MOVE “Y” TO OPTION.
MOVE 20 TO COUNTER.
In the first example, the value 1 was moved to the data field EOF-SW. The second
example moved the character “Y” to the data field OPTION which was defined in the DATA
DIVISION having an alphabetic or an alphanumeric picture. The third example moved the value 20
to the data field COUNTER which was previously defined in the DATA DIVISION having a numeric
picture.
When moving a data value, it is important to ensure that the receiving data field is
correctly defined for that value. If a numeric value is moved, the data field should be defined as
numeric with an adequate number of digits. If an alphanumeric value is moved, the data field
should be defined as alphanumeric with an adequate number of characters.
In the previous examples, the data fields to which data values are moved should be
defined in this way:
05 EOF-SW PIC 9.
05 OPTION PIC X.
05 COUNTER PIC 99.
It also possible to use figurative constants with the MOVE statement. Figurative constants
have predefined meanings to the COBOL compiler. The following table gives the meanings of
some commonly used figurative constants when used in the MOVE statements.
The movement of data to the receiving data field depends on the type of data being
moved. If alphanumeric or alphabetic data are being moved, the movement starts from the
leftmost character. If numeric data are being move, the movement starts from the rightmost
digit. If the receiving data field is longer than the value being moved, the additional positions are
padded with blanks on the right if the data field is defined as alphanumeric, and with zeroes on
the left if the data field is defined as numeric. Truncation or cutting off of the value occurs from
the right is the data field is defined as alphanumeric, and from the left of the value if the data
field is defined as integers. The following examples show the effect of the MOVE statements on
longer and shorter receiving data fields:
In the first example, four remaining positions on the right of the receiving data field are
padded with blanks. In the second example, truncation of the data value occurs from the right.
In the third example, a data field is padded with a zero on the left. In the fourth and fifth
examples, Truncation of data values occur from the left.
Either the VALUE clause or the MOVE statement may be used to assign an initial value to a
data field. The VALUE clause assigns the value during compilation, whereas the MOVE statement
assigns it during program execution. In general, if the value of data field is not going to change, it
is better to use the VALUE clause because it uses less execution time. However, if the value
needs to be continually reinitialized, the MOVE statement should be used.
PATRICK
EMPNAME EMPNAME-OUT
PATRICK PATRICK
EMPNAME EMPNAME-OUT
The PIC clauses of both the sending data field and the receiving data field play an important role
in what happens after the MOVE statement is executed. If the PIC clauses of both data fields are
exactly identical and exact copy of the data value is moved from the sending data field to the
receiving data field. If the PIC clause of the receiving data field is longer or shorter than that of
the sending data field, the extra spaces are padded, or truncation occurs, in the same way as
mentioned for direct moves.
1. Non numeric literals or data values may not be moved to alphabetic data fields.
2. Data may move from a non numeric field to a numeric field only if the non numeric field
contains a valid integer (It cannot be a real number). However, the practice is
discouraged because of unpredictable results. If the field does not contain valid integer
data, an error will occur.
3. Non numeric literals or data values may not be moved to numeric data fields.
4. When a receiving field is non numeric, the value moved into it is left-justified in the field.
This means that the first character is placed in the first position on the left side of the
data field. If the field is larger than the data value, blank spaces will be placed on the
right side to fill out the field. If the size of the field is smaller than the data value, the
value will be truncated to fit in the field. Any extra characters on the right side will be
cut off.
5. When the receiving field is numeric, the data will be aligned at the decimal point. If there
are fewer digits than the size of the field, zeroes will be added to fill out the field. If the
numeric data are longer than the field, the number will be truncated to fit into the field.
WRITE Statements
The function of the WRITE statement is to write a record to an output file. WRITE
statement has the following format.
WRITE output-record-name
The data field output-record-name must be the same as that declared in the FILE SECTION
of the DATA DIVISION. The WRITE statement causes all data fields that are part of the record to
be written, one after another, to the output file.
Before writing an output record, make sure that a data value has been moved to each data
field that is part of the output record.
The WRITE statement is always followed by the name of the output record, not the output
file. Numeric edited (rather than numeric) fields should be used to output numeric data that are
destined for a printer.
Example:
WRITE EMPLOYEE-REC.
CLOSE Statement
The CLOSE statement closes a file so that it is no longer available for processing. All files
that are opened at the beginning of a program should be closed before ending the program. The
format of the CLOSE statement is
More than one file may be closed with one CLOSE statement. If more than one file is
closed with on CLOSE statement, the period appears at the end of the last file name. Do not use
the keyword INPUT or OUTPUT before the file name in a CLOSE statement.
Example
CLOSE STUDENT-FILE
CLOSE EMPLOYEE-FILE TAXTABLE-FILE REPORT-FILE
The STOP RUN statement terminates execution of the program. As soon as this statement
is encountered, the program stops execution. The format of the STOP RUN statement is
STOP RUN.
The STOP RUN statement does not have to the last physical statement in the program, but
it must be the last statement that is executed by the program.
ARITHMETIC STATEMENTS
ADD Statement
The ADD statement is used to add the contents of two or more numeric data fields. The
format of the ADD statement is
This statement performs two functions: it adds the contents of datafield-1 and datafield-2
and stores the result in datafield-2. With an ADD statement, the result is stored in the data field
listed last. On execution of the ADD statement, the contents of the data field that receives the
result of the calculation are changed. However, the contents of the other data field remain
unchanged.
Examples:
SALARY TOTAL-SALARY
PIC 9(5) PIC 9(7)
Before Execution
50000 0
After Execution
50000 50000
PRODUCTION TOTAL_PRODUCTION
PIC 9(4) PIC 9 (6)
Before Execution
200 500
After Execution
200 700
When using the ADD statement, care must be taken to ensure that the data field that
receives the result of the calculation is large enough to hold the result. Otherwise, overflow
occurs and the result stored will be incorrect.
More than two data fields can be used in the ADD statement.
Example:
In this example, the content of the data TOTAL-QUIZ has been changed, but the contents
of the other three data fields remain unchanged.
A numeric literal can be used in the ADD statement. However, the result of the calculation may
not be stored in a numeric literal. The result of the calculation must always be in a data field in
order to be valid.
Example:
ADD 1 TO RECORD-COUNTER.
ADD 100 TO SALARY.
1. The contents of the first data field are added to the contents of the last data field.
2. The result is always stored in the last data field. All other data fields remain unchanged.
3. Any number of data fields may be listed in an ADD statement. They all will be added to
the last data field.
4. The last data field must be large enough to hold the sum.
5. Numeric literals may be used in add statements. However, the sum must be stored in a
data field. The last item in an ADD statement must be a data field.
6. All data fields that take part in a calculation must be numeric with PIC 9s (may have an S
or V). The data field that holds the result must be either numeric or numeric edited. If it
is numeric, the data field can be used in other additional arithmetic statements.
However, if it is numeric edited, the data field may not be used in other arithmetic
statements. This rule applies not only to the ADD statement, but also to all other
arithmetic statements.
SUBTRACT Statements
The SUBTRACT statement subtracts the contents of two or more data fields. It has the
following format
The SUBTRACT statement subtracts the contents of datafield-1 from the contents of
datafield-2 and stores the result of the calculation in datafield-2. The contents of datafield-1
remain unchanged after the statement is executed.
Examples:
Before Execution
250 1000
After Execution
250 750
Before Execution
1000 25000
After Execution
1000 24000
It is possible to subtract the contents of several data fields from that of another data field.
Example:
The contents of WITH-TAX and MEDICARE remain unchanged, the contents of GROSS-
INCOME was changed.
Example:
1. The contents of the first data field are subtracted from the contents of the last data field.
2. The result is always stored in the last data field. All other data fields remain unchanged.
3. Any number of data fields may be subtracted from the last data field.
4. As with ADD statements, numeric literals may be used in SUBTRACT statements. However,
the result must be stored in a data field. The last item in a SUBTRACT statement must be
a data field.
MULTIPLY Statement
This statement multiplies the contents of datafield-1 and datafield-2 and stores the result
in datafield-2. The contents of datafield-1 are not changed after the statement is executed; the
contents of datafield-2 are changed.
Examples:
MULTIPLY RATE BY PRICE.
RATE PRICE
PIC 9(3) PIC 9(5)
Before Execution
10 150
After Execution
10 1500
The result in product is stored in PRICE.
DIVIDE Statement
The DIVIDE statement divides the contents of one data field by the contents of another
data field. There are two methods of writing the DIVIDE statement.
The format of the first type is
The dividend is the number being divided and the divisor is the datafield-2. In a DIVIDE
statement, the result of the division is saved in the dividend data field. Thus, the above DIVIDE
statement saves the result in datafield-1.
In this statement, the dividend is datafield-2 and the divisor is datafield-1. The result of
division is stored in datafield-2.
Example:
DIVIDE 10 INTO AVE.
DIVIDE AVE BY 10.
DIVIDE SALES BY RATE.
DIVIDE RATES INTO SALES.
1. There are two formats for the DIVIDE statement. DIVIDE BY and DIVIDE INTO.
2. When the DIVIDE BY statement is used, the first data field is the dividend and the second is
the divisor.
3. When the DIVIDE INTO statement is used, the first data field is the divisor and the second
is the dividend.
4. Regardless of which format is used, the quotient is always placed in the dividend field.
5. The divisor may be a numeric literal, but he dividend must always be a numeric data field.
CLAUSES
The GIVING clause
All four arithmetic statements (ADD, SUBTRACT, MULTIPLY and SUBTRACT) can be written
in another form. This form uses the GIVING clause. When the GIVING clause is used in any of
these statements, the result of the calculation is stored in a data field specified by the GIVING
clause. This data field can be either one of the data fields that are part of the calculation or a
new data field. The GIVING clause is added toward the end of these statements. Since the
GIVING clause can be used and not interfere with the contents of the data fields that take part in
an arithmetic operation.
In this format, the contents of datafield-1 and datafield-2 are added and the result
is stored in a different data field called datafield-x.
Example:
In this example, the contents of QUIZ1 and QUIZ2 values did not changed, only the
content of the data field TOTAL-QUIZ was changed.
Examples:
The MULTIPLY statement with the GIVING option has the following format:
Examples:
Examples:
Problems with insufficient size of the data field to hold the digits to the right of
the decimal points can be handled by using the ROUNDED clause. This clause rounds the
digits on the right of the decimal point to the number of decimal places that the receiving
data field can hold. Truncation of data is more common with multiplication and division
operations than with addition and subtraction.
The contents of datafield-1 are divided by the contents of datafield-2, the integer
part of the division operations is stored in datafield-3, and the remainder is stored in
datafield-4.
When a data field is not large enough to hold the digits to the left of the decimal
point or digits without any decimal point, the ONSIZE ERROR clause is used. If this clause
is not used in a situation where an insufficient size problem occurs, digits are truncated
from the left, which greatly affects the value stored. The use of the ON SIZE ERROR
option provides a warning signal and does not store the truncated value in the receiving
data field.
The format of the ON SIZE ERROR is
Example:
COMPUTE statement
The word COMPUTE is followed by a blank space, a data field where the result of
the calculation is to be stored, another blank space, an equal sign (=), another blank
space, and then the expression. All arithmetic signs, such as the equal sign, plus sign, and
minus sign, must be preceded and followed by at least one blank space. The COMPUTE
statement makes it possible to perform several arithmetic operations in a single
statement.
Examples:
Course Materials:
https://www.tutorialbrain.com/mainframe/cobol_divisions/
https://devops.com/the-beauty-of-the-cobol-programming-language-v
https://www.infoworld.com/article/3539057/what-is-cobol-cobol-programming-explained.html
http://www.3480-3590-data-conversion.com/article-reading-cobol-layouts-1.html