file-name - GEOCITIES.ws

Download Report

Transcript file-name - GEOCITIES.ws

COBOL
(Common Business
Oriented Language)
1. Introduction
to COBOL





History of COBOL
Coding rules
Program Structure
Data names and Identifiers
Figurative constants
History of COBOL
ANSI (American National Standards
Institute) is an Organization for COBOL
Standards. Like CODASYL, ANSI's
COBOL committee consists of
representatives of academia, user
groups and computer manufacturers.
In 1968 ------ The first ANS version of
COBOL was developed
and approved.
In 1974
------ A 2nd version of ANS
Coding Rules




Columns 1 - 6
: Used for comment
tags, sequence numbers, page or
line numbers.
Column 7
: Used for comment,
continuation or starting a new
page.
1.
An asterisk ( * ) is used to denote a
line as a comment.
2
An underscore ( _ ) is used for the
continuation of non-numeric literals.
Coding Rules





Columns 12 – 72 : These columns are
also referred to as Area B.
Entries such as
1.
File names associates with FD or
SD.
2.
Level numbers 02 to 49, 66 and 88.
3.
PROCEDURE DIVISION sentences.
Columns 73 - 80 : Used to identify
the program. These entries are optional
and usually we omit this entry.
Structure of a
Program



Every COBOL Program consists of four
separate divisions, each with specific
function
The Four Divisions :
IDENTIFICATION DIVISION : Identifies
the name of the program to
the computer. It also can provide
documentation
about the program.
Structure of a
Program


DATA DIVISION
: Describes the
input and output format to be
used by the program. It also
defines any
constant and
work areas necessary for the
processing of data.
PROCEDURE DIVISION : Contains the
instructions necessary for
reading input, processing it, and
creating
output.
Structure of a
Program


Following is the structure of a typical COBOL
program.
IDENTIFICATION DIVISION.
PROGRAM-ID. program-name.
[AUTHOR. author-name.]
[INSTALLATION. installation-name.]
[DATE-WRITTEN. written-date.]
[DATE-COMPILED. compiled-date.]
[SECURITY. authorization.]
Structure of a
Program

