COBOL_CDC[1]
-
Upload
lalith-kumar -
Category
Documents
-
view
219 -
download
0
Transcript of COBOL_CDC[1]
-
7/30/2019 COBOL_CDC[1]
1/313
CDC_Covansys_ChennaiCOBOL _ # 1 _Ver 2.0
COBOL
-
7/30/2019 COBOL_CDC[1]
2/313
CDC_Covansys_ChennaiCOBOL _ # 2 _Ver 2.0
Chapter - 1 - Introduction
1.1 History Of COBOL
1950s Growing need for a high-level languagesuitable for programming in business dataprocessing
1958 US Department of Defence convened a
conference to form three groups to startwith the actual design of the language
1959 1st report on COBOL (COmmon BusinessOriented Language)
1960 Modified version of the earlier report ;CODASYL(Conference on DATA System Language) COBOLprogramming language - the board of directorateestablished a COBOL maintenance committee tokeep COBOL in step with the times
-
7/30/2019 COBOL_CDC[1]
3/313
CDC_Covansys_ChennaiCOBOL _ # 3 _Ver 2.0
1.1 History Of COBOL
1961 COBOL-61 published with some revisionsof the 1960 report; the basis for later versions
1962 1st COBOL compiler made available ; usersstarted writing programs in COBOL
1965 Next Version
1968 Standard Version of COBOL approved bythe American National Standards Institute(ANSI)
1974 ANSI 74 COBOL (COBOL 74)
1985 ANSI 85 COBOL (COBOL II / COBOL 85)
1990s COBOL 9x (Proposed)
Chapter - 1 - Introduction
-
7/30/2019 COBOL_CDC[1]
4/313
CDC_Covansys_ChennaiCOBOL _ # 4 _Ver 2.0
1.2 Features Of COBOL
Oriented towards business applications
English-like
Self-Documenting
Machine independent
Easy to Learn / Read / Write / Test
I/O operations easily designated
Structured Programming Support
Chapter - 1 - Introduction
-
7/30/2019 COBOL_CDC[1]
5/313
CDC_Covansys_ChennaiCOBOL _ # 5 _Ver 2.0
1.3 Source Coding Rules
1 6 7 8 11 12 72 73 80
Sequence
Number Area A Area B
Area
Indicator Area Ignored Area
Chapter - 1 - Introduction
-
7/30/2019 COBOL_CDC[1]
6/313
CDC_Covansys_ChennaiCOBOL _ # 6 _Ver 2.0
1.3 Source Coding Rules
Indicator Area
* in column 7 Line treated as a comment ; Shown in thesource listing
/ in column 7 Line treated as a comment ; Source listingbegins a new page before printing the line
- in column 7 Line treated as continuation of previous line ;(hyphen )
Except when non-numeric literals are continued, all trailingspaces on the preceding line and all leading spaces on thecontinuation line, are ignored ;
Chapter - 1 - Introduction
-
7/30/2019 COBOL_CDC[1]
7/313
CDC_Covansys_ChennaiCOBOL _ # 7 _Ver 2.0
1.3 Source Coding Rules
Area A
Area A (columns 8 thru 11) of the continuation line must be blank
Cols 1 thru 6 Each line of code may have a 6 digit line number
Line numbers to be in ascending order
Blanks also permitted
Area A Reserved for DIVISION, SECTION & PARAGRAPHheaders
File description entry indicators (FD & SD), level nos. 01 and 77
Comment line characters also accepted, provided column 7contains an * or /
Chapter - 1 - Introduction
-
7/30/2019 COBOL_CDC[1]
8/313
CDC_Covansys_ChennaiCOBOL _ # 8 _Ver 2.0
1.3 Source Coding Rules
Area B
Area B Reserved for Filenames associated with FD or SDindicators and the SELECT clause
Level nos. 02 to 49 and 66 and 88
Descriptive clauses and/or data descriptions that make up specific
paragraph entries
Continuation lines, provided column7 contains a (hyphen) -
Chapter - 1 - Introduction
-
7/30/2019 COBOL_CDC[1]
9/313
CDC_Covansys_ChennaiCOBOL _ # 9 _Ver 2.0
1.4 COBOL Character Set and Words
COBOL Character Set
51 different characters
0 to 9 10 Numerals - digits
A to Z 26 English alphabets - letters
Space or Blank ( generally denoted as b )
Note_01 The Space or Blank character in certain cases is treated asa letter
Chapter - 1 - Introduction
-
7/30/2019 COBOL_CDC[1]
10/313
CDC_Covansys_ChennaiCOBOL _ # 10 _Ver 2.0
COBOL Character Set
51 different characters
- + * / = Minus/Hyphen
Plus
Asterisk
Slash
Equal to
$ ( ) < > Currency
Left parenthesisRight parenthesis
Less than
Greater than
Chapter - 1 - Introduction
-
7/30/2019 COBOL_CDC[1]
11/313
CDC_Covansys_ChennaiCOBOL _ # 11 _Ver 2.0
COBOL Character Set
51 different characters . , ; " Period/Decimal point
Comma
Semi Colon
Quotes
Collating Sequence
An ordering that defines the relative rank of all the validcharacters in a computer system
Chapter - 1 - Introduction
-
7/30/2019 COBOL_CDC[1]
12/313
CDC_Covansys_ChennaiCOBOL _ # 12 _Ver 2.0
1.4.2 COBOL Words
User-Defined Words
Formed from 0 to 9, A to Z and hyphen (-)
Not to be more than 30 characters long
( greater than 30 characters in case of COBOL 9x )
Not to contain blank or any special character except hyphen (-)
Not to begin with or end with a hyphen (-) ; Can have hyphens inbetween
Should contain at least one alphabet
Should not be a reserved word
Chapter - 1 - Introduction
-
7/30/2019 COBOL_CDC[1]
13/313
CDC_Covansys_ChennaiCOBOL _ # 13 _Ver 2.0
1.4.2 COBOL Words
User-Defined Words
Practice
BASIC-PAY-BASIC-A
OVERTIME-H0URS OVER TIME RATE
1-2-3
B12-4
AGE-IN-YEARS
COVANSYS INDIA PRIVATE LIMITED
Chapter - 1 - Introduction
-
7/30/2019 COBOL_CDC[1]
14/313
CDC_Covansys_ChennaiCOBOL _ # 14 _Ver 2.0
Reserved Words
Have a specific meaning to the compiler
Must be spelt correctly and used in the proper context
Cannot be used as user-defined words in a program
Eg.
PERFORM, IDENTIFICATION, MOVE, IF, DIVISION
Chapter - 1 - Introduction
-
7/30/2019 COBOL_CDC[1]
15/313
CDC_Covansys_ChennaiCOBOL _ # 15 _Ver 2.0
Data Names & Identifiers
Refer to the storage space in memory where the actual value isstored
The value takes part in the operation when that particular dataname is used in the Procedure Division
In certain cases a data name alone is not good enough for use inthe Procedure Division. In such cases, the data name has to bequalified by another data name or can be indexed or subscripted
In general, a single data name or a data name qualified, indexedor subscripted is normally referred to by the term identifier
To be a user-defined word and not a reserved word
Chapter - 1 - Introduction
-
7/30/2019 COBOL_CDC[1]
16/313
CDC_Covansys_ChennaiCOBOL _ # 16 _Ver 2.0
Data Names & Identifiers
Exercise
TOTAL-HOURS
DATA
ADD
QTY-REQUIRED
46-2A
A45
46+2A
46 - 2A
342B2
45
Chapter - 1 - Introduction
-
7/30/2019 COBOL_CDC[1]
17/313
CDC_Covansys_ChennaiCOBOL _ # 17 _Ver 2.0
Literals
Actual values that appear in the program, instead of a data name
The value of a data name can vary during program execution, but,
that of a literal, is specific and remains unchanged throughout theprogram execution
Represent themselves and are not required to be defined in the
Data Division
Chapter - 1 - Introduction
-
7/30/2019 COBOL_CDC[1]
18/313
CDC_Covansys_ChennaiCOBOL _ # 18 _Ver 2.0
Literals
Chapter - 1 - Introduction
Numeric Literal Non-Numeric Literal
Can have digits alone Used to output messages/headings
A combination of either of + -and/or a period (.)
Characters enclosed within quotes ( )
+ - to be the leftmost character ;
No blank between the sign and the first digit ;
Period (.) not to be the lastcharacter ;
If no period (.), treated as integer ;If no sign, treated as positive
Any character within quotes except another quote
() ;
In ANSI 74, whenever a quote( or ) is to be represented as a part of a value,
it is to replicated( or respectively )
eg DONT WORRY
DONT WORRY
Max # of digits 18(compiler dependent)
Max # of characters 120(compiler dependent)(greater than 120 characters in case of COBOL 9x)
C
-
7/30/2019 COBOL_CDC[1]
19/313
CDC_Covansys_ChennaiCOBOL _ # 19 _Ver 2.0
Literals
Practice
.725
150
12.5
SEVEN
12.5
-46
-38.62
Chapter - 1 - Introduction
Practice
EIGHT-24.
2345678923456789234.5689
QTY*PRICE12.5
DATA DIVISION
7
-
7/30/2019 COBOL_CDC[1]
20/313
CDC_Covansys_ChennaiCOBOL _ # 20 _Ver 2.0
Figurative Constants
Literals representing values that may be frequently used by mostprograms
Fixed names that are recognized by the compiler which sets up thecorresponding values in the object program
Eg1. ZERO 0
ZEROS
ZEROES
SPACE b
SPACES
ALL ******
Eg2.
MOVE 0 TO TOTAL
can be written as MOVE ZERO TO TOTAL
Chapter - 1 - Introduction
QUOTE
QUOTES
HIGH-VALUE maximum possiblealpha-numeric value ;
HIGH-VALUES the computer's highest value
LOW-VALUE minimum possiblealpha-numeric value ;
LOW-VALUES the computer's lowest value
-
7/30/2019 COBOL_CDC[1]
21/313
CDC_Covansys_ChennaiCOBOL _ # 21 _Ver 2.0
1.5 COBOL Program StructureIDENTIFICATION DIVISION.
PROGRAM-ID. entry.
[AUTHOR. author name.][INSTALLATION. installation name.]
[DATE-WRITTEN. written-date.]
[DATE-COMPILED. compiled-date.]
[SECURITY. authorization.]
[REMARKS. prologue.]
ENVIRONMENT DIVISION.[CONFIGURATION SECTION.
SOURCE-COMPUTER. source-computer-entry.
OBJECT-COMPUTER. object-computer-entry.
[SPECIAL-NAMES. special-names-entry.] ]
INPUT-OUTPUT SECTION.
FILE-CONTROL. {file-control-entry}...
DATA DIVISION.FILE SECTION.WORKING-STORAGE SECTION.
[Working-Storage section entries.
PROCEDURE DIVISION.
[ [section name SECTION.]
[ [paragraph name. [statements/sentences]...]...]...]
Chapter - 1 - Introduction
C CO O
-
7/30/2019 COBOL_CDC[1]
22/313
CDC_Covansys_ChennaiCOBOL _ # 22 _Ver 2.0
Chapter - 2 COBOL Divisions
2.1 Identification Division
First division of a COBOL program - the leading division
Specifies information about the program to others who mayread or use the program
Contains several paragraphs
The PROGRAM-ID paragraph is mandatory ; Otherparagraphs are optional and are used mainly for
documentation purposes
Division headings and Paragraph names to be coded asmargin A entries
-
7/30/2019 COBOL_CDC[1]
23/313
CDC_Covansys_ChennaiCOBOL _ # 23 _Ver 2.0
Chapter - 2 COBOL Divisions
2.1 Identification Division
Paragraph names to end with a period followed by at least oneblank space
Entries following paragraph names to be terminated with aperiod
PROGRAM-ID paragraph entry - program name with at least
one letter within the 30 allowable characters
Optional paragraph entries - treated as comments
-
7/30/2019 COBOL_CDC[1]
24/313
CDC_Covansys_ChennaiCOBOL _ # 24 _Ver 2.0
Sample Program
IDENTIFICATION DIVISION.
PROGRAM-ID. EXAMPLE-01.
AUTHOR. CVNS(I).
DATE-WRITTEN. 01-01-1999.
DATE-COMPILED. 01-01-1999.
SECURITY. RESTRICTED TO THE TRAINING DEPT.
INSTALLATION. CVNS(I).
Chapter - 2 COBOL Divisions
Ch 2 COBOL Di i i
-
7/30/2019 COBOL_CDC[1]
25/313
CDC_Covansys_ChennaiCOBOL _ # 25 _Ver 2.0
Chapter - 2 COBOL Divisions
2.2 Environment Division
Second division of a COBOL program
Most machine-dependent division of all the four
Specifies the computer and all other peripheral devicesrequired by the program
Division headings, Section headings and Paragraph headings
to be followed by a period and a space
Paragraph entries coded as margin B entries allowed to startin the same line with the paragraph heading
-
7/30/2019 COBOL_CDC[1]
26/313
CDC_Covansys_ChennaiCOBOL _ # 26 _Ver 2.0
Chapter - 2 COBOL Divisions
2.2.1 Configuration Section
Overall specification of the computer used for the purpose ofcompilation & execution of the program
SOURCE-COMPUTER. (para-1)
Name of the computer used to compile the COBOL program
Eg. SOURCE-COMPUTER. computer-name.
Ch 2 COBOL Di i i
-
7/30/2019 COBOL_CDC[1]
27/313
CDC_Covansys_ChennaiCOBOL _ # 27 _Ver 2.0
Chapter - 2 COBOL Divisions
Configuration Section
OBJECT-COMPUTER. (para-2)
Name of the computer used to execute the COBOL program
Eg. OBJECT-COMPUTER. computer-name.
SPECIAL-NAMES. (para-3)
Relate some hardware names to user-specified mnemonic names
Ch 2 COBOL Di i i
-
7/30/2019 COBOL_CDC[1]
28/313
CDC_Covansys_ChennaiCOBOL _ # 28 _Ver 2.0
Chapter - 2 COBOL Divisions
Configuration Section
Eg.
SPECIAL-NAMES. [,CURRENCY SIGN IS literal-1]
[,DECIMAL-POINT IS COMMA]
Currency When a currency symbol other than $ is
required ; A one-character symbol ( not a digit ;not one of A to D,L,P,R,S,V,X,Z ; not special
characters blank * - , . ; () + / = )
Decimal-Point To edit a numeric data in the European manner
and thus interchange the roles of comma (,)
and period (.) as edit characters
Ch 2 COBOL Di i i
-
7/30/2019 COBOL_CDC[1]
29/313
CDC_Covansys_ChennaiCOBOL _ # 29 _Ver 2.0
Chapter - 2 COBOL Divisions
Configuration Section
Eg.
ENVIRONMENT DIVISION.
CONFIGURATION SECTION.
SOURCE-COMPUTER. CVNS(I)-PC-nn.
OBJECT-COMPUTER. CVNS(I)-PC-nn.
SPECIAL-NAMES. CURRENCY IS &.
Ch t 2 COBOL Di i i
-
7/30/2019 COBOL_CDC[1]
30/313
CDC_Covansys_ChennaiCOBOL _ # 30 _Ver 2.0
Chapter - 2 COBOL Divisions
2.2.2 Input-Output Section
Information regarding files to be used in the program
File-Control (para-1)
Name of each file and the file medium associated with it
Eg.FILE-CONTROL.SELECT [OPTIONAL] file-name ASSIGN TO hardware name
One file control entry each for all the files used in a COBOLsource program
An entry to name the file and assign a peripheral device whichholds that particular file
Ch t 2 COBOL Di i i
-
7/30/2019 COBOL_CDC[1]
31/313
CDC_Covansys_ChennaiCOBOL _ # 31 _Ver 2.0
Chapter - 2 COBOL Divisions
2.2.2 Input-Output Section
File-name in SELECT clause to be unique and to be
mandatorily described in the data division. Optional
Used only for input files
Optional files need not be present on every occasion
when the object program is executed
Any attempt to open an optional file, that is absent,
will not result in an error ; the absent optional file
will be considered to be an empty file (that does not
contain any record)
If the OPTIONAL clause is omitted and the file is not present forreading while executing the program, an execution error will occur
Ch t 2 COBOL Di i i
-
7/30/2019 COBOL_CDC[1]
32/313
CDC_Covansys_ChennaiCOBOL _ # 32 _Ver 2.0
Chapter - 2 COBOL Divisions
2.2.2 Input-Output Section
If the OPTIONAL clause is omitted and the file is not present forreading while executing the program, an execution error willoccur
Assign
Assign a particular physical peripheral device name to a file
Physical peripheral device names are machine dependent
Chapter 2 COBOL Divisions
-
7/30/2019 COBOL_CDC[1]
33/313
CDC_Covansys_ChennaiCOBOL _ # 33 _Ver 2.0
Chapter - 2 COBOL Divisions
Input-Output Section
Eg.DISK, PRINTER, READER, TAPE ....
Eg.
ENVIRONMENT DIVISION.
CONFIGURATION SECTION.
SOURCE-COMPUTER. CVNS(I)-PC-nn.
OBJECT-COMPUTER. CVNS(I)-PC-nn.
INPUT-OUTPUT SECTION.
FILE-CONTROL.
SELECT CVNS(I)-File-001ASSIGN TO DISK.
SELECT CVNS(I)-File-002 ASSIGN TO PRINTER.
Ch t 2 COBOL Di i i
-
7/30/2019 COBOL_CDC[1]
34/313
CDC_Covansys_ChennaiCOBOL _ # 34 _Ver 2.0
Chapter - 2 COBOL Divisions
2.3 Data Division
Part of a COBOL program where every data item processedby the program is described
Unless a data item is specified in the Data Division, it cannotbe used in the Procedure Division
Divided into a number of sections ; a data item is defined in anappropriate section depending on its usage
Chapter 2 COBOL Divisions
-
7/30/2019 COBOL_CDC[1]
35/313
CDC_Covansys_ChennaiCOBOL _ # 35 _Ver 2.0
Chapter - 2 COBOL Divisions
2.3 Data Division
File Section
Description of all data items that should be read from or written onto, some external file
FD ( File Description ) entry for each file in the program followed bythe record description
Record description begins with a record name
( Always at 01 level )
Chapter 2 COBOL Divisions
-
7/30/2019 COBOL_CDC[1]
36/313
CDC_Covansys_ChennaiCOBOL _ # 36 _Ver 2.0
Chapter - 2 COBOL Divisions
Data Division
Eg. The relationships among
SELECT, FD, 01 and FIELDS
SELECT names the file and ASSIGN
assigns it to a hardware deviceFD describes the file
01 names the record format
02-49 describes the fields
within the record format
Chapter 2 COBOL Divisions
-
7/30/2019 COBOL_CDC[1]
37/313
CDC_Covansys_ChennaiCOBOL _ # 37 _Ver 2.0
Chapter - 2 COBOL Divisions
Working-Storage Section
Descriptions of all data items whose values are developedinternally as intermediate results, as well as, the constants
Succeeds the File Section
It is a common practice to group related items together
Chapter 2 COBOL Divisions
-
7/30/2019 COBOL_CDC[1]
38/313
CDC_Covansys_ChennaiCOBOL _ # 38 _Ver 2.0
Chapter - 2 COBOL Divisions
Working-Storage Section
Eg.
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).
Chapter 2 COBOL Divisions
-
7/30/2019 COBOL_CDC[1]
39/313
CDC_Covansys_ChennaiCOBOL _ # 39 _Ver 2.0
Chapter - 2 COBOL Divisions
Linkage Section
Appears in a called program
Describes records or non-contiguous data items with respect to
which connections should be established with the calling program
Corresponding data items in the calling program may be defined
in any section of the Data Division
Chapter 2 COBOL Divisions
-
7/30/2019 COBOL_CDC[1]
40/313
CDC_Covansys_ChennaiCOBOL _ # 40 _Ver 2.0
Chapter - 2 COBOL Divisions
Linkage Section
Eg.
DATA DIVISION.
...........
...........LINKAGE SECTION.
01 LS-ALL-VARS.
05 LS-RECD1 PIC 999.
05 LS-RECD2 PIC 999.
05 LS-RECD3 PIC 9(4).
Chapter 2 COBOL Divisions
-
7/30/2019 COBOL_CDC[1]
41/313
CDC_Covansys_ChennaiCOBOL _ # 41 _Ver 2.0
Chapter - 2 COBOL Divisions
2.4 Procedure Division
Contains statements which specify the operations to beperformed by the computer
Each of these statements is formed with COBOL words andliterals
A statement always starts with a COBOL verb
Chapter - 2 COBOL Divisions
-
7/30/2019 COBOL_CDC[1]
42/313
CDC_Covansys_ChennaiCOBOL _ # 42 _Ver 2.0
Chapter - 2 COBOL Divisions
Eg.
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.
STOP RUN.
0001-MAIN-EXIT-PARA.
EXIT.
Chapter 2 COBOL Divisions
-
7/30/2019 COBOL_CDC[1]
43/313
CDC_Covansys_ChennaiCOBOL _ # 43 _Ver 2.0
Chapter - 2 COBOL Divisions
2.5 Sample Program - 01
IDENTIFICATION DIVISION.
PROGRAM-ID. EXAMPLE-01.
AUTHOR. CVNS(I).
DATE-WRITTEN. 01-01-1999.DATE-COMPILED. 01-01-1999.
ENVIRONMENT DIVISION.
CONFIGURATION SECTION.
SOURCE-COMPUTER. CVNS(I)-PC-nn.
OBJECT-COMPUTER. CVNS(I)-PC-nn.
Chapter - 2 COBOL Divisions
-
7/30/2019 COBOL_CDC[1]
44/313
CDC_Covansys_ChennaiCOBOL _ # 44 _Ver 2.0
Chapter - 2 COBOL Divisions
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 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.
STOP RUN.
0001-MAIN-EXIT-PARA.
EXIT.
Chapter - 2 COBOL Divisions
-
7/30/2019 COBOL_CDC[1]
45/313
CDC_Covansys_ChennaiCOBOL _ # 45 _Ver 2.0
Chapter - 2 COBOL Divisions
2.6 Sample Program - 02
IDENTIFICATION DIVISION.PROGRAM-ID. EXAMPLE-02.
AUTHOR. CVNS(I).
DATE-WRITTEN. 01-01-1999.
DATE-COMPILED. 01-01-1999.
ENVIRONMENT DIVISION.
CONFIGURATION SECTION.
SOURCE-COMPUTER. CVNS(I)-PC-nn.
OBJECT-COMPUTER. CVNS(I)-PC-nn.INPUT-OUTPUT SECTION.
FILE-CONTROL.
SELECT STOK-FILE ASSIGN TO DISK.
Chapter - 2 COBOL Divisions
-
7/30/2019 COBOL_CDC[1]
46/313
CDC_Covansys_ChennaiCOBOL _ # 46 _Ver 2.0
Chapter - 2 COBOL Divisions
DATA DIVISION.
FILE SECTION.
FD STOK-FILE
LABEL RECORDS ARE STANDARD
VALUE OF FILE-ID IS STOK.DAT.
01 STOK-ITEM.
02 STOCK-CODE PIC X(4).
02 PROD-DESC PIC X(24).
02 UNIT-SIZE PIC 9(4).
Chapter - 2 COBOL Divisions
-
7/30/2019 COBOL_CDC[1]
47/313
CDC_Covansys_ChennaiCOBOL _ # 47 _Ver 2.0
Chapter - 2 COBOL Divisions
PROCEDURE DIVISION.
0001-FST-PARA.
DISPLAY ERASE.OPEN INPUT STOK-FILE.
DISPLAY FILE OPENED .
0001-FST-EXIT-PARA.
EXIT.0001-SEC-PARA.
CLOSE STOK-FILE.
DISPLAY END OF PROGRAM.
STOP RUN.0001-SEC-EXIT-PARA.
EXIT.
Chapter - 3 - Picture Clause
-
7/30/2019 COBOL_CDC[1]
48/313
CDC_Covansys_ChennaiCOBOL _ # 48 _Ver 2.0
Chapter - 3 - Picture Clause
Introduction
Specified for every elementary data item
Eg.
Data Item Type Symbols for Character String
Numeric 9 S V ( )
Alphabetic A ( )
Alphanumeric 9 X A ( )
Chapter - 3 - Picture Clause
-
7/30/2019 COBOL_CDC[1]
49/313
CDC_Covansys_ChennaiCOBOL _ # 49 _Ver 2.0
Chapter - 3 - Picture Clause
Syntax
{ PICTURE } IS character-string
{ PIC }
Code Character Meaning
9 Data item contains a numeral
X Data item contains any allowable
character from the COBOL character set
A Data item contains only a letter or space
V Data item contains an assumed decimal point
S Data item is signed
( specified as the left most character )
Chapter - 3 - Picture Clause
-
7/30/2019 COBOL_CDC[1]
50/313
CDC_Covansys_ChennaiCOBOL _ # 50 _Ver 2.0
Chapter 3 Picture Clause
Note_01
Code Character Nature of the Stored Value
A, X is Left.......... Justified
9 is ..........Right Justified
S is either Positive or Negative
Chapter - 3 - Picture Clause
-
7/30/2019 COBOL_CDC[1]
51/313
CDC_Covansys_ChennaiCOBOL _ # 51 _Ver 2.0
Chapter 3 Picture Clause
Internal Representation
Picture Data Representation
AAAA ABCD ABCD
A(04) XYZ XYZb
XXXX ?e/X ?e/X
X(05) 123 123bb
9(03) 123 123
Chapter - 3 - Picture Clause
-
7/30/2019 COBOL_CDC[1]
52/313
CDC_Covansys_ChennaiCOBOL _ # 52 _Ver 2.0
Chapter 3 Picture Clause
Picture Data Representation
9(04) 123 0123
99V99 38.50 38 50
9(4)V9(2) 34.5 0034 50
99V9 123.45 23 4
S9(4)V99 123.45 0123 45
S9(3)V99 -12.3 012 30
Chapter - 3 - Picture Clause
-
7/30/2019 COBOL_CDC[1]
53/313
CDC_Covansys_ChennaiCOBOL _ # 53 _Ver 2.0
Chapter 3 Picture Clause
3.3 Editing Symbols
Need for editing data
Edit Types Numeric data
Z Zero Suppression
* Asterisk
$ Currency sign
- Minus sign
+ Plus sign
CR DB Credit Debit Sign
Chapter - 3 - Picture Clause
-
7/30/2019 COBOL_CDC[1]
54/313
CDC_Covansys_ChennaiCOBOL _ # 54 _Ver 2.0
Chapter 3 Picture Clause
Edit Types Numeric data
. Period
, Comma
B Z / Blank, Zero, Slash Insertion
BLANK WHEN ZERO b
$ + - Floating Insertion
Edit Types Alphabetic
Blank Insertion
Edit Types Alphanumeric
Blank, Zero, Slash Insertion
Chapter - 3 - Picture Clause
-
7/30/2019 COBOL_CDC[1]
55/313
CDC_Covansys_ChennaiCOBOL _ # 55 _Ver 2.0
Chapter 3 Picture Clause Mutually Exclusive Symbols
V . + - + - CR DB S $ + - Z *
3.4 Zero Suppression Z
Its a zero suppression character that replaces each non-significant zero with a blank space
In the process, the commas that are embedded within the non-significant zeroes are also suppressed (replaced with blank
spaces)
To be used only with numerals
Chapter - 3 - Picture Clause
-
7/30/2019 COBOL_CDC[1]
56/313
CDC_Covansys_ChennaiCOBOL _ # 56 _Ver 2.0
Chapter 3 Picture Clause
Eg.
Pic of the Field Numeric Value Edited Value
ZZ999 04678 b4678
ZZ999 00052 bb052
ZZ999 1.68 bb001
ZZZV99 38 4 b3840
ZZZV99 0 65 bbb65
ZZZZVZZ 0 05 bbbb05
ZZZZVZZ 0 bbbbbb
Chapter - 3 - Picture Clause
-
7/30/2019 COBOL_CDC[1]
57/313
CDC_Covansys_ChennaiCOBOL _ # 57 _Ver 2.0
Chapter 3 Picture Clause
3.5 Asterisk *
Acts as a check protection symbol
Its a zero suppression character that replaces each non-significant zero with an asterisk (*)
In the process, the commas that are embedded within the non-significant zeroes are also replaced with asterisks (*)
To be used only with numerals
Eg. Pic of the Field Numeric Value Edited Value
**999 04678 *4678 **999 00052 **052
**999 1.68 **001
Chapter - 3 - Picture Clause
-
7/30/2019 COBOL_CDC[1]
58/313
CDC_Covansys_ChennaiCOBOL _ # 58 _Ver 2.0
Chapter 3 Picture Clause
3.6 Currency Sign $
Its a zero suppression character that replaces each non-significant zero with a dollar ($)
In the process, the commas that are embedded within the non-significant zeroes are also replaced with dollars ($)
To be used only with numerals
Eg. Pic of the Field Numeric Value Edited Value
$99999 985 $00985
$99999 32264 $32264
$ZZ999 985 $bb985
$ZZ999 32264 $32264
$**999 985 $**985
Chapter - 3 - Picture Clause
-
7/30/2019 COBOL_CDC[1]
59/313
CDC_Covansys_ChennaiCOBOL _ # 59 _Ver 2.0
C apte 3 ctu e C ause
3.7 Minus Sign -
A minus sign can appear either at the leftmost or therightmost position of a picture
If the value of a data item is negative, a minus sign willappear in the said position
If the value of the data item is positive, a space character willappear instead
Eg Pic of the Field Numeric Value Edited Value
-9999 -382 -0382
-9999 382 b0382
9999- -382 0382-
9999- 382 0382b
-ZZZV99 -46 2 -b4652
-ZZZV99 46 52 bb4652
Chapter - 3 - Picture Clause
-
7/30/2019 COBOL_CDC[1]
60/313
CDC_Covansys_ChennaiCOBOL _ # 60 _Ver 2.0
p
3.8 Plus Sign +
A plus sign, like the minus sign, can appear either at theleftmost or the rightmost position of a picture
If the value of a data item is positive, a plus sign will appear inthe said position
If the value of the data item is negative, a minus sign will appearinstead
Eg. Pic of the Field Numeric Value Edited Value +9999 -382 -0382
+9999 382 +0382
9999+ -382 0382- 9999+ 382 0382+
+ZZZV99 -46 52 -b4652
+ZZZV99 46 52 +b4652
Chapter - 3 - Picture Clause
-
7/30/2019 COBOL_CDC[1]
61/313
CDC_Covansys_ChennaiCOBOL _ # 61 _Ver 2.0
p
3.9 Credit Debit Sign CR DB
The two symbols CR and DB can appear only at the rightmost position of the picture
They are identical to the minus sign edit character
The symbol CR or DB will appear at the right most position ifthe item is negative, otherwise it will be replaced by two spacecharacters
Eg. Pic of the Field Numeric Value Edited Value 9999CR -4625 4625CR
9999CR 4625 4625bb ZZZCR -42 b42CR
ZZ9V99DB -152 25 15225DB
ZZZ9V99DB 52 25 bb5225bb
Chapter - 3 - Picture Clause
-
7/30/2019 COBOL_CDC[1]
62/313
CDC_Covansys_ChennaiCOBOL _ # 62 _Ver 2.0
p
3.10 Period . A period may be used to insert a decimal point and may not appear
more than once A period must not appear as the rightmost character position in thepicture
If Zero Suppression is also specified, the period stops zerosuppression on its right
All the digits before and after the period happen to be zeros, the
period does not stop zero suppression and the entire field includingthe period is space-filled
Eg. Pic of the Field Numeric Value Edited Value 999.99 324.52 324.52
ZZ99.99 45.25 bb45.25
ZZ99.99 245 b245.00 ZZZZ.ZZ 0.05 bbbb.05
ZZZZ.ZZ 0 bbbbbbb
****.** 0 ****.**
$ZZZ9.99 0285 $b285.00
Chapter - 3 - Picture Clause
-
7/30/2019 COBOL_CDC[1]
63/313
CDC_Covansys_ChennaiCOBOL _ # 63 _Ver 2.0
p
3.11 Comma Insertion ,
Dollar signs & Commas are editing symbols frequently used inconjunction with suppression of leading zeroes and printing ofdecimal points
Eg. Pic of the Field Numeric Value Edited Value 99,999 2456 02,456
99,999 37 00,037
ZZ,Z99 2456 b2,456
**,*** 12 ****12
*,***,** 246 ****2,46
*,***,** 123456 1,234,56
ZZ,Z9.99 123.45 b1,23.45
Chapter - 3 - Picture Clause
-
7/30/2019 COBOL_CDC[1]
64/313
CDC_Covansys_ChennaiCOBOL _ # 64 _Ver 2.0
p
3.12 Blank Insertion B b
Eg. Pic of the Field Numeric Value Edited Value
99B99B99 150182 15b01b82
99B99B99 46 00b00b46
3.13 Zero Insertion 0 0
Eg. Pic of the Field Numeric Value Edited Value 9900 12 1200
09990 456 04560
Chapter - 3 - Picture Clause
-
7/30/2019 COBOL_CDC[1]
65/313
CDC_Covansys_ChennaiCOBOL _ # 65 _Ver 2.0
p
3.14 Slash Insertion / /
Eg. Pic of the Field Numeric Value Edited Value
99/99/99 150681 15/06/81
999/999/99 3254 000/032/54
3.15 Blank When Zero b
Eg. Pic of the Field Numeric Value Edited Value
ZZZ.99 BLANK WHEN ZERO 2.5 bb2.50
999.99 BLANK WHEN ZERO 0 bbbbbb
Chapter - 3 - Picture Clause
-
7/30/2019 COBOL_CDC[1]
66/313
CDC_Covansys_ChennaiCOBOL _ # 66 _Ver 2.0
p
3.16 Floating Insertion
The occurrence, in multiples, of an edit symbol
The Currency symbol ($), the Minus (-) and the Plus (+)signs can appear in multiples, on the the left-hand side of aPICture
In case of the minus character (-), no sign will be insertedunless the data is negative
Eg. Pic of the Field Numeric Value Edited Value $$$$9.99 235.25 b$235.25
$$$$9.99 342 b$342.00
++++.99 -475.25 -475.25
++++.99 45 b+45.00 ----.99 -3.5 bb-3.50
----.99 3.5 bbb3.50
----.99 -382 -382.00
Chapter - 4 COBOL Verbs
-
7/30/2019 COBOL_CDC[1]
67/313
CDC_Covansys_ChennaiCOBOL _ # 67 _Ver 2.0
p
4.1 Data-Oriented I/O
DISPLAY
Causes the literal(s) ( any literal or an appropriate figurativeconstant ) or the contents of the specified data name(s) to bedisplayed on an output device, either a screen or a printer
Syntax
DISPLAY [ (row, col) ] {identifier-1} {,identifier-2}
{literal-1 } {,literal-2 }
Eg.DISPLAY (05,10) ENTER NAME. ENTER NAME
is displayed on the 5th row, at the 10th column
Chapter - 4 COBOL Verbs
-
7/30/2019 COBOL_CDC[1]
68/313
CDC_Covansys_ChennaiCOBOL _ # 68 _Ver 2.0
DISPLAY SPACE
Clears the screen at the current cursor position ( the whole of thecurrent line alone )
Chapter - 4 COBOL Verbs
-
7/30/2019 COBOL_CDC[1]
69/313
CDC_Covansys_ChennaiCOBOL _ # 69 _Ver 2.0
ACCEPT
Reads data from the console or gets data from the COBOL
predefined fields ( DATE, DAY, TIME ), and places the same in
the identifier specified
Note_01
The predefined fields DATE, DAY, TIME are not defined by the
programmer but are made available by the compiler i.e, they get
their values at the time of execution from the underlying operatingsystem
Chapter - 4 COBOL Verbs
-
7/30/2019 COBOL_CDC[1]
70/313
CDC_Covansys_ChennaiCOBOL _ # 70 _Ver 2.0
ACCEPT
Syntax - 1
ACCEPT [ (row, col) ] {identifier-1}
Eg.
ACCEPT (05,15) WS-NAME.
The value entered for WS-NAME is accepted on the 5th row, atthe 15th column
Data input via ACCEPT is treated as if it were alphanumeric withrespect to positioning when used with (row, col)
Chapter - 4 COBOL Verbs
-
7/30/2019 COBOL_CDC[1]
71/313
CDC_Covansys_ChennaiCOBOL _ # 71 _Ver 2.0
ACCEPT
Syntax - 2
{ DATE }
ACCEPT identifier [ FROM { DAY } ]
{ TIME }
The above syntax of ACCEPT is used to move the contents of
the COBOL predefined fields DATE, DAY and TIME to a
specified identifier
Chapter - 4 COBOL Verbs
-
7/30/2019 COBOL_CDC[1]
72/313
CDC_Covansys_ChennaiCOBOL _ # 72 _Ver 2.0
ACCEPT
The DATE field contains the year, month and day
DATE PIC 999999
Eg.
Assume an identifier WS-TODAY with the following PICture :
01 WS-TODAY.
05 WS-T-YEAR PIC 99.
05 WS-T-MONTH PIC 99.
05 WS-T-DAY PIC 99.
Chapter - 4 COBOL Verbs
-
7/30/2019 COBOL_CDC[1]
73/313
CDC_Covansys_ChennaiCOBOL _ # 73 _Ver 2.0
ACCEPT
ACCEPT WS-TODAY FROM DATE.
issued on June 1, 1962, will cause the content of DATE,620601
to be moved to WS-TODAY
The values of the year, month and day are stored in
WS-T-YEAR, WS-T-MONTH and WS-T-DAY Viz.,
The DAY field contains the year and the day of the year, i.e, the
Julian date
DAY PIC 99999
Chapter - 4 COBOL Verbs
-
7/30/2019 COBOL_CDC[1]
74/313
CDC_Covansys_ChennaiCOBOL _ # 74 _Ver 2.0
ACCEPT
Eg.
Assume an identifier WS-DAY with the following PICture :
01 WS-DAY.
05 WS-T-YEAR PIC 99.
05 WS-T-DAY PIC 999.
ACCEPT WS-DAY FROM DAY.
issued on July 1, 1979, will be stored as 79183
Chapter - 4 COBOL Verbs
-
7/30/2019 COBOL_CDC[1]
75/313
CDC_Covansys_ChennaiCOBOL _ # 75 _Ver 2.0
ACCEPT
TIME contains hours, minutes, seconds, and hundredths of asecond based on elapsed time after midnight on a 24 hour basis
TIME PIC 99999999
Eg. 8:30 P.M. is stored as 20300000
ACCEPT cannot reference a series of identifiers or literals unlikeDISPLAY i.e.,
ACCEPT WS-NUM1,WS-NUM2.
is invalid
Chapter - 4 COBOL Verbs
-
7/30/2019 COBOL_CDC[1]
76/313
CDC_Covansys_ChennaiCOBOL _ # 76 _Ver 2.0
4.2 Data Movement
MOVE
Copies the contents of one data item ( the sending field ) to an
other 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
Chapter - 4 COBOL Verbs
-
7/30/2019 COBOL_CDC[1]
77/313
CDC_Covansys_ChennaiCOBOL _ # 77 _Ver 2.0
MOVE
Moving an integer
movement is from right to left non-filled higher-order
( leftmost ) integers are replaced by 0s
Moving a decimal
movement is from left to right non-filled lower-order
( rightmost ) integers are replaced by 0s
Moving a non-numeric or alphanumeric field
movement is from left to right non-filled lower-order
( rightmost ) integers are replaced by spaces
Chapter - 4 COBOL Verbs
-
7/30/2019 COBOL_CDC[1]
78/313
CDC_Covansys_ChennaiCOBOL _ # 78 _Ver 2.0
MOVE
Eg.MOVE AMT-IN TO AMT-OUT
AMT-IN AMT-OUT
PIC 999 9(4)
VALUE 123 0123
PIC 999V99 9(4)V999
VALUE 123 45 0123 450
PIC XXX X(4)
VALUE ABC ABCb
Chapter - 4 COBOL Verbs
-
7/30/2019 COBOL_CDC[1]
79/313
CDC_Covansys_Chennai
COBOL _ # 79 _Ver 2.0
MOVE
Move Corresponding
To move the contents of some or all those data items, of one group,to the corresponding identically named data items, of another group
Syntax
MOVE { CORRESPONDING } identifier-1 TO identifier-2
{ CORR }
Note_01 identifier-1 and identifier-2 should be group itemsat the same level with correspondingly relatedPICture description need not be of the same lengthand structure
Chapter - 4 COBOL Verbs
-
7/30/2019 COBOL_CDC[1]
80/313
CDC_Covansys_Chennai
COBOL _ # 80 _Ver 2.0
MOVE
Eg.
01 WS-DATE-SYSTEM.
05 WS-YY PIC 9(2).
05 WS-MM PIC 9(2).
05 WS-DD PIC 9(2).
01 WS-DATE-PRINT.
05 WS-DD PIC Z9.
05 FILLER PIC X VALUE /.
05 WS-MM PIC Z9.
05 FILLER PIC X VALUE /.
05 WS-YY PIC 99.
MOVE CORR WS-DATE-SYSTEM TO WS-DATE-PRINT.
is equivalent to the following three MOVE statements
MOVE WS-DD OF WS-DATE-SYSTEM TO WS-DD OF WS-DATE-PRINT.
MOVE WS-MM OF WS-DATE-SYSTEM TO WS-MM OF WS-DATE-PRINT.
MOVE WS-YY OF WS-DATE-SYSTEM TO WS-YY OF WS-DATE-PRINT.
Chapter - 4 COBOL Verbs
-
7/30/2019 COBOL_CDC[1]
81/313
CDC_Covansys_Chennai
COBOL _ # 81 _Ver 2.0
4.3 Arithmetic Verbs
ADD
Elementary numeric data items alone can be added
Numeric literals alone can be added
Decimal point alignment is automatic
The target field's PICture is the deciding factor
( Internal format transformation )
Chapter - 4 COBOL Verbs
-
7/30/2019 COBOL_CDC[1]
82/313
CDC_Covansys_Chennai
COBOL _ # 82 _Ver 2.0
ADD
Syntax 1 : ADD ... TO
ADD {identifier-1} {identifier-2}
{literal-1 } {literal-2 } ...
TO
identifier-m [ ROUNDED ]
[ identifier-n [ ROUNDED ]] ...
[ON SIZE ERROR imperative-stmt.]
Chapter - 4 COBOL Verbs
-
7/30/2019 COBOL_CDC[1]
83/313
CDC_Covansys_Chennai
COBOL _ # 83 _Ver 2.0
ADD
Syntax 2: ADD ... TO ...GIVING
ADD {identifier-1} {identifier-2} {identifier-3}
{literal-1 } {literal-2 } {literal-3 } ...
[ TO identifier-m [ ROUNDED ] ]
GIVING identifier-m [ ROUNDED ]
[identifier-n [ ROUNDED ] ] ...
[ON SIZE ERROR imperative-stmt.]
Chapter - 4 COBOL Verbs
-
7/30/2019 COBOL_CDC[1]
84/313
CDC_Covansys_Chennai
COBOL _ # 84 _Ver 2.0
ADD
Eg.
Values After Execution
Arithmetic Statement A B C D
ADD A TO B A B+A C D
ADD A B C TO D A B C D+A+B+C
ADD A TO B C A B+A C+A D
ADD A B C A B C A+B+C
GIVING D
Chapter - 4 COBOL Verbs
-
7/30/2019 COBOL_CDC[1]
85/313
CDC_Covansys_Chennai
COBOL _ # 85 _Ver 2.0
ADD Eg.
Values After Execution
Arithmetic Statement A B C D
ADD A 15 A B A+15 A+15
GIVING C D
ADD A TO B A B B+A D
GIVING C
ADD A B TO C A B C A+B+C
GIVING D
ADD A 15 TO B A B A+B+15 A+B+15
GIVING C D
Chapter - 4 COBOL Verbs
-
7/30/2019 COBOL_CDC[1]
86/313
CDC_Covansys_Chennai
COBOL _ # 86 _Ver 2.0
ADD
Note_01
After the TO in ADD..TO..GIVING, not more than one dataname is permitted
ADD A TO B C GIVING D
is invalid
Chapter - 4 COBOL Verbs
-
7/30/2019 COBOL_CDC[1]
87/313
CDC_Covansys_Chennai
COBOL _ # 87 _Ver 2.0
ADD
ADD ... CORRESPONDING
To add the contents of some or all those data items, of one group, tothe corresponding identically named data items, of an other group
Syntax
ADD { CORRESPONDING }
{ CORR } identifier-1
TO identifier-2 [ ROUNDED ]
[ON SIZE ERROR imperative-stmt.]
Note_01 identifier-1, identifier-2 to be group items
Chapter - 4 COBOL Verbs
-
7/30/2019 COBOL_CDC[1]
88/313
CDC_Covansys_Chennai
COBOL _ # 88 _Ver 2.0
ADD Eg05 WS-X.
10 WS-A PIC 9(3).10 WS-B PIC 9(4).10 WS-C PIC 9(5).
05 WS-Y.10 WS-C PIC 9(5).10 WS-A PIC 9(3).10 WS-B PIC 9(4).
ADD CORR WS-X TO WS-Y.
INITIAL FINALWS-XA 2 2B 4 4
C 6 6WS-Y
A 1 3B 3 7C 5 11
Chapter - 4 COBOL Verbs
-
7/30/2019 COBOL_CDC[1]
89/313
CDC_Covansys_Chennai
COBOL _ # 89 _Ver 2.0
SUBTRACT
Elementary numeric data items alone can be subtracted
Numeric literals alone can be subtracted
Decimal point alignment is automatic
The target field's PICture is the deciding factor
( Internal format transformation )
Chapter - 4 COBOL Verbs
-
7/30/2019 COBOL_CDC[1]
90/313
CDC_Covansys_Chennai
COBOL _ # 90 _Ver 2.0
SUBTRACT
Syntax 1 : SUBTRACT ... FROM
SUBTRACT { identifier-1}
{ literal-1}
FROM { identifier-m [ ROUNDED ] }
[ identifier-n [ ROUNDED ] ] ...
[ ON SIZE ERROR imperative-stmt.]
Chapter - 4 COBOL Verbs
-
7/30/2019 COBOL_CDC[1]
91/313
CDC_Covansys_Chennai
COBOL _ # 91 _Ver 2.0
SUBTRACT
Syntax 2: SUBTRACT ... FROM ... GIVING
SUBTRACT {identifier-1} {,identifier-2}
{literal-1 } {,literal-2 } ...
FROM {identifier-m} {literal-m }
GIVING {identifier-n [ ROUNDED ] }
[ ,identifier-o [ ROUNDED ] ] ...[ ON SIZE ERROR imperative-stmt.]
Chapter - 4 COBOL Verbs
-
7/30/2019 COBOL_CDC[1]
92/313
CDC_Covansys_Chennai
COBOL _ # 92 _Ver 2.0
SUBTRACT
Eg.Values After Execution
Arithmetic Statement A B C D
SUBTRACT A A B-A C D
FROM B
SUBTRACT 15 A-15 B-15 C D
FROM A B
SUBTRACT A B A B C-(A+B) D
FROM C
Chapter - 4 COBOL Verbs
-
7/30/2019 COBOL_CDC[1]
93/313
CDC_Covansys_Chennai
COBOL _ # 93 _Ver 2.0
SUBTRACT
Eg.
Values After Execution
Arithmetic Statement A B C D
SUBTRACT A 15 A B C-(A+15) D-(A+15)
FROM C D
SUBTRACT A A B B-A D
FROM B GIVING C
SUBTRACT A B A B C C-(A+B)
FROM C GIVING D
SUBTRACT
Chapter - 4 COBOL Verbs
-
7/30/2019 COBOL_CDC[1]
94/313
CDC_Covansys_Chennai
COBOL _ # 94 _Ver 2.0
SUBTRACT
Eg.
Values After Execution
Arithmetic Statement A B C D
SUBTRACT A A B B-A B-A
FROM B GIVING C D
SUBTRACT A 15 A B B-(A+15) B-(A+15)FROM B GIVING C D
Note_01
After the FROM in SUBTRACT..FROM..GIVING,
not more than one data name is permitted
SUBTRACT A FROM B C GIVING D is invalid
Chapter - 4 COBOL Verbs
-
7/30/2019 COBOL_CDC[1]
95/313
CDC_Covansys_Chennai
COBOL _ # 95 _Ver 2.0
SUBTRACT
SUBTRACT ... CORRESPONDING
Syntax
SUBTRACT { CORRESPONDING } identifier-1
{ CORR }
FROM identifier-2 [ ROUNDED ]
[ ON SIZE ERROR imperative-stmt.]
Note_01
identifier-1, identifier-2 to be group items
S C
Chapter - 4 COBOL Verbs
-
7/30/2019 COBOL_CDC[1]
96/313
CDC_Covansys_Chennai
COBOL _ # 96 _Ver 2.0
SUBTRACT To substract the contents of some or all those data items, of one group,
from the corresponding identically named data items, of an other group
Eg.05 WS-X.
10 WS-A PIC 9(3).
10 WS-B PIC 9(4).
10 WS-C PIC 9(5).
SUBTRACT CORR WS-X FROM WS-Y.INITIAL FINAL
WS-X
A 2 2
B 4 4
C 6 6
WS-YA 1 -1
B 3 -1
C 5 -1
05 WS-Y.
10 WS-C PIC 9(5).
10 WS-A PIC 9(3).
10 WS-B PIC 9(4).
Chapter - 4 COBOL Verbs
-
7/30/2019 COBOL_CDC[1]
97/313
CDC_Covansys_ChennaiCOBOL _ # 97 _Ver 2.0
MULTIPLY
Elementary numeric data items alone can be multiplied
Numeric literals alone can be multiplied
Decimal point alignment is automatic
The target field's PICture is the deciding factor
( Internal format transformation )
Chapter - 4 COBOL Verbs
-
7/30/2019 COBOL_CDC[1]
98/313
CDC_Covansys_ChennaiCOBOL _ # 98 _Ver 2.0
MULTIPLY
Syntax 1 : MULTIPLY ... BY
MULTIPLY {identifier-1} BY identifier-2 [ROUNDED ]
{literal-1 }
[ identifier-3 [ROUNDED ]]
...
[ ON SIZE ERROR imperative-stmt.]
Chapter - 4 COBOL Verbs
-
7/30/2019 COBOL_CDC[1]
99/313
CDC_Covansys_ChennaiCOBOL _ # 99 _Ver 2.0
MULTIPLY
Syntax 2: MULTIPLY ... BY ... GIVING
MULTIPLY {identifier-1} BY {identifier-2}
{literal-1 } {literal-2 }
GIVING identifier-3 [ ROUNDED ]
[ identifier-4 [ ROUNDED ]]...
[ON SIZE ERROR imperative-stmt.]
MULTIPLY
Chapter - 4 COBOL Verbs
-
7/30/2019 COBOL_CDC[1]
100/313
CDC_Covansys_ChennaiCOBOL _ # 100 _Ver 2.0
MULTIPLY
Eg.
Values After ExecutionArithmetic Statement A B C D
MULTIPLY A BY B A A*B C D
MULTIPLY A BY B C A B*A C*A D
MULTIPLY A BY B A B A*B D
GIVING C
MULTIPLY A BY 2 A A*2 A*2 D
GIVING B C
Note_01
After the BY in MULTIPLY..BY..GIVING,not more than one data name ispermitted
MULTIPLY A BY B C GIVING D is invalid
DIVIDE
Chapter - 4 COBOL Verbs
-
7/30/2019 COBOL_CDC[1]
101/313
CDC_Covansys_ChennaiCOBOL _ # 101 _Ver 2.0
DIVIDE
Syntax 1 : DIVIDE ... INTO
DIVIDE {identifier-1} INTO identifier-2 [ ROUNDED ]
{literal-1 }
[ identifier-3 [ ROUNDED ] ]...
[ ON SIZE ERROR imperative-stmt. ]
DIVIDE
Chapter - 4 COBOL Verbs
-
7/30/2019 COBOL_CDC[1]
102/313
CDC_Covansys_ChennaiCOBOL _ # 102 _Ver 2.0
DIVIDE
Syntax 2: DIVIDE ... BY ... GIVING
DIVIDE {identifier-1} BY {identifier-2}
{literal-1 } {literal-2 }
GIVING identifier-3 [ ROUNDED ]
[ identifier-4 [ ROUNDED ]]...
[ON SIZE ERROR imperative-stmt. ]
DIVIDE
Chapter - 4 COBOL Verbs
-
7/30/2019 COBOL_CDC[1]
103/313
CDC_Covansys_ChennaiCOBOL _ # 103 _Ver 2.0
DIVIDE
Syntax 3: DIVIDE ... BY/INTO
... GIVING
... REMAINDER
DIVIDE {identifier-1} { BY } {identifier-2}
{literal-1} {INTO} {literal-2}
GIVING identifier-3 [ ROUNDED ]
REMAINDER identifier-4
[ ON SIZE ERROR imperative-stmt. ]
DIVIDE
Chapter - 4 COBOL Verbs
-
7/30/2019 COBOL_CDC[1]
104/313
CDC_Covansys_ChennaiCOBOL _ # 104 _Ver 2.0
DIVIDE
Eg.
Values After Execution
Arithmetic Statement A B C D
DIVIDE A INTO B C A B/A C/A D
DIVIDE A INTO B A B B/A D
GIVING C
DIVIDE A INTO B A B B/A B/A
GIVING C D
DIVIDE A INTO B A B Integer : Integer :
GIVING C Quotient Remainder
REMAINDER D of B/A of B/A
DIVIDE
Chapter - 4 COBOL Verbs
-
7/30/2019 COBOL_CDC[1]
105/313
CDC_Covansys_ChennaiCOBOL _ # 105 _Ver 2.0
DIVIDE
Eg.
Values After Execution
Arithmetic Statement A B C D
DIVIDE A BY B A B A/B A/B
GIVING C D
DIVIDE A BY B A B Integer : Integer :GIVING C QuotientRemainder
REMAINDER D of A/B of A/B
Note_01 After the BY/INTO in the
DIVIDE..BY/INTO..GIVING..REMAINDER, not more than one data nameis permitted
DIVIDE A BY B C GIVING D REMAINDER is invalid
COMPUTE
Chapter - 4 COBOL Verbs
-
7/30/2019 COBOL_CDC[1]
106/313
CDC_Covansys_ChennaiCOBOL _ # 106 _Ver 2.0
COMPUTE
Combination of more than one arithmetic operation in a singlestatement
Shortens programs, avoids intermediate data names
Related expressions are made up of
Operators ( evaluated by their precedences )
Operands - Data names
Literals
COMPUTE
Chapter - 4 COBOL Verbs
-
7/30/2019 COBOL_CDC[1]
107/313
CDC_Covansys_ChennaiCOBOL _ # 107 _Ver 2.0
COMPUTE
Syntax
COMPUTE identifier-1 [ ROUNDED ]
[identifier-2 [ ROUNDED ]]...
= Arithmetic-Expression
[ON SIZE ERROR imperative-stmt. ]
Hierarchy of execution (in terms of operators)
1. Parenthesis ( )
2. Unary - +
3. Exponentiation ** 4. Division / Multiplication *
5. Addition + Subtraction -
Within innermost parenthesis, scanning is from left to right
COMPUTE
Chapter - 4 COBOL Verbs
-
7/30/2019 COBOL_CDC[1]
108/313
CDC_Covansys_ChennaiCOBOL _ # 108 _Ver 2.0
COMPUTE
Eg.
COMPUTE PRICE = RATE * QTY.
COMPUTE F = 1.8 * C + 32.
COMPUTE A = 2 + ( 3 * 5 ).
Chapter 5 Structured Programming
5 1 Conditional Statements
-
7/30/2019 COBOL_CDC[1]
109/313
CDC_Covansys_ChennaiCOBOL _ # 109 _Ver 2.0
5.1 Conditional Statements
IF statement
Passes the control between alternate paths of Statement(s)depending upon the truth of value of a test condition
Syntax
IF Condition-1
Statement-1
[ ELSE
Statement-2
[ END-IF ]].
Types of Conditions
Chapter 5 Structured Programming
-
7/30/2019 COBOL_CDC[1]
110/313
CDC_Covansys_ChennaiCOBOL _ # 110 _Ver 2.0
Types of Conditions
Simple
Class condition
Relation condition
Sign condition
Complex Abbreviated Compound
Class condition
Numeric or Alphabetic check of the operand
Numeric test cannot be done for a data item declared as alphabetic( PIC A )
Alphabetic test cannot be done for a data item declared as numeric( PIC 9 )
IF statement
Chapter 5 Structured Programming
-
7/30/2019 COBOL_CDC[1]
111/313
CDC_Covansys_ChennaiCOBOL _ # 111 _Ver 2.0
IF statement
Syntax
IF identifier IS [ NOT ] { NUMERIC }
{ ALPHABETIC }
Numeric 0 thru 9 with Sign
Alphabetic A-Z, Space
-
7/30/2019 COBOL_CDC[1]
112/313
Sign condition
Chapter 5 Structured Programming
-
7/30/2019 COBOL_CDC[1]
113/313
CDC_Covansys_ChennaiCOBOL _ # 113 _Ver 2.0
Sign condition
Determines whether the algebraic value is less than, or greaterthan, or equal to, zero
{identifier-1 } {POSITIVE }
{arith.expr.-1} IS [NOT] {NEGATIVE}
{ZERO }
Nested IFs
Chapter 5 Structured Programming
-
7/30/2019 COBOL_CDC[1]
114/313
CDC_Covansys_ChennaiCOBOL _ # 114 _Ver 2.0
Nested IFs
Eg1. Conditions
MARKS = 80 & ABOVE GRADE A= 60 - 79 GRADE B= 50 - 59 GRADE C= BELOW 50 GRADE D
Eg2. Equivalent Nested IF Statements
IF MARKS > 79MOVE A TO GRADE
ELSEIF MARKS > 59
MOVE B TO GRADE
ELSEIF MARKS > 49
MOVE C TO GRADEELSE
MOVE D TO GRADE.
Complex conditions
Chapter 5 Structured Programming
-
7/30/2019 COBOL_CDC[1]
115/313
CDC_Covansys_ChennaiCOBOL _ # 115 _Ver 2.0
Co p e co d t o s
Formed by Combining simple conditions
Nested IFsLogical connectives
Logical AND Logical ConjunctionOperators OR Logical Inclusion
NOT Logical Negation
Used for Combining simple conditionsAbbreviating conditionsNegation
Evaluation 1. Arithmetic ExpressionOrder 2. All relational operators
2.1 NOT
2.2 AND2.3 OR
Complex conditions
Chapter 5 Structured Programming
-
7/30/2019 COBOL_CDC[1]
116/313
CDC_Covansys_ChennaiCOBOL _ # 116 _Ver 2.0
Complex conditions
Note_01
Excepting the NOT operator, no two operators should be placed
consecutively,
i.e, every operator should be embedded
The logical operators NOT and OR should not
come together
Abbreviated Compound conditions
Chapter 5 Structured Programming
-
7/30/2019 COBOL_CDC[1]
117/313
CDC_Covansys_ChennaiCOBOL _ # 117 _Ver 2.0
p
With implied-subjects and relational operators
Eg1.
IF A = B AND A = C is equivalent to
IF A = B AND = C
Identical subjects are omitted in a consecutive sequence of relationalconditions
Eg2.
IF A = B AND A = C is equivalent to
IF A = B AND C
Identical subjects and relational operators are omitted in
a consecutive sequence of relational conditions
EVALUATE
Chapter 5 Structured Programming
-
7/30/2019 COBOL_CDC[1]
118/313
CDC_Covansys_ChennaiCOBOL _ # 118 _Ver 2.0
Multiple conditions may be evaluated
Syntax - 1
EVALUATE expr.-1
WHEN condition-1imperative-stmt.-1
[ WHEN condition-2
imperative-stmt.-2 ]
............. .............
[ WHEN OTHER
imperative-stmt.-n ]
[ END-EVALUATE ].
EVALUATE
Chapter 5 Structured Programming
-
7/30/2019 COBOL_CDC[1]
119/313
CDC_Covansys_ChennaiCOBOL _ # 119 _Ver 2.0
EVALUATE
Eg1.
EVALUATE COMMUTER-DRIVING-STATUS
WHEN D
MOVE DRIVER TO P-DRIVING-STATUS
WHEN R
MOVE RIDER TO P-DRIVING-STATUS
WHEN SPACES
MOVE DR/RID TO P-DRIVING-STATUS
END-EVALUATE.
EVALUATE
Chapter 5 Structured Programming
-
7/30/2019 COBOL_CDC[1]
120/313
CDC_Covansys_ChennaiCOBOL _ # 120 _Ver 2.0
Syntax 2
EVALUATE expr.-1 [ ALSO expr.-2 ]
WHEN condition-1 [ ALSO condition-2 ]
imperative-stmt.-1
[ WHEN condition-3 [ ALSO condition-4 ]imperative-stmt.-2 ]
[ WHEN condition-5 [ ALSO condition-6 ]
imperative-stmt.-3 ]
............. .............
[ WHEN OTHER
imperative-stmt.-n ]
[ END-EVALUATE ].
EVALUATE
Chapter 5 Structured Programming
-
7/30/2019 COBOL_CDC[1]
121/313
CDC_Covansys_ChennaiCOBOL _ # 121 _Ver 2.0
Eg2.
PROCEDURE DIVISION.
0001-MAIN-PARA.
EVALUATE TRUE ALSO TRUE
WHEN WS-AGE < 13 ALSO WS-SEX = M
MOVE YOUNG BOY TO WS-DESC
WHEN WS-AGE < 13 ALSO WS-SEX = F
MOVE YOUNG GIRL TO WS-DESC
WHEN OTHER
MOVE NOT YOUNG TO WS-DESCEND-EVALUATE.
0001-MAIN-EXIT-PARA.
EXIT.
EVALUATE
Chapter 5 Structured Programming
-
7/30/2019 COBOL_CDC[1]
122/313
CDC_Covansys_ChennaiCOBOL _ # 122 _Ver 2.0
EVALUATE
Eg3.
IDENTIFICATION DIVISION.
...........
...........
ENVIRONMENT DIVISION.
...........
...........
DATA DIVISION.WORKING-STORAGE SECTION.
01 WS-A PIC 9(1).
EVALUATE
Chapter 5 Structured Programming
-
7/30/2019 COBOL_CDC[1]
123/313
CDC_Covansys_ChennaiCOBOL _ # 123 _Ver 2.0
Eg3. (contd.)PROCEDURE DIVISION.
0001-MAIN-PARA.ACCEPT WS-A.
EVALUATE WS-A
WHEN 1
WHEN 3
WHEN 5
WHEN 7WHEN 9
DISPLAY THE GIVEN VALUE IS ODD
WHEN 2
WHEN 4
WHEN 6
WHEN 8DISPLAY THE GIVEN VALUE IS EVEN
END-EVALUATE.
0001-MAIN-PARA-EXIT.
EXIT.
EVALUATE
Chapter 5 Structured Programming
-
7/30/2019 COBOL_CDC[1]
124/313
CDC_Covansys_ChennaiCOBOL _ # 124 _Ver 2.0
Eg4.IDENTIFICATION DIVISION.
...........
ENVIRONMENT DIVISION............
DATA DIVISION.
WORKING-STORAGE SECTION.
01 WS-PERCNT PIC 9(3).
PROCEDURE DIVISION.
0001-MAIN-PARA.
ACCEPT WS-PERCNT.
EVALUATE WS-PERCNT
WHEN 50 THRU 59
DISPLAY SECOND CLASS
WHEN 60 THRU 69
DISPLAY FIRST CLASS
WHEN 70 THRU 100DISPLAY DISTINCTION
END-EVALUATE.
0001-MAIN-PARA-EXIT.
EXIT.
5.2 Internal Program Control
Chapter 5 Structured Programming
-
7/30/2019 COBOL_CDC[1]
125/313
CDC_Covansys_ChennaiCOBOL _ # 125 _Ver 2.0
g
PERFORM
Executes group of consecutive statements written elsewhere in
the program
Control departs from the normal sequence of execution to the
specified set of statements, and after completion, returns
implicitly to the statement immediately succeeding the
PERFORM statement
PERFORM
Chapter 5 Structured Programming
-
7/30/2019 COBOL_CDC[1]
126/313
CDC_Covansys_ChennaiCOBOL _ # 126 _Ver 2.0
Syntax - 1
PERFORM procedure-name
Note_01procedure-name can either be a Para-name or a Section-name
Eg1.PERFORM PARA-A
Syntax 2: THROUGH option
PERFORM procedure-name-1
{THROUGH} procedure-name-2{THRU }
Eg2.PERFORM PARA-A THRU PARA-E
PERFORM
Chapter 5 Structured Programming
-
7/30/2019 COBOL_CDC[1]
127/313
CDC_Covansys_ChennaiCOBOL _ # 127 _Ver 2.0
Syntax
3
: TIMES option
PERFORM procedure-name-1
[ THRU procedure-name-2 ]
{identifier} TIMES
{integer }
The procedure is repeatedly performed a fixed number of timesspecified by the identifier / integer
Once the PERFORM statement is initiated, any reference to theidentifier will have no effect on the number of times theprocedure is to be executed
PERFORM
Chapter 5 Structured Programming
-
7/30/2019 COBOL_CDC[1]
128/313
CDC_Covansys_ChennaiCOBOL _ # 128 _Ver 2.0
Eg3.
PERFORM PARA-A THRU PARA-C 6 TIMES.PERFORM PARA-A THRU PARA-C WS-NUM TIMES.
Syntax 4: UNTIL option
PERFORM procedure-name-1[ THRU procedure-name-2 ]UNTIL condition
The procedure is performed repeatedly as long as the
condition is False
Eg4.PERFORM PARA-A THRU PARA-D UNTIL WS-ANS = Y
PERFORM
Chapter 5 Structured Programming
-
7/30/2019 COBOL_CDC[1]
129/313
CDC_Covansys_ChennaiCOBOL _ # 129 _Ver 2.0
Syntax 5: VARYING option
PERFORM procedure-name-1[ THRU procedure-name-2 ]
{identifier-2} {identifier-3}
VARYING identifier-1 FROM {literal-1 } BY {literal-2 }
{expr.-1 } {expr.-2 }
UNTIL condition-1{identifier-5} {identifier-6}
[ AFTER identifier-4 FROM {literal-3 } BY {literal-4 }
{expr.-3 } { expr.-4 }
UNTIL condition-2
{identifier-8} {identifier-9}[ AFTER identifier-7 FROM {literal-5 } BY {literal-6 }
{expr.-5 } {expr.-6 }
UNTIL condition-3 ] ]
PERFORM
Chapter 5 Structured Programming
-
7/30/2019 COBOL_CDC[1]
130/313
CDC_Covansys_ChennaiCOBOL _ # 130 _Ver 2.0
Eg5.
PERFORM RANGE-TO-BE-EXECUTED
VARYING I FROM 1 BY 1 UNTIL I > 50
AFTER J FROM 1 BY 1 UNTIL J > 10.
The RANGE-TO-BE-EXECUTED will be performed 500 times.
With I as 1 and J varying from 1 to 10 in step of 1;
With I as 2 and J again varying from 1 to 10 in step of 1;
... and so on, till ...
With I as 50 and J again varying from 1 to 10 in step of 1.
i.e, Every time the value of I changes, J varies from 1 to 10.
5.3 Structured Programming
Chapter 5 Structured Programming
-
7/30/2019 COBOL_CDC[1]
131/313
CDC_Covansys_ChennaiCOBOL _ # 131 _Ver 2.0
g g
Enabling optimization of code by presenting the logic in themost structured way possible
Using the three kinds of control structures that facilitateStructured Programming namely
Simple Sequencing
Selection
Repetition
-
7/30/2019 COBOL_CDC[1]
132/313
Selection
Chapter 5 Structured Programming
-
7/30/2019 COBOL_CDC[1]
133/313
CDC_Covansys_ChennaiCOBOL _ # 133 _Ver 2.0
Performing an operation depending on a condition
Eg.
...........
IF WS-EOF = N
PERFORM 0002-OPEN-PARA.
...........
selection of a course of activity only if a condition holds true
Repetition
Chapter 5 Structured Programming
-
7/30/2019 COBOL_CDC[1]
134/313
CDC_Covansys_ChennaiCOBOL _ # 134 _Ver 2.0
Continually performing an operation until a certain condition holdstrue
Eg.
...........
PERFORM 0002-OPEN-PARA
UNTIL WS-EOF = Y
...........
iteration of a para (set of instructions) till a condition is true
Chapter 6 - Files6.1 File Handling
-
7/30/2019 COBOL_CDC[1]
135/313
CDC_Covansys_ChennaiCOBOL _ # 135 _Ver 2.0
Sequential File
Records are stored serially - one after another
Used when data volume is very high or when all the recordsstored in the file are to be processed
ENVIRONMENT DIVISION EntriesINPUT-OUTPUT SECTION.FILE-CONTROL.SELECT file-name
{DISK }ASSIGN [TO] {TAPE }
{PRINTER}[ ; ORGANIZATION IS SEQUENTIAL][ ; ACCESS MODE [IS] SEQUENTIAL][ ; FILE STATUS IS data-name-1]
-
7/30/2019 COBOL_CDC[1]
136/313
Sequential File
Chapter 6 - Files
-
7/30/2019 COBOL_CDC[1]
137/313
CDC_Covansys_ChennaiCOBOL _ # 137 _Ver 2.0
DATA DIVISION Entries
FILE SECTION Clauses
Clause Specifies BLOCK CONTAINS the number of records in a block
RECORD CONTAINS the maximum number of characters ina record
LABEL RECORD the presence or absence oflabel information
VALUE OF the file-attribute
DATA RECORD documents the names ofdata records of the associated file
Note_01 All the above clauses are optional
Sequential File
Chapter 6 - Files
-
7/30/2019 COBOL_CDC[1]
138/313
CDC_Covansys_ChennaiCOBOL _ # 138 _Ver 2.0
Eg.
FD EMP-FILE
BLOCK CONTAINS 10 RECORDS
RECORD CONTAINS 100 CHARACTERS
DATA RECORD IS FS-EMP-REC
LABEL RECORDS ARE STANDARD
VALUE OF FILE-ID IS PAYROLL.DAT.
Indexed File
Chapter 6 - Files
-
7/30/2019 COBOL_CDC[1]
139/313
CDC_Covansys_ChennaiCOBOL _ # 139 _Ver 2.0
Records are accessed by the value of one or more keys withinthem
ENVIRONMENT DIVISION Entries
INPUT-OUTPUT SECTION.
FILE-CONTROL.SELECT file-name
ASSIGN [TO] DISK
ORGANIZATION IS INDEXED
ACCESS MODE IS [ Dynamic / Random / Sequential ]
RECORD KEY IS data-name-1[ ;ALTERNATE RECORD KEY IS data-name-2]
[ ; FILE STATUS IS data-name-3]
Indexed File
Chapter 6 - Files
-
7/30/2019 COBOL_CDC[1]
140/313
CDC_Covansys_ChennaiCOBOL _ # 140 _Ver 2.0
Explanations
ORGANIZATION Specifies that the file is an
INDEXED indexed file
ACCESS MODE Specifies the way the record isaccessed
Sequential in a sequential manner
Random in a random manner
Dynamic in both sequential and randommanner, in the same program
RECORD KEY Specifies which data item holds the keyvalue
ALTERNATE Allows an indexed file to be,
RECORD created with / accessed by more
KEY than one identifying key
FILE STATUS Contains the status of the operationduring the execution of a file handlingstatement; status fields length=2 bytes
Relative File
Chapter 6 - Files
-
7/30/2019 COBOL_CDC[1]
141/313
CDC_Covansys_ChennaiCOBOL _ # 141 _Ver 2.0
Organized in such a way that each record is identified by arelative record number
The relative record number specifies the position of the recordfrom the beginning of the file
Can be accessed either sequentially or randomly
When the access is sequential, the records are accessed in theincreasing order of their relative record numbers
When the access is random, the relative record number must bespecified and the record is accessed directly regardless of theprevious access of any other record in the file
Organization / Access Mode / Open Mode / Functions
Chapter 6 - Files
-
7/30/2019 COBOL_CDC[1]
142/313
CDC_Covansys_ChennaiCOBOL _ # 142 _Ver 2.0
Possible Combinations
ORGANIZATION SEQUENTIAL
( the way data is stored INDEXED
i.e, arranged ) RELATIVE
ACCESS MODE SEQUENTIAL
( the way data is accessed RANDOM
i.e, read / written ) DYNAMIC
Organization / Access Mode / Open Mode / FunctionsPossible Combinations
Chapter 6 - Files
-
7/30/2019 COBOL_CDC[1]
143/313
CDC_Covansys_ChennaiCOBOL _ # 143 _Ver 2.0
Possible Combinations
OPEN MODE OUTPUT
( the way the file is opened ) INPUT
I-O
EXTEND
FUNCTIONS WRITE
( the different operations that READ
can be performed on the REWRITE
record / file ) DELETE------------
START
6.2 File-Oriented I/O
Chapter 6 - Files
-
7/30/2019 COBOL_CDC[1]
144/313
CDC_Covansys_ChennaiCOBOL _ # 144 _Ver 2.0
OPEN
Initiates processing of a file
Every file must be opened before use and closed after use
For this statement to be executed, the file should not be already
open
Syntax
OPEN INPUT file-name-1
OUTPUT file-name-2I-O file-name-3
EXTEND file-name-4
OPEN
Chapter 6 - Files
-
7/30/2019 COBOL_CDC[1]
145/313
CDC_Covansys_ChennaiCOBOL _ # 145 _Ver 2.0
Modes of OPENing a file
INPUT Reads records from a file
OUTPUT Writes records onto a file ( firsttime creation, irrespective of the
files existence ) I-O Reads from and writes onto a file
EXTEND Appends records to a file
Note_01Sequential file can be opened in any of the above modes
Indexed and Relative files cannot be opened in the Extend mode
OPEN
Chapter 6 - Files
-
7/30/2019 COBOL_CDC[1]
146/313
CDC_Covansys_ChennaiCOBOL _ # 146 _Ver 2.0
Eg1.(To read from the sequential file DAILY-ACT and write onto thesequential file MAST-ACT)
OPEN INPUT DAILY-ACT OUTPUT MAST-ACT.
Eg2.
(To read from the sequential file DAILY-ACT and append
records onto the sequential file MAST-ACT)
OPEN INPUT DAILY-ACT EXTEND MAST-ACT.
CLOSE
Chapter 6 - Files
-
7/30/2019 COBOL_CDC[1]
147/313
CDC_Covansys_ChennaiCOBOL _ # 147 _Ver 2.0
Terminates the processing of files
For this statement to be executed, the file must be open
A simple CLOSE statement releases the file from the program
Syntax
CLOSE file-name
Eg.CLOSE DAILY-ACT MAST-ACT.
Note_01It is customary to OPEN a file in the beginning of a program andCLOSE it at its end
READ
Chapter 6 - Files
-
7/30/2019 COBOL_CDC[1]
148/313
CDC_Covansys_ChennaiCOBOL _ # 148 _Ver 2.0
Retrieves the next available record from an opened file
Syntax - 1
READ file-name [ RECORD ]
[ INTO identifier ]
[; AT END imperative-stmt.]
READ
Chapter 6 - Files
-
7/30/2019 COBOL_CDC[1]
149/313
CDC_Covansys_ChennaiCOBOL _ # 149 _Ver 2.0
Explanations
Used for Sequential files only
Before the use of the READ statement, the file should be
opened in the INPUT or I-O mode
Eg1.
READ DAILY-ACT AT END MOVE Y TO WS-EOF.
READ
Chapter 6 - Files
-
7/30/2019 COBOL_CDC[1]
150/313
CDC_Covansys_ChennaiCOBOL _ # 150 _Ver 2.0
Syntax - 2
READ file-name [ RECORD ]
[ INTO identifier ]
[ ; INVALID KEY imperative-stmt. ]
Explanations Used for Random or Dynamic access in Indexed and Relative
files only
INVALID KEY condition occurs when the record key does not
match that of an existing record
Eg2.
READ CUST-FILE INVALID KEY PERFORM ERR-RTN.
READ
Chapter 6 - Files
-
7/30/2019 COBOL_CDC[1]
151/313
CDC_Covansys_ChennaiCOBOL _ # 151 _Ver 2.0
Syntax - 3
READ file-name [ NEXT ] [ RECORD ]
[ INTO identifier ]
[; AT END imperative-stmt. ]
Explanations
Beginning a sequential read from some point other than thebeginning of a file
Used for Dynamic accessing
NEXT is used to perform a Sequential read of an Indexed fileaccessed Dynamically
READ
Chapter 6 - Files
-
7/30/2019 COBOL_CDC[1]
152/313
CDC_Covansys_ChennaiCOBOL _ # 152 _Ver 2.0
Eg3.
READ CUST-FILE
NEXT RECORD
AT END
MOVE NO TO ARE-THERE-RECORDS.
Note_01
READ... reads the record from the file and stores it in theidentifier(s) of the record description pertaining to the file-namespecified
READ...INTO... reads the record from the file and stores it inthe identifier(s) of the record description pertaining to the file-name specified, as well as the identifier specified with the INTOoption
WRITE
Chapter 6 - Files
-
7/30/2019 COBOL_CDC[1]
153/313
CDC_Covansys_ChennaiCOBOL _ # 153 _Ver 2.0
Stores a record in a file opened in the OUTPUT mode or
EXTEND mode or in the I-O mode
Syntax - 1
WRITE record-name [ FROM identifier-1 ]
Used for sequential files
WRITE...FROM... writes the record to the file from the identifierspecified with the FROM option
Eg1.(To write records into the record EMP-REC of the sequential fileEMP-FILE)
WRITE EMP-REC.
WRITE
Chapter 6 - Files
-
7/30/2019 COBOL_CDC[1]
154/313
CDC_Covansys_ChennaiCOBOL _ # 154 _Ver 2.0
Syntax - 2
WRITE record-name [ FROM identifier-1 ][ INVALID KEY imperative-stmt.-1 ]
Explanations
Used for indexed and relative files
INVALID KEY condition occurs when the record key matches thatof an existing record
Eg2.WRITE EMP-REC
INVALID KEY DISPLAY Record cannot be added.
Note_01File name should not be mentioned
REWRITE
Chapter 6 - Files
-
7/30/2019 COBOL_CDC[1]
155/313
CDC_Covansys_ChennaiCOBOL _ # 155 _Ver 2.0
Replaces an existing record in a file
File should be opened in the I-O mode
Rewrite statement, preferably to be preceded by a READstatement
Syntax - 1
REWRITE record-name [ FROM identifier-1 ]
(Used for Sequential files)
Eg1.REWRITE DAILY-ACT-REC.
REWRITE
Chapter 6 - Files
-
7/30/2019 COBOL_CDC[1]
156/313
CDC_Covansys_ChennaiCOBOL _ # 156 _Ver 2.0
Syntax - 2
REWRITE record-name [ FROM identifier-1 ]
[ INVALID KEY imperative-stmt. ]
Explanations
Used for Indexed files
INVALID KEY condition occurs when the record key does not
match that of an existing record
Eg2.
REWRITE EMP-REC INVALID KEY PERFORM ERR-RTN.
DELETE
Chapter 6 - Files
-
7/30/2019 COBOL_CDC[1]
157/313
CDC_Covansys_ChennaiCOBOL _ # 157 _Ver 2.0
Deletes the record identified by the key given
Used only for Indexed and Relative files
Associated file to be opened in the I-O mode
Delete statement, preferably to be preceded by a READstatement
DELETE
Chapter 6 - Files
-
7/30/2019 COBOL_CDC[1]
158/313
CDC_Covansys_ChennaiCOBOL _ # 158 _Ver 2.0
Syntax
DELETE file-name [ RECORD ]
[ INVALID KEY imperative-stmt. ]
INVALID KEY condition occurs when the record key does notmatch that of an existing record
Eg.DELETE CUST-FILE
INVALID KEY PERFORM ERR-ROUTINE.
START
Chapter 6 - Files
-
7/30/2019 COBOL_CDC[1]
159/313
CDC_Covansys_ChennaiCOBOL _ # 159 _Ver 2.0
Starts processing records from a particular point in a file
Positions the pointer in the file to the first logical record whoserecord key satisfies the condition specified by the key phrase
Access mode to be Sequential or Dynamic
File to be opened in the I-O or Input mode
START
Chapter 6 - Files
-
7/30/2019 COBOL_CDC[1]
160/313
CDC_Covansys_ChennaiCOBOL _ # 160 _Ver 2.0
Syntax
START filename [ KEY IS EQUAL TO data-name ][ INVALID KEY imperative-stmt. ]
Explanations
Data name in the KEY field must be the record key, alternatekey or any alphanumeric data item, that must be the leftmostfield in the record key
If the 'KEY IS EQUAL TO' phrase is omitted, the key for theStart verb is taken as the data name that follows the 'RECORD
KEY IS' phrase in the SELECT clause
The INVALID KEY condition arises if the condition specified bythe key phrase cannot be met
START
Chapter 6 - Files
-
7/30/2019 COBOL_CDC[1]
161/313
CDC_Covansys_ChennaiCOBOL _ # 161 _Ver 2.0
Eg. Assume the following SELECT clause of an indexed file
SELECT IND-FILE ASSIGN TO DISK
ORGANIZATION IS INDEXED
ACCESS MODE IS SEQUENTIAL
RECORD KEY IS REC-KEY.
Let us further assume that the record description of the file
describes the record key in the following manner
02 REC-KEY.03 FIRST-PART PIC X.
03 FILLER PIC X(7).
START
Chapter 6 - Files
-
7/30/2019 COBOL_CDC[1]
162/313
CDC_Covansys_ChennaiCOBOL _ # 162 _Ver 2.0
Now, suppose we wish to position the file at the first record
whose key begins with the letter C
MOVE C TO FIRST-PART.START IND-FILEKEY IS EQUAL TO FIRST-PART
INVALID KEY PERFORM ERROR-RTN.
This is equivalent to
MOVE C TO FIRST-PART.START IND-FILE
INVALID KEY PERFORM ERROR-RTN.
(with an assumed RECORD KEY IS REC-KEY.)
File Access Table (Access Mode Sequential)
Chapter 6 - Files
-
7/30/2019 COBOL_CDC[1]
163/313
CDC_Covansys_ChennaiCOBOL _ # 163 _Ver 2.0
Sequential Indexed Relative
Open Mode
READ
I O I-O E I O I-O E I O I-O E
WRITE
REWRITE
START
DELETE
Y
Y
Y
Y
Y
Y Y
Y
Y
Y
Y
Y
Y
Y
Y
Y
Y
Y
Y
Organization
( q )
File Access Table (Access Mode Random)
Chapter 6 - Files
-
7/30/2019 COBOL_CDC[1]
164/313
CDC_Covansys_ChennaiCOBOL _ # 164 _Ver 2.0
Sequential Indexed Relative
Open Mode
READ
I O I-O E I O I-O E I O I-O E
WRITE
REWRITE
START
DELETE
Y Y
Y
Y
Y
Y
YY
Y
Y
Y
Organization
( )
Y
File Access Table (Access Mode Dynamic)
Chapter 6 - Files
-
7/30/2019 COBOL_CDC[1]
165/313
CDC_Covansys_ChennaiCOBOL _ # 165 _Ver 2.0
Sequential Indexed Relative
Open Mode
READ
I O I-O E I O I-O E I O I-O E
WRITE
REWRITE
START
DELETE
Y Y
Y Y
Y
Y
Y
YY
Y
Y
Y
Organization
( y )
Y Y Y Y
Chapter 7 Redefines & Renames7.1 Redefines
-
7/30/2019 COBOL_CDC[1]
166/313
CDC_Covansys_ChennaiCOBOL _ # 166 _Ver 2.0
Refers to the same storage area by different names with
different formats and sizes
Views a field of data with a different structure
Applies to storage area involved and not to the data that's
stored in that area
The programmer is to work with the data names which
correctly match the actual contents of the memory at a given
point
Redefines
Chapter 7 Redefines & Renames
-
7/30/2019 COBOL_CDC[1]
167/313
CDC_Covansys_ChennaiCOBOL _ # 167 _Ver 2.0
Syntax
level-number data-name-2 REDEFINES data-name-1
Explanations
Used in group level and elementary level
Multiple redefinition is allowed
Data names corresponding to special levels (66, 88) and those
that appear at the 01 level of the File Section cannot be redefined
Redefines Eg1.
01 WS TELEPHONE NUMBER PIC X(10)
Chapter 7 Redefines & Renames
-
7/30/2019 COBOL_CDC[1]
168/313
CDC_Covansys_ChennaiCOBOL _ # 168 _Ver 2.0
01 WS-TELEPHONE-NUMBER PIC X(10).01 WS-R-TEL-NUMBER REDEFINES WS-TELEPHONE-NUMBER.
05 WS-AREA-CODE PIC X(3).05 WS-CODE1 PIC X(4).05 WS-CODE2 PIC X(3).
Eg2.
01 PRODUCT-CODE.05 PLANT-CODE PIC 9(3).05 MATL-CODE PIC X(4).05 SIZE-CODE PIC 99.
01 PRODUCT-INFO REDEFINES PRODUCT-CODE.05 PLANT-LOCN PIC X(2).
05 PLANT-NUM PIC 9.05 MATL-TYPE PIC X(2).05 MATL-CAT PIC 99.05 PRODUCT-PACK PIC XX.
Redefines Eg3.
Chapter 7 Redefines & Renames
-
7/30/2019 COBOL_CDC[1]
169/313
CDC_Covansys_ChennaiCOBOL _ # 169 _Ver 2.0
01 WS-STORED-AREAS.
05 WS-DATE PIC X(8).05 WS-DATE-X REDEFINES WS-DATE.
10 WS-YEAR PIC 99.
10 FILLER PIC X VALUE /.
10 WS-MONTH PIC 99.
10 FILLER PIC X VALUE /.
10 WS-DATE PIC 99.
Eg4. ( REDEFINES with a PIC clause )
01 WS-SAMPLE.
05 WS-NAME1.
10 WS-SUR-NAME PIC X(10).10 WS-MY-NAME PIC X(10).
05 WS-NAME2 REDEFINES WS-NAME1 PIC X(20).
Redefines
Chapter 7 Redefines & Renames
-
7/30/2019 COBOL_CDC[1]
170/313
CDC_Covansys_ChennaiCOBOL _ # 170 _Ver 2.0
Group item redefined as an Elementary item
REDEFINES clause must immediately follow the redefined dataname
The Redefined and the Redefining data names should be ofidentical levels
The redefined and the redefining data names cannot beaccessed simultaneously
At 01 level the size of the redefining data name can be greater
than or equal to the size of the redefined data name
At other levels sizes of the redefining and redefined datanames ought to be identical
-
7/30/2019 COBOL_CDC[1]
171/313
Renames
Explanations
Chapter 7 Redefines & Renames
-
7/30/2019 COBOL_CDC[1]
172/313
CDC_Covansys_ChennaiCOBOL _ # 172 _Ver 2.0
Explanations
Data-name-4 to be present in Area B
Data-name-1 and data-name-3 to be names of elementary items/ group items, not items of levels 01, 66,77 or 88
Both data-name-1 and data-name-3 should not have an Occursclause, nor can they be subordinate to an item that has anOccurs clause in its data description entry
Data-name-3, if specified must follow data-name-1, in the recordand must not be one of its sub fields
Renames Eg1.
01 WS PAY
Chapter 7 Redefines & Renames
-
7/30/2019 COBOL_CDC[1]
173/313
CDC_Covansys_ChennaiCOBOL _ # 173 _Ver 2.0
01 WS-PAY.
05 WS-FIXED-PAY.10 WS-BASIC PIC 9(6)V99.
10 WS-DA PIC 9(6)V99.
05 WS-ADDITIONAL-PAY.
10 WS-HRA PIC 9(6)V99.
05 WS-MTHLY-INCENTIVE PIC 9(6)V99.
05 WS-DEDUCTIONS.
10 WS-PF PIC 9(6)V99.
10 WS-IT PIC 9(6)V99.
10 WS-OTHER PIC 9(6)V99.
66 WS-PAY-OTHER-BASIC RENAMES WS-DATHRU WS-MTHLY-INCENTIVE.
66 WS-IT-AND-PF RENAMES WS-PF THRU WS-IT.
Renames
Note_01
R bl REDEFINES t th t it f i f d t
Chapter 7 Redefines & Renames
-
7/30/2019 COBOL_CDC[1]
174/313
CDC_Covansys_ChennaiCOBOL _ # 174 _Ver 2.0
Resembles REDEFINES, except that it can form a new grouping of dataitems which combines several items
Note_02
The elementary data items to be regrouped, whether present in thesame group or across different groups, ought to be contiguous
Eg2.
01 WS-SAMPLE.
05 WS-NAME1.
10 WS-SUR-NAME PIC X(10).
10 WS-MY-NAME PIC X(10).
05 WS-NAME2 REDEFINES WS-NAME1 PIC X(20).
... REDEFINES acting as a RENAMES clause
Chapter - 8 - Tables8.1 Table
-
7/30/2019 COBOL_CDC[1]
175/313
CDC_Covansys_ChennaiCOBOL _ # 175 _Ver 2.0
Set of values stored in consecutive storage locations andassigned a specific data name - the table name
Structured way of defining memory areas that the program will
address
Contains homogeneous elements stored in rows and columns- the table items
A specific table item is referred to by the use of the Tablename and a Subscript
Table
Eg.
The phone-bill paid every month For One Year can be represented in COBOL as
Chapter - 8 - Tables
-
7/30/2019 COBOL_CDC[1]
176/313
CDC_Covansys_ChennaiCOBOL _ # 176 _Ver 2.0
p p y p
01 WS-PHONE-BILL-DETAILS.05 WS-PHONE-BILL-JAN PIC 9(6)V99.
05 WS-PHONE-BILL-FEB PIC 9(6)V99.
05 WS-PHONE-BILL-MAR PIC 9(6)V99.
05 WS-PHONE-BILL-APR PIC 9(6)V99.
05 WS-PHONE-BILL-MAY PIC 9(6)V99.
05 WS-PHONE-BILL-JUN PIC 9(6)V99.05 WS-PHONE-BILL-JUL PIC 9(6)V99.
05 WS-PHONE-BILL-AUG PIC 9(6)V99.
05 WS-PHONE-BILL-SEP PIC 9(6)V99.
05 WS-PHONE-BILL-OCT PIC 9(6)V99.
05 WS-PHONE-BILL-NOV PIC 9(6)V99.
05 WS-PHONE-BILL-DEC PIC 9(6)V99. Note_01
A cumbersome approach to store data for a period of 5 years.
COBOL simplifies this with the concept of Tables and the Occurs clause
-
7/30/2019 COBOL_CDC[1]
177/313
OCCURS Eg.
The phone-bill details will now be represented as
Chapter - 8 - Tables
-
7/30/2019 COBOL_CDC[1]
178/313
CDC_Covansys_ChennaiCOBOL _ # 178 _Ver 2.0
p p
01 WS-PHONE-TABLE.05 WS-PHONE-BILL PIC 9(6)V99 OCCURS 12 TIMES.
... the Phone-Table with 12 values, one each for the 12 months
A simple change in the value of 12 to 60 ...
01 WS-PHONE-TABLE.
05 WS-PHONE-BILL OCCURS 60 TIMES PIC 9(6)V99.
... makes the Phone-Table hold data for a period of 5 years
The number of OCCURS clause specified in the table definition, categorizestables as
One-dimensional
Multi-dimensional
8.2 Subscripts
Chapter - 8 - Tables
-
7/30/2019 COBOL_CDC[1]
179/313
CDC_Covansys_ChennaiCOBOL _ # 179 _Ver 2.0
Refer to an individual element in a table
Identifiers that denote the location of an entry in the table
Enclosed within a pair of parenthesis
Can be integers or data names with positive integral values
Data name used as a subscript cannot itself be subscripted
Eg. ABC(I(K)) is not allowed, because I is a subscript and is itselfnot to be subscripted
8.2.1 Values of a subscript
Chapter - 8 - Tables
-
7/30/2019 COBOL_CDC[1]
180/313
CDC_Covansys_ChennaiCOBOL _ # 180 _Ver 2.0
Highest - the one specified by the integer in the OCCURS
clause
Lowest - implicitly assumed to be 1
Syntax 1 : One-Dimensional table
Data-name (subscript)
Syntax 2: Multi-Dimensional table
Data-name(subscript1, subscript2..)
Subscripts
Chapter - 8 - Tables
-
7/30/2019 COBOL_CDC[1]
181/313
CDC_Covansys_ChennaiCOBOL _ # 181 _Ver 2.0
Note_01
Multi-dimensional tables are specified by using more than one
subscript ;
In a multi-dimensional table, the 1st data name with the
OCCURS clause indicates the 1st subscript, the 2nd data
name with the OCCURS clause the 2nd subscript, and so on
8.3 Multi-dimensional tables
R i l i l b i l i di id l bl i
Chapter - 8 - Tables
-
7/30/2019 COBOL_CDC[1]
182/313
CDC_Covansys_ChennaiCOBOL _ # 182 _Ver 2.0
Require multiple subscripts to locate an individual table item
Eg1. Two-dimensional table
Table to store courses taken by a student in four semesters with six
courses per semester
01 WS-COURSE-DETAILS.
02 WS-ROLL-NO PIC 9(6).
02 WS-NAME PIC X(25).
02 WS-SEMESTER OCCURS 4 TIMES.
04 WS-COURSE OCCURS 6 TIMES.
06 WS-CN-NAME PIC XXX.
06 WS-CN-MARKS PIC 999.
Multi-dimensional tables
Chapter - 8 - Tables
-
7/30/2019 COBOL_CDC[1]
183/313
CDC_Covansys_ChennaiCOBOL _ # 183 _Ver 2.0
To retrieve the marks of the 3rd
Course attended by a student inthe 2nd Semester
WS-CN-MARKS(2,3).
Subscript 2 selects the SEMESTER and subscript 3 selects theCOURSE within it
This being equivalent to
WS-CN-MARKS(3) IN WS-SEMESTER(2)
Multi-dimensional tables
Chapter - 8 - Tables
-
7/30/2019 COBOL_CDC[1]
184/313
CDC_Covansys_ChennaiCOBOL _ # 184 _Ver 2.0
Eg2. Three-dimensional tableTable to store marks scored by a student in 3 Exams under 10Courses across 20 Departments in a University
01 WS-UNIVERSITY-TABLE.05 WS-DEPT OCCURS 20 TIMES.
10 WS-COURSE OCCURS 10 TIMES.
15 EXAM-MARKS-1 PIC 9(3).
15 EXAM-MARKS-2 PIC 9(3).
15 EXAM-MARKS-3 PIC 9(3).
Multi-dimensional tables
Chapter - 8 - Tables
-
7/30/2019 COBOL_CDC[1]
185/313
CDC_Covansys_ChennaiCOBOL _ # 185 _Ver 2.0
Equivalent three-dimensional table
01 WS-UNIVERSITY-TABLE.
05 WS-DEPT OCCURS 20 TIMES.
10 WS-COURSE OCCURS 10 TIMES.
15 WS-EXAM-MARKS OCCURS 3 TIMES PIC 9(3).
Multi-dimensional tables
Chapter - 8 - Tables
-
7/30/2019 COBOL_CDC[1]
186/313
CDC_Covansys_ChennaiCOBOL _ # 186 _Ver 2.0
To retrieve the marks of a student in the 2nd Exam under the 8thCourse offered by the 12th Department
WS-EXAM-MARKS(12,8,2).
This being equivalent to
WS-EXAM-MARKS(2) IN WS-COURSE(8) IN WS-DEPT(12)
Multi-dimensional tables
Chapter - 8 - Tables
-
7/30/2019 COBOL_CDC[1]
187/313
CDC_Covansys_ChennaiCOBOL _ # 187 _Ver 2.0
Note_01
While accessing an item in a multi-dimensional table, the
subscript corresponding to the outermost OCCURS level is to
be specified first, followed by the one corresponding to the next
OCCURS, and so on ; the table item corresponding to the
innermost OCCURS level is to be specified as the data name
8.4 Initialization of tables using REDEFINES
Chapter - 8 - Tables
-
7/30/2019 COBOL_CDC[1]
188/313
CDC_Covansys_ChennaiCOBOL _ # 188 _Ver 2.0
REDEFINES clause cannot appear in the same datadescription entry which contains an OCCURS clause
REDEFINES clause can appear for a group item whose
subordin