Module 3 - Divisions Reviewer
Module 3 - Divisions Reviewer
Module 3 - Divisions Reviewer
II. FOUR DIVISIONS OF COBOL - (KAY SIR DEFINITION -_-) • Thus, after the DATE-WRITTEN we could have written
1. IDENTIFICATION DIVISION ANYTIME IN JUNE. The compiler derives no more
Syntax: meaning from it, therefore, the programmer should be
IDENTIFICATION DIVISION. concerned simply with choosing verbal descriptions that
PROGRAM-ID. program name will be meaningful to the potential readers of the program.
[ AUTHOR. {comment-entry}….. ] • The DATE-COMPILED paragraph may be left blank.
[ INSTALLATION. {comment-entry…..} ] • The compiler will insert the actual date and the source
[ DATE-WRITTEN. {comment-entry…} ] listing will include that date.
DATE-COMPILED.
[ SECURITY. {comment-entry….} ] 2. ENVIRONMENT DIVISION
REMARKS. 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}
- FUNCTION: is to supply information about the program to ASSIGN TO {implementor-name-1}….]
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.
- PURPOSE: 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.
- start in column-8 of the COBOL coding sheet. - It describes the “COMPUTING ENVIRONMENT” of the
- The only required paragraph in this division is the PROGRAM- program.
ID paragraph while the rest is optional. • The “computing environment” refers to the
type of computer hardware on which the
TIPS: program is written and run.
❖ A clause with a combination of two words is always - It is the only MACHINE-DEPENDENT DIVISION of a COBOL
connected with hyphen. program.
Examples: - It supplies information about the computer equipment to
− PROGRAM-ID, be used in the program.
− INPUT-OUTPUT SECTION, - That is, the entries in this division will be dependent in
− FILE-CONTROL [1] the Computer System and
❖ Take note also of how the statement is separated by period. [2] the Specific Devices or Hardware used in the program.
The following example includes optional paragraphs. - 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
Fig 1. Example of IDENTIFICATION DIVISION SECTION, which are again divided into paragraphs.
Explanation: SECTION of the Environment Division
• All paragraph-names start in column 8 and, as indicated a. Configuration Section
above, are optional with exception of the PROGRAM-ID. b. Input-Output Section
• The compiler does not process what follows the COBOL
words but ONLY PRINTS THE CONTENT.
CONTINUATION OF MODULE 3 COMPROG
TIPS:
❖ 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
Fig 2. Example of CONFIGURATION SECTION blanks, at least one alphabetic character and not a
reserved word).
Explanation: ❖ It is more logical to define the Input File First before the
• Each paragraph name is directly followed by a period output file.
and then a space. ❖ Write SELECT statement on two lines for better
• The designated computer IS ALSO FOLLOWED BY A readability. The best format is as follows:
PERIOD. o SELECT filename
o ASSIGN TO device
• In the example, the SOURCE and OBJECT
COMPUTERS are the same. ❖ A filename MUST be specific and meaningful. Avoid
acronyms, person’s name and aliases.
• In general, this will be the case, since compilation and
❖ Typically, a filename MUST be related to the subject of
execution are usually performed on the same computer.
the program. If the program is regarding sales, it can be
If, however, the program will be compiled on one model
regarded as SALES-INPUT on input file and SALES-
computer and executed, at some future time, on another
OUTPUT on output file.
model computer, these entries will differ.
❖ Assigned filename must be enclosed in double quotes.
• This is also optional in COBOL program.
Ex. SELECT WORKREC ASSIGN TO “WORKREC.DAT”
CONTINUATION OF MODULE 3 COMPROG
Syntax:
DATA DIVISION.
FILE SECTION.
Fig 3. Example of INPUT-OUTPUT SECTION FD filename
[LABEL RECORD IS STANDARD]
Explanation: [LABEL RECORDS ARE OMMITED]
• The implementor-name varies depending on the [RECORD CONTAINS ______ CHARACTERS]
computer used. [DATA RECORD IS _________________]
• It can rather be cryptic as: SELECT CUSTFILE ASSIGN 01 RECORD NAME.
TO SYS005-UR-2540R-S or can be easily as: SELECT
GRADEFILE ASSIGN TO “MYFILE.DAT”.
• SELECT {filename} ASSIGN TO
{DISK Drive Letter:\sub-directory\filename
PRINTER}
• The drive Letter\sub-directory is the path where
filename is located.
a.1. FILE DESCRIPTION ENTRIES
Example: a. FD Clause
SELECT SALES-INPUT ASSIGN TO B:\DATA\SALES- - is an abbreviation for File Description and is
INPUT.DAT. coded in Area A.
- This paragraph is followed by file name that
• It must be noted that PRINTER is commonly used in an must correspond exactly, character for character,
output file and that there must be a connected printed to
to a file that is already mentioned in the
it.
Environment Division.
3. DATA DIVISION
Syntax: b. LABEL RECORD or LABEL RECORDS Clause
DATA DIVISION. - These clauses are OPTIONAL for COBOL 85.
FILE SECTION. - This used to indicate if a header and a trailer
FD filename record are to be created by system. Both
[LABEL RECORDS IS STANDARD] records provide file identification information
[LABEL RECORDS ARE OMMITED] about the file.
[RECORD CONTAINS _______ CHARACTERS] - The first record will bear the header label
[DATA RECORD IS _________________] while the last record will be a trailer label.
01 record name - These are created on output files and
WORKING-STORAGE SECTION. checked on input files. If Label Records is
{record description entry variables description Used, these records are not created.
entry 77-level description entry} - This is typically used on files that are directed
to a printer or screen which do not require the
- Data Division is the one concerned with the identification header and trailer records.
and description of storage fields and data used by program.
- It describes the input and output files to specific devices c. RECORDS CONTAINS Clause
in the Input-Output Section and Configuration Section. - clause that indicates the number of
- It defines and describes fields, records, and files in storage. characters in record.
Note: Whatever variables and constant used in PROCEDURE - Data entered on a terminal is typically specified
DIVISION STATEMENTS ARE DECLARED HERE. with the clause RECORD CONTAINS 80
- It consists of two sections: the FILE SECTION and the CHARACTERS, although the number of
WORKING-STORAGE SECTION. characters will depend on how many characters
can be stored on one line of the specific terminal.
MAIN SECTION of the Data Division ▪ For disk or tape files, the RECORD
a. File Section CONTAINS clause varies.
b. Working-Storage Section ▪ One of the advantages of storing data
on the media is that records can be of
any size.
CONTINUATION OF MODULE 3 COMPROG
Fig 4. Example of FILE SECTION of DATA DIVISION These includes the ff:
✓ counters,
a.2. RECORD DESCRIPTION ENTRIES ✓ end-of-file indicators, and
- We have earlier defined record as a collection of ✓ work areas.
related data items within a file.
- After a file is declared in FD clause, the record Syntax:
description entries follow. Record description DATA DIVISION.
specifies the format of a record. WORKING-STORAGE SECTION.
- There are various level numbers in a record. Record 01 [variable entry]
name takes the value of 01. This is considered as the 01 [variable entry]
highest level of data in a file. 01 [variable entry]
- Since record is divided in fields, all other subordinate 05 [variable entry]
field is coded with any level number between 02 and 05 [variable entry]
49. level 01 is coded in Area A and all other levels are
coded in Area B.
- This section provides for the storage of data items that 4.1 USING STATEMENTS
are not part of any section, such as a. OPEN Statement
✓ intermediate calculations, - The OPEN statement used to link the actual
✓ report headings for printing and physical file to a program file and prepare the file to
✓ numeric constants for use in calculations. be processed.
- The procedure for declaring an elementary and group - The file MUST be opened before data can be read
item is still the same however, in older version of COBOL, from or written to a file.
it uses a special level number 77 to describe all elementary
independent items.
The OPEN statement has the following format:
- 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.
Examples:
OPEN INPUT STUDENT-FILE.
OPEN OUTPUT REPORT-FILE.
OPEN I-O EMPLOYEE-FILE.
OPEN EXTEND SALES-FILE.
Explanation:
- The procedure division is the main section of the COBOL • In the first statement the file STUDENT-FILE
program. Like the main() function of C/C++ program. must be opened for INPUT function only and
- From the procedure, division section program will actually • the second statement, REPORT-FILE is
start the execution. opened for OUTPUT function only.
CONTINUATION OF MODULE 3 COMPROG
Explanation:
• In the first example, the value 1 was moved to
the data field EOF-SW.
- The READ statement refers to the name of the • The second example moved the character “Y”
input file defined in the Input-Output Section of the to the data field OPTION which was defined in the
Environment Division. DATA DIVISION having an alphabetic or an
- The AT END clause of the READ statement tests alphanumeric picture.
whether the end of the file has been reached. • The third example moved the value 20 to the
data field COUNTER which was previously
- If the READ statement provided with the AT END
defined in the DATA DIVISION having a numeric
clause execution time or a run time ERROR will
picture.
result.
• When moving a data value, it is important to
ensure that the receiving data field is correctly
Example: defined for that value.
READ STUDFILE
AT END MOVE 1 TO EOFSW.
CONTINUATION OF MODULE 3 COMPROG
Explanation:
• In the first example, four remaining positions on
- It also possible to use figurative constants with the the right of the receiving data field are padded
MOVE statement. with blanks.
- Figurative constants have predefined meanings • In the second example, truncation of the data
to the COBOL compiler. value occurs from the right.
• In the third example, a data field is padded with
The following table gives the meanings of some a zero on the left.
commonly used figurative constants when used in the • In the fourth and fifth examples, Truncation of
MOVE statements. 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
Example:
If a record named REMARKS is defined as: change, it is better to use the VALUE clause
05 REMARKS PIC X (10). because it uses less execution time. However, if
the value needs to be continually reinitialized, the
Its value can be initialized to 10 blank MOVE statement should be used.
spaces using this MOVE statement
MOVE SPACES TO REMARKS. 2. General format of the
INDIRECT MOVE statement:
Explanation:
• The movement of data to the receiving data MOVE data-field-1 TO data-field-2.
field depends on the type of data being
moved. Examples:
▪ If alphanumeric or alphabetic data MOVE EMPNAME TO EMPNAME-OUT.
are being moved, the movement MOVE SALARY TO SALARY-OUT.
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 Explanation:
occurs from the right is the data field is • The PIC clauses of both the sending data field
defined as alphanumeric, and from the left and the receiving data field play an important
of the value if the data field is defined as role in what happens after the MOVE statement
integers. is executed.
CONTINUATION OF MODULE 3 COMPROG
• If the PIC clauses of both data fields are - The data field output-record-name MUST be the
exactly identical and exact copy of the data SAME as that declared in the FILE SECTION of the
value is moved from the sending data field to the DATA DIVISION.
receiving data field. - The WRITE statement causes all data fields that are
• If the PIC clause of the receiving data field is part of the record to be written, one after another,
longer or shorter than that of the sending data to the output file.
field, the extra spaces are padded, or - BEFORE WRITING an output record, make sure that
truncation occurs, in the same way as mentioned
a data value has been moved to each data field that
for direct moves
is part of the output record.
RULES FOR THE MOVE STATEMENT - The WRITE statement is ALWAYS FOLLOWED by
▪ Non Numeric Literals or data values may NOT BE the NAME of the OUTPUT RECORD, not the output
MOVED to alphabetic data fields. file. Numeric edited (rather than numeric) fields
▪ Data MAY MOVE from a non numeric field to a should be used to output numeric data that are
numeric field only if the non numeric field contains destined for a printer.
a valid integer (It cannot be a real number).
o However, the practice is discouraged Examples:
because of unpredictable results. If the field WRITE EMPLOYEE-REC.
does not contain valid integer data, an
ERROR WILL OCCUR. e. CLOSE Statement
▪ Non Numeric Literals or data values may NOT BE - The CLOSE statement closes a file so that it is no
MOVED to numeric data fields.
longer available for processing. All files that are
▪ When a receiving field is non numeric, the value
opened at the beginning of a program should be
moved into it is left-justified in the field.
o This means that the first character is placed in closed before ending the program.
the first position on the left side of the data field.
o If the field is LARGER than the data value, CLOSE statement has the following format:
blank spaces will be placed on the right side
to fill out the field. CLOSE filename-1, filename-2, … filename-n.
o If the size of the field is SMALLER than the
data value, the value will be truncated to fit in - MORE THAN ONE FILE MAY BE CLOSED with one
the field. Any extra characters on the right CLOSE statement.
side will be cut off. - If more than one file is closed with on CLOSE
▪ When the receiving field is numeric, the data will be statement, the period appears at the end of the last
aligned at the decimal point. If there are fewer digits file name.
than the size of the field, zeroes will be added to fill out
- Do not use the keyword INPUT or OUTPUT before
the field. If the numeric data are longer than the field,
the number will be truncated to fit into the field. the file name in a CLOSE statement.
Examples:
CLOSE STUDENT-FILE.
CLOSE EMPLOYEE-FILE, TAXTABLE-FILE, REPORT-FILE.
Filename No.1 Filename No.2 Filename No. 3
4.1.a. ARITHMETIC STATEMENTS - When using the ADD statement, CARE MUST BE
- ARITHMETIC STATEMENTS perform arithmetic TAKEN to ensure that the data field that receives the
calculations. result of the calculation is large enough to hold the
result.
Basic Arithmetic Statements - Otherwise, overflow occurs and the result stored will
o Add be incorrect.
o Subtract - More than two data fields can be used in the
o Multiply ADD statement.
o Divide
o Compute Example No. 2:
- This statement performs TWO FUNCTIONS: Note: The result of the calculation must always be in
• ADDS the contents of datafield-1 and a data field in order to be valid.
datafield-2
Example No. 3:
• STORES the result in datafield-2.
Example No.1:
Example:
RULES FOR
ORDER OF ARITHMETIC OPERATIONS
- Arithmetic Operations are performed in the
following order:
1. Anything in Parentheses is calculated first.
• If parentheses are nested, they are
evaluated from the INSIDE OUT.
RULES FOR DIVIDE STATEMENT 2. Exponentiation.
▪ There are two formats for the DIVIDE statement. 3. Multiplication / Division.
DIVIDE BY and DIVIDE INTO. 4. Addition / Subtraction.
▪ When the DIVIDE BY statement is used, the first data
Note: Operations of EQUAL PRIORITY are
field is the dividend and the second is the divisor. evaluated LEFT TO RIGHT.
▪ When the DIVIDE INTO statement is used, the first
data field is the divisor and the second is the
dividend.
4.2 DIFFERENT CLAUSES
▪ Regardless of which format is used, the quotient is a. GIVING Clause (used in Add, Subtract, Multiply, Divide)
always placed in the dividend field. - All four arithmetic statements (Add, Subtract,
▪ The divisor may be a numeric literal, but the dividend Multiply and Subtract) can be written in another
must always be a numeric data field. form.
- This form uses the GIVING clause. When the
GIVING clause is used in any of these statements
(Arithmetic Statements), the result of the calculation
is stored in a data field specified by the GIVING
clause.
CONTINUATION OF MODULE 3 COMPROG
- This data field can be either one of the data fields - Both of these DIVIDE statements yield the
that are part of the calculation or a new data field. SAME RESULT.
- The GIVING clause is added toward the end of
these statements. Example:
- Since the GIVING clause CAN BE USED and NOT
INTERFERE with the contents of the data fields that
take part in an arithmetic operation.
b. ROUNDED Clause
i. GIVING Clause for the ADD Statement - Problems with insufficient size of the data field to
Format: 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
- In this format, the contents of datafield- decimal point to the number of decimal places that
1 and datafield-2 are ADDED and the the receiving data field can hold.
- Truncation of data is more common with
result is STORED in a different data field
multiplication and division operations than with
called datafield-x. addition and subtraction.