ENVIRONMENT DIVISION
CONFIGURATION SECTION.
[SOURCE-COMPUTER. sourcecomputer name.]
[OBJECT-COMPUTER. objectcomputer name.]
[SPECIAL NAMES. special-names
entry.]
[INPUT-OUTPUT SECTION.
FILE-CONTROL. {file-control entry},
...
Structure of a
Program


DATA DIVISION.
[FILE SECTION. file section entries.]
[WORKING-STORAGE SECTION.
working-storage section
entries.]
[LINKAGE SECTION. linkage section
entries.]
PROCEDURE DIVISION.
[section name SECTION.
[paragraph name.
Data names and
Identifiers


Data name
A data name is a reference to the storage
space in the memory where the actual
value is stored. This value takes part in
the operation when that particular data
name is used in the PROCEDURE
DIVISION.
Identifier
A data name can be qualified by another
data name or can be indexed or
Figurative Constants



Figurative constants are literals
representing values that may be
frequently used by most programs.
ZERO / ZEROS / ZEROES
Represents the value 0
SPACE / SPACES
Represents one
or more spaces
or blanks

HIGH-VALUE / HIGH-VALUES
Represents the highest value in the
collating sequence
2. COBOL Divisions

Identification Division

Environment Division

Data Division

Procedure Division

Sample Program
Identification
Division


The IDENTIFICATION DIVISION is the
first division of every COBOL source
program. It is the smallest, simplest
division of a COBOL source program
Of all the paragraphs in this division,
the paragraph PROGRAM-ID is essential
contd…
Identification
Division

Example:
IDENTIFICATION DIVISION.
PROGRAM-ID. PHBOADR.
AUTHOR. CSCI.
INSTALLATION. CSCI.
DATE-WRITTEN. 01-01-1997.
DATE-COMPILED. 01-01-1997.
SECURITY. RESTRICTED TO THE
TRAINING DEPT.
REMARKS. THIS PROGRAM ISSUES A
Environment
Division

The ENVIRONMENT DIVISION must
follow the IDENTIFICATION DIVISION
in a COBOL source program. This is the
only machine-dependent division of a
COBOL program and contains two
sections, viz., CONFIGURATION
SECTION and INPUT-OUTPUT SECTION.
ENVIRONMENT DIVISION.
CONFIGURATION SECTION.
INPUT-OUTPUT SECTION.
FILE-CONTROL.
Data Division



DATA DIVISION.
The DATA DIVISION is that part of the
COBOL program where every data item
processed by the program is described.
This division consists of three sections.
FILE SECTION
WORKING-STORAGE SECTION
LINKAGE SECTION
Data Division

DATA DIVISION.
FILE SECTION.
FD EMP-FILE
RECORDING MODE IS V
LABEL RECORDS ARE
STANDARD
DATA RECORDS ARE EMPRECORD
BLOCK CONTAINS 0 RECORDS.
01 EMP-RECORD.
Procedure Division

Contains statements, which specify
the operations to be performed by the
computer

Each of these statements is formed
with COBOL words and literals

A statement always starts with a
COBOL verb
Procedure Division
PROCEDURE DIVISION.
0001-MAIN-PARA.
DISPLAY "ENTER FIRST NUMBER" AT
0510.
ACCEPT WS-SUM1.
DISPLAY "ENTER SECOND NUMBER" AT
0710.
ACCEPT WS-SUM2.
COMPUTE WS-TOTAL = WS-SUM1 + WSSUM2.
DISPLAY "THE SUM IS "WS-TOTAL.
Sample Program
SAMPLE PROGRAM - 01
IDENTIFICATION DIVISION.
PROGRAM-ID.EXAMPLE-01.
AUTHOR. EMP991.
DATE-WRITTEN 01-01-1999.
DATE-COMPILED 01-01-1999.
ENVIRONMENT DIVISION.
CONFIGURATION SECTION.
Sample Program
DATA DIVISION.
WORKING-STORAGE SECTION.
01 WS-ALL-VARS.
05 WS-SUM1 PIC 999.
05 WS-SUM2 PIC 999.
05 WS-TOTAL PIC 9(4).
PROCEDURE DIVISION
0001-MAIN-PARA.
DISPLAY "ENTER FIRST NUMBER" AT
Sample Program
DISPLAY "ENTER SECOND NUMBER"
AT 0710.
ACCEPT WS-SUM2.
COMPUTE WS-TOTAL= WS-SUM1 +
WS-SUM2.
DISPLAY "THE SUM IS "WS-TOTAL.
STOP RUN.
0001-MAIN-EXIT-PARA.
EXIT.
3. Data Definition
and Editing

File Section

Working Storage Section

Picture Clause

Editing
File Section

The FILE SECTION includes the
descriptions of all data items that
should be read from or written
onto some external file. It contains
the File Description (FD) entry for
each file in the program followed
by the record description.
Working-Storage
Section
& Linkage Section


Any field necessary for processing that
is not part of input or output, i.e., the
data items which are developed
internally as intermediate results, may
be defined in the WORKING-STORAGE
SECTION.
The LINKAGE SECTION is an optional
section in the DATA DIVISION. The
LINKAGE SECTION describes data made
available from another program or
Picture Clause
Specified for every elementary data item.

Code Character
Meaning
9
Data item contains a
numeral
X
Data item contain any
allowable character
from the COBOL character set.
A
Data item contains only a
letter or space
Editing

Editing Symbols
Need for editing data
Edit Types
Z
*
$
+
CR DB
BZ/
Numeric Data
Zero Suppression
Asterisk
Currency sign
Minus sign
Plus sign
Credit Debit Sign
Blank, Zero, Slash Insertion
Editing
Pic of the Field
Numeric Value
9999CR
-4625
4625CR
9999CR
4625
4625bb
ZZZCR
-42
ZZ9V99DB
-152^25
15225DB
ZZZ9V99DB
-152^25
Edited Value
b42CR
4.Procedure Division
and Basic Verbs

Structure of Procedure Division

Move Statement

Arithmetic Verbs

Control verbs

I/O verbs
Structure of
procedure Division



The following format shows the
simplified structure of the Procedure
Division.
PROCEDURE DIVISION.
[Paragraph-name. [sentence] …]…
A paragraph can consists of one or more
sentences that constitute its body.
MOVE Statement


Copies the contents of one data item
(the sending field) to another data item
(the receiving field), keeping the contents
of the first data item unchanged
Syntax
MOVE
{ identifier-1 }
TO
identifier-2 [,identifier-3]...
{ literal-1 }
Sending Field
Receiving Field
MOVE Statement
Moving an integer

Movement is from right to left (right
aligned)

Non-filled higher-order (leftmost)
integers are replaced by 0s

Moving a decimal

Movement is decimal aligned

Non-filled lower-order (rightmost)
integers are replaced by 0s

MOVE Statement

Example :
MOVE
AMT-IN
AMT-IN
PIC
999
VALUE
123
PIC
999v99
9(4)v999
VALUE
123^15
0123 150
TO AMT-OUT
AMT-OUT
9(4)
0123
Arithmetic Verbs
ADD

Elementary numeric data items alone
can be added

Numeric literals alone can be added

Decimal point alignment is automatic
 Arithmetic Statement
A
B
C

ADD A TO B C
C+A
A
B+A
Arithmetic Verbs
SUBTRACT

Elementary numeric data items alone
can be subtracted

Numeric literals alone can be
subtracted

Decimal point alignment is automatic

Arithmetic Statement
A
B
SUBTRACT A B
C-(A+B)
A
B
C
Arithmetic Verbs

MULTIPLY

Elementary numeric data items alone
can be multiplied.

Numeric literals alone can be
multiplied.

Decimal point alignment is
automatic.
Arithmetic Statement
A
B
C
Arithmetic Verbs

DIVIDE

Arithmetic Statement


A
DIVIDE A INTO B C
C/A
DIVIDE A BY B
GIVING C
A
B
C
A
B/A
B
A/B
Arithmetic Verbs

Compute

Combination of more than one
arithmetic operation in a single
statement.

Shortens programs, avoids
intermediate data names.
Example :
COMPUTE
A
=
2 + (3 * 5).
Control Verbs

The statements are executed
sequentially one after another.
This sequence can be altered with the
help of Control Verbs.

PERFORM

EXIT

GO TO
I/O Verbs






DISPLAY
ACCEPT
OPEN
READ
WRITE
CLOSE
Conditional Verbs

IF followed by END-IF.

Nested IF sentence.

EVALUATE followed by END-EVALUATE.

Note: for slides 30 to 42 refer chapter 6.
5. Writing a sample
Program
IDENTIFICATION DIVISION.
PROGRAM-ID.EXAMPLE-01.
AUTHOR. RAJU.
ENVIRONMENT DIVISION.
CONFIGURATION SECTION.
SOURCE-COMPUTER PMSI-PC-nn.
OBJECT-COMPUTER PMSI-PC-nn.
Writing a sample
program

DATA DIVISION.
WORKING-STORAGE SECTION.
01 WS-ALL-VARS.
05 WS-SUM1 PIC 999.
05 WS-SUM2 PIC 999.
05 WS-TOTAL PIC 9(4).
Writing a sample
Program
PROCEDURE DIVISION
0001-MAIN-PARA.
DISPLAY "ENTER FIRST NUMBER" AT
0510.
ACCEPT WS-SUM1.
DISPLAY "ENTER SECOND NUMBER"
AT 0710.
ACCEPT WS-SUM2.
COMPUTE WS-TOTAL= WS-SUM1 +
WS-SUM2.
DISPLAY "THE SUM IS “ WS-TOTAL.
Exercise - 1
1)
2)
Accept data from the user in the format
DDMMCCYY. Find difference between
the current date and the user date in
terms of number of days?
Accept a number( N between 1 - 99)
from the user and write a multiplication
table for the given number.
Example
1 * 20 = 20
2 * 20 = 40
|
|
|
|
6. More About Data
Division

USAGE Clause

JUSTIFIED clause

REDEFINES

RENAMES
USAGE clause

USAGE IS COMPUTATIONAL/COMP
COMP
COMP-1
COMP-2
COMP-3
Justified clause

JUSTIFIED/JUST RIGHT CLAUSE
Example :
With a field description:
02 TITLE
PIC X(10).
The command:
MOVE "JONES" TO
TITLE
Stores in TITLE a value equal to
"JONESbbbbb", with the name leftjustified; but, if the field description is:
REDEFINES

It is used for the purpose of conservation
of storage space.
Example:
01 SALES-RECORD.
02 SALES-TYPE
02
PIC X.
SALES-BY-UNIT
03
03
QTY
UNIT-PRICE
PIC 9(4).
PIC 9(8)V99.
02
TOTAL-SALES REDEFINES SALES-BYUNIT.
03
AMOUNT
PIC 9(10)V99.
RENAMES

Regrouping of elementary data items.
It must be used with the special level
number 66.
66 data-name-1 RENAMES data-name-2
THRU data-name-3.
7. TABLE Handling

OCCURS Clause

PEFORM verb

INDEXING

SET verb

SEARCH
OCCURS Clause


TO be used for putting data in a tabular
format.
This clause cannot be specified for an
item whose level number is 01, 66, 77
and 88.
Example :
01 DIRECT-TAX-RATE
02 TAX-RATE
PIC 99 OCCURS 10
PERFORM
Different types of PERFORM Statements.


PERFORM PROCEDURE-NAME-1.
PROCEDURE-NAME-1 is executed only
once.
PERFORM PROCEDURE-NAME-1 N TIMES.
PROCEDURE-NAME-1 is executed N
number of times.
PERFORM
The other types of PERFORM statement



PERFORM PROCEDURE-NAME-1 THRU
PROCEDURE-NAME-2.
PERFORM PROCEDURE-NAME-1 UNTIL
Condition.
PERFORM PROCEDURE-NAME-1 with
VARYING option.
INDEXING



INDEX: It is a Data item associated with
a table, denotes a displacement of the
element from the beginning of the
table.
Example :
01 EMP-TABLE.
02 DEPT OCCURS 10 TIMES
INDEXED BY D1.
03 MANAGER OCCURS 5
SET Verb

This Verb is used to set, increase or
decrease the values of the Indexes.

Examples :

SET D1 to 4.

SET D1 UP BY 2.

SET D1 DOWN BY 2.
SEARCH
This verb is used to locate elements in
table.
 It will Search the elements in the table
sequentially.
 Example :
PARA-REPEAT.
SEARCH ACCOUNT-TABLE
AT END GO TO PARA-REPEAT-EXIT
WHEN AMOUNT (A1) > 5000
ADD 1 TO TOTAL-NUM

SEARCH ALL
This verb is used to locate elements in
table.
 The table should be in some
order(ascending/descending).
 The advantage is total comparisons are
less compared to sequential Search.
 Example :
PARA-REPEAT.
SEARCH ALL ACCOUNT-TABLE
AT END GO TO PARA-REPEAT-EXIT
WHEN AMOUNT (A1) > 5000

Exercise - 2
1) Write a program to accept 2 3x3
matrices and add them. Program
should accept maximum of 3 digit
number excluding sign digit.Display
the result on the screen in the matrix
form.
2) Accept N 3 digit number excluding sign
from the user. Accept one more number
from the user to search, whether that
number is already in the entered
8. Structured
Programming
Enabling optimisation of code by
presenting the logic in the most
structured way possible.
Using the three kinds of control structures
that facilitate Structured Programming
namely.

