Module 3 - Divisions Reviewer

Download as pdf or txt
Download as pdf or txt
You are on page 1of 12

CONTINUATION OF MODULE 3 COMPROG

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

a. Configuration Section b. Input-Output Section


Syntax: Syntax:
[ CONFIGURATION SECTION. FILE-CONTROL.
SOURCE-COMPUTER. {computer-name} SELECT {filename}
OBJECT-COMPUTER. {computer-name} ASSIGN TO {implementor-name-1}
SPECIAL-NAMES. {computer-name} ]

- The Input-Output Section supplies the information about


the specific devices used in the program namely:
- It supplies the information about the computer on which • terminals,
the COBOL program will be compiled and executed.
• printers, and
- (It is optional in COBOL-85)
• disk drives.
- Configuration section provides documentation
- It is also OPTIONAL but is being used most of the time since
information which includes the
programs use files.
• computer manufacturer, - It supplies information concerning the input and output
• computer number and devices used in the program.
• computer model number. - This is started by the FILE-CONTROL paragraph. The FILE-
CONTROL paragraph consists of SELECT statements.
- The two sections are coded in column 8. • A SELECT statement defines a filename and
assigns a device to that file
Three Paragraphs:
• (A file is the major collection of data for a given
1. SOURCE-COMPUTER – The computer that will be
application).
used for compiling the program.
- Typically, we have an Input File and an Output File.
2. OBJECT-COMPUTER – The computer that will be
• Input File is a collection of data which serves as
used for executing or running the program.
a reference for processing of data.
3. SPECIAL-NAMES – A third paragraph that is
optional in the configuration section. This focuses • Output File is a second collection of data which
serves as an output report.
on special devices used for reading or displaying
- The implementor-name is not a COBOL reserved word nor
data.
user-defined word. It is machine-dependent device
specification provided by the computer center.

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

a. File Section – Defines all input and output files including


the records needed.

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

d. BLOCK CONTAINS Clause


- indicates the blocking factor for disk or tape.

e. DATA RECORD Clause


- pertains to the name of the designated record.

RULES FOR CODING DESCRPTION ENTRIES


▪ File Description is coded in Area A. Fig 6. Example of Record Entry
with Invalid Elementary and Group Level
▪ All other entries should be coded in Area B
(i.e. filename and LABEL RECORDS)
Explanation:
▪ If Label Records Clause is used, no period is
coded from FD until the last clause has been • Fig. 6 describes INREC as level 01, AREA-CODE,
specified. SALENO and SALE-NAME are all coded in Level 05.
▪ Commas are always optional in a program to • All items that are coded on the same level are not
separate clause. If USED, they must be followed subordinated to or related to one another. These are
by at least on blank. called INDEPENDENT ITEMS.
▪ It is recommended that each clause appear on • Note that level 05 is used and not level 02. We will use
separate line for clarity and ease of debugging. levels 5, 10, 15 and so on in our further discussion. This is
to provide additional levels in case insertion of field is
needed.
• In case the SALE-NAME field is further divided as
LASTNAME, FIRSTNAME and MIDINITIAL, this will be
declared as:
o SALE-NAME becomes a group data field
and its subordinate an elementary field.
o 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.

b. Working-Storage Section – this is where variables and


constant which not part of input but nonetheless required
for processing are DECLARED.

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.

- The Working-Storage Section immediately follows the


File Section in the program.

Fig 5. Example of FILE SECTION of DATA DIVISION


CONTINUATION OF MODULE 3 COMPROG

- 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.

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 - The filename MUST be exactly the same as it is in
contain an initial value using VALUE clause. VALUE the SELECT Statement of the Environment
clause may precede or succeed PIC clause. Division and the File Section of the Data Division.
▪ VALUE clause is used only in the WORKING- - A file may be opened for one of the following
STORAGE SECTION and not in the FILE SECTION. purpose:
▪ All items must follow the rules for forming data-names. 1. Input
2. Output
b.1. USES OF WORKING STORAGE SECTION
3. I-O
✓ For storing intermediate results, counters and
4. Extend
end-of-file indicators through the use of
variable data field.
✓ For storing initial value not contained in the - When the program needs to read data from a file, the
input file. file must be opened for INPUT. A file is opened for
OUTPUT when the program will write data to the file.
4. PROCEDURE DIVISION Only direct access files can be opened for I-O and
Syntax: 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.

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