Simple Sequencing

Selection
Simple Sequencing

Simple Sequencing is a sequence of of two
or more operations.
Example
............

0001-MAIN-PARA.
PERFORM 0002-OPEN-PARA.
PERFORM 0003-PROCESS-PARA.
PERFORM 0004-CLOSE-PARA.
............
Selection

Execution of one of two operations depending
on a condition
Example
............

IF WS-EOF = "N"
PERFORM 0002-OPEN-PARA
ELSE
PERFORM 0003-DISPLAY-PARA
............
Repetition

Continually performing an operation
until a certain condition holds true
Example
............
PERFORM 0002-OPEN-PARA
UNTIL WS-EOF = "Y"
: iteration of a para (set of instructions)
till a condition is true.

Exercise - 3
1) Write a program to calculate sales tax
based on entered sales-amount and
Tax-rates.
Sales-tax = Sales-Amount * Tax-Rates
Validate the Sales-Amount is in between 1
to 10000 and Tax-Rate is
1 to 20%. If the entered data is invalid
then it should display proper message
like “Invalid entry”.
Exercise – 3
(Optional)
2) Write a program by using Evaluate
verb to calculate electricity bill rate
based on number of units.
No-of-units(1 to 100) the rate per unit
is 1 Rs.
101 to 200
“
is
2 Rs.
201 to 500
“
is
2.5 Rs.
501 to Any “
is
9. Sequential Files
Important features are:

Records are stored serially-one after
the other.

Used when data volume is very high
or when all the records stored in the file
are to be processed .


File-Control entries

File Description
File-control Entries




Organization
Access
File Status
SELECT FILE-NAME ASSIGN TO
HARDWARE-NAME
[; ORGANIZATION IS
SEQUENTIAL ]
[;ACCESS MODE IS
SEQUENTIAL ]
[;FILE STATUS IS Data-name-
File Description




Block Contains
Record Contains
Label Record
FD File-name
[; BLOCK CONTAINS integer-1
{RECORDS/CHARACTERS} ]
[; RECORD CONTAINS integer-2
{CHARACTERS} ]
[; LABEL RECORDS ARE
{STANDARD/OMITTED}]
Statements for
Sequential File

OPEN

READ

WRITE

REWRITE

CLOSE
Statements for
Sequential File

OPEN { INPUT } file-name-1,
[,filename-2]…
{ OUTPUT }
{ EXTEND }
{ I-O
}
READ file-name RECORD [ INTO identifier1]
Statements for
Sequential File

WRITE record-name [ FROM identifier ]

REWRITE record-name [FROM identifier ]

CLOSE file-name-1 [, file-name-2 ] …
Exercise - 4
1)Write a program to create a data file
which contains the Name of the person
and Phone number by accepting data
from the user.
2) Write a program to read records from
the Input-file and write the records in
the output-file .
product-amount = quantity-inhand * unit-price
10. Sorting and
Merging of Files