• The third statement opened EMPLOYEE-FILE Explanation:


as I-O, which means it can be used as an INPUT • In the give example, the records of the
and at the same time as OUTPUT. STUDFILE will be sequentially read until the end
• In the fourth statement, SALES-FILE was of the file is reached.
opened using the EXTEND option which means • The imperative statement MOVE 1 TO EOFSW
that any new record written to it will be will be executed only if all records in the file have
appended to the end of the file. been read already or a run time error was
• A single OPEN statement may be used to open encountered.
more than one file. • In this READ statement, the PIC clause of
EOFSW must be defined as a numeric data field
- Each filename MUST be preceded by the with one digit (PIC 9). The value moved to the
keyword INPUT or OUTPUT. EOFSW, can be anything that the programmer
- A period is placed at the end of the last file name wants it to be as long as its PIC clause is
only. appropriate for that value.

Example: c. MOVE Statement


OPEN INPUT STUDENT-FILE. - The MOVE statement moves a literal or the
OUTPUT REPORT-FILE. contents of a data field to another data field.
- Receiving Data Field: data field receiving the value
- A single OPEN statement can also be used to open - Sending Data Field: data field from which the data
several input and output files.
are copied.
- There are Two Types of the MOVE statements:
Example:
OPEN INPUT FILE1, FILE2, OUTPUT FILE2, FILE4. • direct MOVE in which a value is directly
moved to a data field,
b. READ Statement • indirect MOVE, in which a value contained
- The READ statement reads data from the input in data field is moved to another data field.
file. The data are READ ONE RECORD AT A TIME.
- When processing a sequential input file, the READ FORMAT/ SYNTAX:
statement reads records in sequence. The first 1. General format of the
READ statement reads the first record, the next record DIRECT MOVE statement:
reads the second record, and so on.
MOVE value TO data-field.
The READ statement has the following format:
Examples:
READ filename AT THE END imperative statement. MOVE 1 TO EOF-SW.
MOVE “Y” TO OPTION.
MOVE 20 TO COUNTER.

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

• If a Numeric Value is Moved, the data field


should be defined as numeric with an adequate The following examples show the effect of the MOVE
number of digits. statements on longer and shorter receiving data fields:
• 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:

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

f. STOP RUN Statement


- The STOP RUN statement terminates execution of
the program. As soon as this statement is
encountered, the program stops execution.
d. WRITE Statement
- FUNCTION: of WRITE statement is to write a STOP statement has the following format:
record to an output file.
STOP RUN.
WRITE statement has the following format:
- The STOP RUN statement DOES NOT HAVE to the
WRITE output-record-name last physical statement in the program, but it MUST
BE the last statement that is executed by the program.
CONTINUATION OF MODULE 3 COMPROG

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:

- The first four of these statements can be used both with


and without a COBOL feature called the GIVING clause. Explanation:
• In this example, the content of the data TOTAL-
g. 1. ADD Statement QUIZ has been changed, but the contents of the
- The ADD statement is used to add the contents other three data fields remain unchanged.
of two or more numeric data fields.
- A Numeric Literal can be used in the ADD
ADD Statement Format: statement.
- However, the result of the calculation MAY NOT
ADD datafield-1 TO datafield-2. BE STORED in a Numeric Literal.

- 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.

- With an ADD statement, the result is stored in the


data field LISTED LAST.
RULES FOR ADD STATEMENT
- On EXECUTION of the ADD statement, the
▪ The contents of the first data field are added to the
contents of the data field that receives the result of
contents of the last data field.
the calculation are changed.
- However, the contents of the other data field ▪ The result is always stored in the last data field. All
remain unchanged. other data fields remain unchanged.
▪ Any number of data fields may be listed in an ADD
Example No.1: statement. They all will be added to the last data
field.
▪ The last data field must be large enough to hold the
sum.
▪ 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.
▪ 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.
CONTINUATION OF MODULE 3 COMPROG

RULES FOR SUBTRACT STATEMENT