Simple SORT Verb

Simple Merge Verb


INPUT and OUTPUT Procedures in SORT
statement
MERGE Verb with Output Procedure
SORT Verb


It is used for Sorting an Input-file, after
sorting a new Output file is created
contains the records in sorted order.
SORT file-name-1 ON
{ASCENDING/DESCENDING} KEY
data-name-1 [,data-name-2]…
USING file-name-2 GIVING filename-3.
Here file-name-1 is the Work file, defines
Merge Verb


It is used to Merge two or more Input
files.
MERGE file-name-1 ON
{ASCENDING/DESCENDING} KEY
data-name-1 [,data-name-2]…
USING file-name-2, file-name-3
[,file-name-4 ]
GIVING file-name-5.
INPUT OUTPUT
Procedures

INPUT PROCEDURE:
The records which meet certain conditions are
to be selected for sorting.
RELEASE statement is part of the INPUT
PROCEDURE

OUTPUT PROCEDURE:
Used for editing of sorted records.
RETURN statement is a part of OUTPUT
PROCEDURE
Only Output Procedure is used in MERGE
statement.
11. Direct Access Files

RELATIVE FILES

INDEXED FILES

I/O statements for different File Organizations
Relative Files
Organized in such a way each record
is identified by a relative Record
number.

The relative record number specifies
the position of the records from the
beginning of the file.

FILE-CONTROL for Relative files
 Example:
SELECT HISTORY ASSIGN TO DISK
ORGANIZATION IS RELATIVE

Relative Files






PROCEDURE DIVISION for Relative files
READ
WRITE
REWRITE
DELETE
START
contd…
Relative Files


READ
READ file-name RECORD [ INTO
identifier ]
[ ; INVALID KEY imperativestatement ]
WRITE
WRITE record-name [ FROM identifier ]
[ ; INVALID KEY imperativestatement ]
Relative Files


DELETE
DELETE file-name RECORD [ ; INVALID
KEY imperative-statement ]
START
START file-name [ KEY IS { EQUAL TO
/
} data-name ]
GREATER THAN /
NOT LESS
THAN
[ ; INVALID KEY imperative-
INDEXED Files

Records are accessed by the value of
one or more keys within them.
FILE-CONTROL for Indexed files
 Example:
SELECT HISTORY ASSIGN TO DISK
ORGANIZATION IS INDEXED
ACCESS MODE IS DYNAMIC
RECORD KEY IS data-name-1

INDEXED Files






PROCEDURE DIVISION for Indexed
files
READ
WRITE
REWRITE
DELETE
START
The syntax is same as Relative files.
I/O statements for
different files
FILE ACESS TABLE Sequential
Organization

Statement
F Seque READ
I ntial
WRITE
L
REWRI
TE
E
START
DELETE
A Rand
C
om
C
E
S
READ
WRITE
REWRI
TE
START
I
0
I- E
Open Modes
O
X
X
X
X
X
Indexed
Organizatio
n
I
O
IOpen
O
Modes
X
X
X
X
X
X
X
Relative
Organizat
ion
I
O IOpen O
Modes
X
X
X
X
X
X
X
X
X
X
X
X
X
X
X
X
X
X
X
12. Character
Handling

A string refers to a sequence of
characters. We can do string
manipulations by using the following
verbs.

Examine

Inspect

String
Examine Verb


EXAMINE Verb
It is used to scan a string to find the
number of occurrences of a given
character. It is also used to replace
some or all occurrences of a said
character.
Example :
01 NAME
PIC X(5) VALUE IS
“DAVID”.
EXAMINE NAME TALLYING ALL “D”

INSPECT Verb

INSPECT Verb
It is used to tally and/or replace the the
occurrence of a single or group of
characters in a data field.
 Example :
01 MY-STRING PIC X(20) VALUE IS “
BACDABCZPABCABCPQ .“

INSPECT MY-STRING TALLYING TALLY-1
FOR ALL “ABC”
STRING


Concatenates the partial or complete
contents of two or more data items and
stores the result.
Example :
01 WS-A
01 WS-B
01 WS-C
STRING
BY ' '
BY ' '
PIC X(6) VALUE "HUMPTY".
PIC X(6) VALUE "DUMPTY".
PIC X(14).
WS-A
WS-B
DELIMITED
DELIMITED
UNSTRING
Splits the contents of a data item into
separate character strings and stores
them.
 Example :
01 WS-A
PIC X (13) VALUE
'HUMPTY DUMPTY'
01 WS-B
PIC X (6)
01 WS-C
PIC X (6)

UNSTRING
WS-A
DELIMITED BY ' '
Reference
Modification


It is possible to refer part of a data field
by specifying the leftmost character
position and length.
Example :
01 DATA-FIELD1 PIC X(11) VALUE
“MATHEMATICS”.
MOVE DATA-FIELD-1(4:2) TO DATAFILED-2.
13. Subroutines

Structure of a COBOL Subroutine

Calling of a Subroutine

State of a Subroutine and Cancel
statement
Structure of a
Subroutine
The PROCEDURE DIVISION header must
have using phrase.
The format is
PROCEDURE DIVISION [ USING dataname-1 [, data-name-2]…]

Data-name-1 and data-name-2 must be
defined in the
LINKAGE SECTION of called Program.
The corresponding data items in the
calling program may be defined in any
Calling a Subroutine
Main program(calling program) will call
the subroutine by using CALL
statement.
Actual Parameters
The data-names defined in the Calling
Program which is establishing
connection with formal parameters.
Formal parameters
The data items defined in the Linkage
section of the Called program.
Cancel Statement
When a subroutine is called for the first
time, it is said to be in its initial
state.When same subroutine is called
next, it will be in its last used state.
CANCEL :
Used for setting the subroutine in its
initial state rather than the last used
state.
CANCEL { identifier-1 / literal-1 } [,
COBOL END