h. 2. SUBTRACT Statement ▪ The contents of the first data field are subtracted from
- The SUBTRACT statement subtracts the contents the contents of the last data field.
of two or more data fields. ▪ The result is always stored in the last data field. All
other data fields remain unchanged.
SUBTRACT Statement Format: ▪ Any number of data fields may be subtracted from the
last data field.
SUBTRACT datafield-1 FROM datafield-2. ▪ As with ADD statements, numeric literals may be
used in SUBTRACT statements. However, the result
- The SUBTRACT statement subtracts the contents
must be stored in a data field. The last item in a
of datafield-1 FROM the contents of datafield-2 and
SUBTRACT statement must be a data field.
stores the result of the calculation in datafield-2.
- The contents of datafield-1 REMAIN
i. 3. MULTIPLY Statement
UNCHANGED after the statement is executed.
- The MULTIPLY statement multiplies the contents
of two data fields.
Example No.1:
MULTIPLY Statement Format:

MULTIPLY datafield-1 BY datafield-2.

- 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.

Example No.1:

- It is possible to subtract the contents of several


Explanation:
data fields from that of another data field.
• The result in product is stored in PRICE.
Example No.2:
RULES FOR MULTIPLY STATEMENT
▪ The contents of two data fields are multiplied
together.
Explanation:
▪ The result is always stored in the second data field.
• The contents of WITH-TAX and MEDICARE
remain unchanged, the contents of GROSS- The other data field remains unchanged.
INCOME were changed. ▪ A MULTIPLY statement can involve only two data
fields.
- It is possible to use a numeric literal in the ▪ Care must be taken to ensure that the PIC clause of
SUBTRACT statement. the data field where the result of the multiplication will
be store is large enough to hold the result.
Example No.3: ▪ As with ADD and SUBTRACT statements, numeric
literals are allowed in the MULTIPLY statement, but
the result of the multiplication must be sorted in a
data field.
CONTINUATION OF MODULE 3 COMPROG

j. 4. DIVIDE Statement k. 5. COMPUTE Statement


- The DIVIDE statement divides the contents of one - The COMPUTE statement is used to determine the
data field by the contents of another data field. value of arithmetic expressions.

DIVIDE Statement Formats: COMPUTE Statement Format:


1. First Type
COMPUTE data-field = arithmetic expression.
DIVIDE datafield-1 BY datafield-2.
- The word COMPUTE is followed by a blank space,
- The DIVIDEND is the number being divided a DATA FIELD where the result of the calculation is
and the DIVISOR is the datafield-2. to be stored, another blank space, an EQUAL SIGN
- In a DIVIDE statement, the result of the (=), another blank space, and then the expression.
division is saved in the dividend data field.
- ALL ARITHMETIC SIGNS, such as the equal sign,
Thus, the above DIVIDE statement saves the
plus sign, and minus sign, MUST be preceded and
result in datafield-1.
followed by at least one blank space.
2. Second Type - The COMPUTE statement makes it possible to
perform several arithmetic operations in a single
DIVIDE datafield-1 INTO datafield-2. statement.

- In this statement, the DIVIDEND is datafield-2 Example:


and the DIVISOR is datafield-1.
- The result of division is stored in datafield-2.

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.

Example: c. REMAINDER Clause


- The REMAINDER clause is used in COBOL to store
the remainder of a division operation.
Explanation:
- The REMAINDER clause is used with the DIVIDE
• In this example, the contents of QUIZ1 and
QUIZ2 values DID NOT CHANGED, statement for this purpose.
• Only the content of the data field TOTAL- - When using the REMAINDER clause, the DIVIDE
QUIZ was CHANGED. statement MUST USE the GIVING clause.

ii. GIVING Clause for the SUBTRACT Statement REMAINDER


Format: Clause Format:

- The contents of datafield-1 are DIVIDED BY the


- This statement subtracts the contents of contents of datafield-2, the Integer Part of the
datafield-1 FROM the contents of datafield-2 division operations is STORED in datafield-3, and the
and STORES the result in datafield-3. Remainder is STORED in datafield-4.
- More than two data fields may participate in
the calculation. d. ON SIZE ERROR Clause
- When a data field is NOT LARGE ENOUGH to hold
Example: 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
iii. GIVING Clause for the MULTIPLY Statement TRUNCATED from the LEFT, which greatly affects
Format: 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.
Example:
ON SIZE ERROR Clause Format:

iv. GIVING Clause for the DIVIDE Statement


Format: Example:

You might also like