(NOTE: PLEASE CLICK ON THE IMAGES FOR BETTER VIEW AND CLARITY)
COBOL – Stands for Common Business Oriented Language.
This language is developed in 1959 by Conference on Data Systems Languages (CODASYL). It is one of the First High-level language developed. It is used in mainframe systems. It is English like language as it uses English keywords like DISPLAY, ADD, SUBTRACT etc.
Structure of COBOL:
COBOL Language consists of below components:
1) Divisions - Compulsory
2) Sections - Optional
3) Paragraphs – Compulsory in some conditions
4) Statements - compulsory
5) Sentences - compulsory
Divisions in COBOL:
All Division names should start in Area A.
1) IDENTIFICATION DIVISION.
2) ENVIRONMENT DIVISION
3) DATA DIVISION
4) PROCEDURE DIVISION
DIVISIONS:
1) IDENTIFICATION DIVISION
This division identifies name of the program, author of the program, date written and compiled. Below items are provided in IDENTIFICATION DIVISION.
IDENTIFICATION DIVISION. (Compulsory)
PROGRAM-ID. INLBTEST. (Compulsory)
AUTHOR. SANDIP WALSINGE. (Optional)
DATE WRITTEN. 30-03-2020. (Optional)
DATE COMPILED. 30-03-2020. (Optional)
IDENTIFICATION DIVISION:
Every program should start with IDENTIFICATION Division.
PROGRAM-ID :
In PROGRAM-ID name of program is provided. Name of program should be 1-8 alphanumeric characters. It should not start with number or special characters such as &,$,#,@. Hyphen should given in between Program and ID. One blank space should be given between PROGRAM-ID and name of program.
Other parameters are optional.
2) ENVIRONMENT DIVISION:
This division describes the environment in which program will run, files which will be used in this program, any special names (like comma) used in program.
This division consists of two Sections:
1) CONFIGURATION SECTION.
2) INPUT-OUTPUT SECTION.
Environment division is option but it is standard practice to define this this division. If we do not define this division program will execute without any error.
CONFIGURATION SECTION:
This section consists of below parameters –
CONFIGURATION SECTION.
SOURCE-COMPUTER. IBM-390.
OBJECT-COMPUTER. IBM-390.
SPECIAL-NAMES.
DECIMAL-POINT IS COMMA.
INPUT-OUTPUT SECTION:
This section describes files and their access modes, type of organization. It consists of below parameters –
INPUT-OUTPUT SECTION.
FILE-CONTROL.
SELECT INFILE ASSIGN TO DD1 - COMP
ORGANIZATION IS SEQUENTIAL - OPT
ACCESS MODE IS SEQUENTIAL - OPT
FILE-STATUS IS FS1. - OPT
3) DATA DIVISION:
In this section all variables and files are defined. It consists of below three sections:
A) FILE SECTION:
File structure is defined in this section.
B) WORKING-STORAGE SECTION:
All program variables are defined in this section.
C) LINKAGE SECTION.
This section is defined either in sub program to receive data from main program. Also it is defined in main program to receive data from PARM parameter.
4) PROCEDURE DIVISION:
Program logic is written in this division.
Sample Display Program:
@Sandip Walsinge
----------------------------------------------------------------------------------
Characters:
In COBOL below characters are used:
A to Z (26 chars) (small and caps)
0 to 9 (10 chars)
Other characters:
1) - Minus
2) + Plus
3) * Asterisk or used for multiplication (in 7th Column indicates comment)
4) / Slash used for Division
5) = Equal to sign
6) $ Currency sign
7) , Comma
8) : Colon
9) ; Semicolon
10) ‘ “ Single and double quotes
11) ( ) Opening and closing brackets
12) < > Less than and greater than sign
13) - Hyphen (In 7th Column indicates continuation)
14) . Period or full stop
@Sandip Walsinge
----------------------------------------------------------------------------------
COBOL Words:
There are two types of COBOL words:
1) User defined words - defined by developer
2) COBOL reserved words (Keywords)
Rules for user defined COBOL words:
These words are defined by users to define file names, Variables, records, paragraph, sections.
1) It should be of max 30 chars.
2) First letter should be char.3) Not begin or end with Hyphen.
4) No blank space or special char except hyphen in between.
User Defined Words:
ACCOUNT-NO – Valid Word
ACCOUNT NO - Invalid
COBOL Reserved Words:
These are COBOL reserved keywords like ADD, MOVE, DIVIDE, COMPUTE,
DISPLAY, ACCEPT, READ, OPEN, CLOSE, WRITE, REWRITE etc.
@Sandip Walsinge
----------------------------------------------------------------------------------
Literals, Identifiers and Figurative Constants:
Literals :
Literal is a constant, non-numeric and numeric. Numeric literal can store up to 18 bytes and non-numeric can store up to 160 bytes value.
Identifier:
Identifier or variable defined by user which holds value of literal.
01 ACCT-NO PIC 9(04) VALUE 1234.
Here ACCT-NO is Identifier or variable and 1234 is literal.
Figurative Constants:
These are COBOL reserved words which indicate values.
ZERO, ZEROS, ZEROES - 000
SPACE, SPACES, SPACE
LOW-VALUE, LOW-VALUES - 000000
HIGH-VALUE, HIGH-VALUES - 999999
QUOTE, QUOTES - “
@Sandip Walsinge
----------------------------------------------------------------------------------
Variable declaration:
Variables are defined in DATA DIVISION.
Syntax for Variable definition:
[Level Number] [Variable | FILLER] [Picture] [Usage] [Sync] [Value]
Level Number:
Level numbers are defined as below –
01 - Group data item or elementary data item.
02 - 48 - Group data item or elementary data items.
49 - Elementary data item.
77 – Elementary data item.
66 – Renames clause – Will be covered in Clauses.
88 – Conditional name
01 EID PIC X(05) Elementary Data Item
01 EMP-INFO. Group data item
05 ENAME PIC X(10). Elementary data item
05 ESAL PIC 9(06). Elementary data item
77 EADD PIC X(20) Elementary data item.
01 CREDIT-CARD PIC X(01).
88 GOLD-CARD VALUE ‘G’. - Conditional names
88 PLATINUM-CARD VALUE ‘P’. - Conditional names
88 TITANIUM-CARD VALUE ‘T’. - Conditional names
Variable Names:
Variable names are nothing but user-defined words which are covered earlier.
FILLER :
If any selected field is not used in program then it can be defined as FILLER. It does not take part in any operation or can not be initialized.
01 EMP-INFO.
05 EID PIC X(05).
05 FILLER PIC X(01).
05 ENAME PIC X(10).
05 FILLER PIC X(64).
PICTURE Clause:
9 – Numeric variable 01 ACCT-NO PIC 9(16).
X – Alphanumeric 01 ACCT-ADD PIC X(26).
A – Alphabetic 01 ACCT-NAME PIC A(10).
V – Assumed Decimal 01 TRANS-AMT PIC 9999V99. 1234.89191
P – Positioned decimal 01 TRANS-AMT PIC 9999P. 1234
S – Signed 01 TRANS-AMT PIC S9(04). -1234.
Program for PICTURE CLAUSE:
Output of Program:
Numeric Editing characters:
Always for display variables
Z – Suppress leading zeros. PIC ZZZ9 0005 == > bbb5 (b is space character)
$ - Currency sign PIC $9999 0005 == > $0005
* - Asterisk PIC *9999 0005 == > *0005
-, +
DB, CR
Program for EDITING CHARACTERS:
Output of Program:
Usage Clause:
Usage clause defines how data to be stored in memory.
DISPLAY,
COMP
COMP-1
COMP-2,
COMP-3,
COMP-4,
COMP-5
DISPLAY Usage Clause:
This is simple display format.
01 EID PIC 9(04). - Display format. 1234.
COMP:
Data is stored in Binary format.
01 SEQ-NO PIC 9(04) COMP.
9(01) To 9(04) COMP – 2 Bytes - Half word ( 8 Bits – 2 Nibbles - 1 Byte)
9(05) To 9(09) COMP – 4 Bytes – full word ( 4 Bytes - one word)
9(10) To 9(18) COMP – 8 Bytes – Double word ( 8 Bytes – double word)
COMP-1:
Single word floating point. (4 Bytes), for large numbers this is used. Leftmost bit is used for sign 7 bits of 1 bytes are used to store exponent and last 3 bytes are used to store mantissa. PIC is not defined in this clause.
01 VEL-OF-LIGHT COMP-1.
[ 3 * 10 rest to power 8 (3 – mantissa , 10 8 – Exponent) ]
COMP-2:
Double word floating point. 7 Bytes are for mantissa. Used for high precision calculation.
01 LIGHT-YEARS COMP-2.
COMP-3:
Packed decimal field. This usage is used for better utilization of memory as it stores two digits in one byte. Sign is stored in last nibble (last half byte). Value of this field can be seen by HEX ON.
C in last nibble indicates Plus (+)
D in last nibble indicates Minus (-)
01 TRANS-AMT PIC 9(08) COMP-3 – 5 Bytes
01 TRANS-AMT1 PIC 9(05) COMP-3 – 3 bytes
Calculation of bytes for comp-3:
Even PIC – N/2 + 1 for exam – 8/2 + 1 = 5 Bytes
Odd PIC – (N + 1)/2 for exam – (5 + 1)/2 = 3 bytes
COMP-4 and COMP-5:
Not generally used, these are also used for Binary representation.
SYNC or SYNCHRONIZED Clause:
This clause is used to store values at word boundaries. It is used with COMP, COMP-1 & 2. This is faster processing of the data.
01 TEST-DATA.
05 TS-DATA1 PIC X(06).
05 TS-DATA2 PIC 9(06) COMP SYNC.
Here TS-DATA1 is stored from 0 to 5th byte and TS-DATA2 is stored from 8th to 11th byte. 6th and 7th byte will be unused.
Value Clause:
It is used to initialize values to variables in WORKING-STORAGE SECTION.
01 WS-ACCT-NO PIC 9(12) VALUE 123478906789.
01 WS-CREDIT-CARD PIC X(20) VALUE 'AMERICAN EXPRESS'.
01 WS-ACCT-NAME PIC X(20) VALUE 'SANDIP'.
@Sandip Walsinge
----------------------------------------------------------------------------------
Clauses in COBOL:
JUSTIFIED RIGHT Clause:
REDEFINES Clause:
RENAMES Clause:
Renames clause is used to regroup of elementary data items.
Same memory location is shared by both groups.
It does not have PIC clause.
3) MULTIPLY:
4) DIVIDE:
5) COMPUTE:
DISPLAY Statement :
77 – Elementary data item.
66 – Renames clause – Will be covered in Clauses.
88 – Conditional name
01 EID PIC X(05) Elementary Data Item
01 EMP-INFO. Group data item
05 ENAME PIC X(10). Elementary data item
05 ESAL PIC 9(06). Elementary data item
77 EADD PIC X(20) Elementary data item.
01 CREDIT-CARD PIC X(01).
88 GOLD-CARD VALUE ‘G’. - Conditional names
88 PLATINUM-CARD VALUE ‘P’. - Conditional names
88 TITANIUM-CARD VALUE ‘T’. - Conditional names
Variable Names:
Variable names are nothing but user-defined words which are covered earlier.
FILLER :
If any selected field is not used in program then it can be defined as FILLER. It does not take part in any operation or can not be initialized.
01 EMP-INFO.
05 EID PIC X(05).
05 FILLER PIC X(01).
05 ENAME PIC X(10).
05 FILLER PIC X(64).
PICTURE Clause:
9 – Numeric variable 01 ACCT-NO PIC 9(16).
X – Alphanumeric 01 ACCT-ADD PIC X(26).
A – Alphabetic 01 ACCT-NAME PIC A(10).
V – Assumed Decimal 01 TRANS-AMT PIC 9999V99. 1234.89191
P – Positioned decimal 01 TRANS-AMT PIC 9999P. 1234
S – Signed 01 TRANS-AMT PIC S9(04). -1234.
Program for PICTURE CLAUSE:
Output of Program:
Numeric Editing characters:
Always for display variables
Z – Suppress leading zeros. PIC ZZZ9 0005 == > bbb5 (b is space character)
$ - Currency sign PIC $9999 0005 == > $0005
* - Asterisk PIC *9999 0005 == > *0005
-, +
DB, CR
Program for EDITING CHARACTERS:
Output of Program:
Usage Clause:
Usage clause defines how data to be stored in memory.
DISPLAY,
COMP
COMP-1
COMP-2,
COMP-3,
COMP-4,
COMP-5
DISPLAY Usage Clause:
This is simple display format.
01 EID PIC 9(04). - Display format. 1234.
COMP:
Data is stored in Binary format.
01 SEQ-NO PIC 9(04) COMP.
9(01) To 9(04) COMP – 2 Bytes - Half word ( 8 Bits – 2 Nibbles - 1 Byte)
9(05) To 9(09) COMP – 4 Bytes – full word ( 4 Bytes - one word)
9(10) To 9(18) COMP – 8 Bytes – Double word ( 8 Bytes – double word)
COMP-1:
Single word floating point. (4 Bytes), for large numbers this is used. Leftmost bit is used for sign 7 bits of 1 bytes are used to store exponent and last 3 bytes are used to store mantissa. PIC is not defined in this clause.
01 VEL-OF-LIGHT COMP-1.
[ 3 * 10 rest to power 8 (3 – mantissa , 10 8 – Exponent) ]
COMP-2:
Double word floating point. 7 Bytes are for mantissa. Used for high precision calculation.
01 LIGHT-YEARS COMP-2.
COMP-3:
Packed decimal field. This usage is used for better utilization of memory as it stores two digits in one byte. Sign is stored in last nibble (last half byte). Value of this field can be seen by HEX ON.
C in last nibble indicates Plus (+)
D in last nibble indicates Minus (-)
01 TRANS-AMT PIC 9(08) COMP-3 – 5 Bytes
01 TRANS-AMT1 PIC 9(05) COMP-3 – 3 bytes
Calculation of bytes for comp-3:
Even PIC – N/2 + 1 for exam – 8/2 + 1 = 5 Bytes
Odd PIC – (N + 1)/2 for exam – (5 + 1)/2 = 3 bytes
COMP-4 and COMP-5:
Not generally used, these are also used for Binary representation.
SYNC or SYNCHRONIZED Clause:
This clause is used to store values at word boundaries. It is used with COMP, COMP-1 & 2. This is faster processing of the data.
01 TEST-DATA.
05 TS-DATA1 PIC X(06).
05 TS-DATA2 PIC 9(06) COMP SYNC.
Here TS-DATA1 is stored from 0 to 5th byte and TS-DATA2 is stored from 8th to 11th byte. 6th and 7th byte will be unused.
Value Clause:
It is used to initialize values to variables in WORKING-STORAGE SECTION.
01 WS-ACCT-NO PIC 9(12) VALUE 123478906789.
01 WS-CREDIT-CARD PIC X(20) VALUE 'AMERICAN EXPRESS'.
01 WS-ACCT-NAME PIC X(20) VALUE 'SANDIP'.
@Sandip Walsinge
----------------------------------------------------------------------------------
Clauses in COBOL:
SIGN Clause:
SIGN Clause is used to store sign separately for signed variables. It will take
extra byte to store sign. It can’t be used with 66 or 88 level number.
SIGN IS { LEADING \ TRAILING } [ SEPARATE CHARACTER ]
01 TRANS-AMT PIC S9(04) SIGN LEADING
SEPARATE CHARACTER.
SEPARATE CHARACTER.
Let’s say value of TRANS-AMT is plus 2550 then it will display +2550 and it will
take 5 bytes to store this value.
If SIGN clause is not specified then it will take 4 bytes to store value.
NOTE: "S" should be always defined in PIC clause.
SIGN LEADING Program:
If SIGN clause is not specified then it will take 4 bytes to store value.
NOTE: "S" should be always defined in PIC clause.
SIGN LEADING Program:
JUSTIFIED RIGHT Clause:
This clause is used for alphanumeric or alphabetic variables. These variables
are stored or received from left to right. In order to reverse this order
JUSTIFIED RIGHT is used.
01 EMP-NAME PIC X(15) JUSTIFIED RIGHT.
MOVE ‘SANDIP’ TO EMP-NAME == > bbbbbbbbbSANDIP (b is space)
Note: Numeric variables are RIGHT justified, means values are stored or
received from right to left.
received from right to left.
REDEFINES Clause:
Same memory can be used by multiple variables. Redefining variable should be
immediately followed by redefined variable and level number should be same.
Size of both variables need not to be same. It can’t be used with 66 and 88
levels, also not used in 01 level file section.
01 WS-DT PIC 9(08).
01 WS-DATE REDEFINES WS-DT.
05 WS-YR PIC X(04).
05 WS-MN PIC X(02).
05 WS-DY PIC X(02).
RENAMES Clause:
Renames clause is used to regroup of elementary data items.
Same memory location is shared by both groups.
It should be declared at 66 level number,
should not be defined at 01,77 or 88 level.
should not be defined at 01,77 or 88 level.
It should not be used with occurrence in an array.
Renamed variable should be immediately followed by last
variable of renaming group.
variable of renaming group.
It does not have PIC clause.
Example of Renames Clause:
01 WS-A.
05 WS-DATA1 PIC X(05).
05 WS-DATA2 PIC X(03).
05 WS-DATA3 PIC X(05).
05 WS-DATA4 PIC X(03).
66 WS-B RENAMES WS-DATA1 THRU WS-DATA2.
There are five arithmetic verbs in COBOL
1) ADD
2) SUBTRACT
3) MULTIPLY
4) DIVIDE
5) COMPUTE
1) ADD:
Below are the syntaxes for ADD verb.
ADD A TO B == >
A=7 B=3 THEN B=10
ADD A B C TO D == >
A=2 B=3 C=4 D=2 THEN D=11
ADD 15 A TO B == >
15, A=2 B=2 THEN B=19
ADD A, B GIVING C == >
A=2 B=3 C=9 THEN C=5 (override value of C)
ADD A, B GIVING C, D, E == >
A=2 B=3 C=1 D=1 E=1 THEN C=5 D=5 E=5
Below are the syntaxes for SUBTRACT verb.
SUBTRACT A FROM B == >
A=2 B=7 THEN B=5, A=2 Value of B will change
SUBTRACT A B FROM C == >
A=2 B=3 C=7 THEN C=7-(2+3)=2
SUBTRACT A B FROM C GIVING D == >
A=2 B=3 C=7 D=30 THEN C=7 D=7-(2+3)=2
Here value of C will not change D will change
SUBTRACT 15 FROM A B == >
A=20 B=30 THEN A=20-15=5 B=30-15=15
3) MULTIPLY:
MULTIPLY A BY B == >
A=2 B=3 THEN B=6
MULTIPLY A BY B GIVING C == >
A=2 B=3 C=20 THEN C=6
MULTIPLY A BY B C D == >
A=2 B=3 C=4 D=5 THEN B=6 C=8 D=10
MULTIPLY A BY B C GIVING D E == >
A=2 B=3 C=4 D=20 E=30 THEN D=6 E=8
4) DIVIDE:
DIVIDE 5 INTO A == >
IF A=20 THEN A=4
DIVIDE 5 INTO A GIVING B == >
IF A=20 THEN B=4 Value of A will not change
DIVIDE 3 INTO A GIVING B C == >
IF A=9 THEN B=3 C=3 value A will not change
DIVIDE A BY 3 GIVING C == >
IF A=21 THEN C=7
DIVIDE A INTO B GIVING C REMAINDER D == >
IF A=7 B=22 THEN C=3 D=1
5) COMPUTE:
All the operations which can be performed by ADD, SUBTRACT, MULTIPLY or
DIVIDE it can be performed by COMPUTE. We can use below operators in
compute statement.
+ Add
- Subtract
/ Divide
* Multiply
** exponentiation (for example 2**3=8)
The order in which operations will be performed:
1) : **
2) * or / - if both operators are present then from left to right
3) + or - - if both operators are present then from left to right
Note: If any operators are given in Brackets then it will execute first.
It will override above order.
COMPUTE A = B + C
B=2 C=3 A=5
COMPUTE A = B * C
B=2 C=3 A=6
COMPUTE A = (B + C) * 3
B=2 C=3 5 * 3 A=15
----------------------------------------------------------------------------------
MOVE STATEMENT:
Move statement is used to transfer data between internal storage area defined
either in File section or working-storage section.
Syntax:
MOVE indetifier1/literal/figurative constant TO identier2 (identifer3)
MOVE WS-AMT TO FI-AMT
MOVE ‘GOLD’ TO WS-CRED-TYPE
MOVE ZEROS TO TRANS-AMOUNT
MOVE WS-AMT TO FI-AMT WS-BAL-AMT
Movement of Numeric fields:
Numeric values are moved from right to left. For decimals data is moved from
left to right and data before decimal is moved from right to left.
01 WS-AMT PIC 9(04).
MOVE 123456 TO WS-AMT == >
3456 (Leftmost bytes are truncated)
MOVE 12 TO WS-AMT == >
0012 (leftmost bytes are filled with zero)
01 WS-BAL-AMT PIC 9(04)V99.
MOVE 123456.7891 TO WS-BAL-AMT == > 3456.78
MOVE 12.7 TO WS-BAL-AMT == > 0012.70
Alphanumeric or Alphabetic data is moved from left to right.
01 WS-LANG-NAME PIC X(03).
MOVE ‘COBOL’ TO WS-LANG-NAME == >
COB (Rightmost bytes are truncated)
MOVE ‘C’ TO WS-LANG-NAME == >
Cbb (Rightmost bytes are filled with spaces)
MOVE CORRESPONDING:
This move statement is used to move value from one group to other, but value
will be moved for variables which have same names.
Syntax:
MOVE CORRESPONDING/CORR identifer1 TO identifier2
01 WS-GRP1.
05 WS-A PIC X(02) VALUE ‘11’.
05 WS-B PIC X(02) VALUE ‘22’.
05 WS-C PIC X(02) VALUE ‘33’.
05 WS-Z PIC X(02) VALUE ‘44’.
01 WS-GRP2.
05 WS-A PIC X(02).
05 WS-C PIC X(02).
05 WS-B PIC X(02).
05 WS-X PIC X(02).
MOVE CORRESPONDING:
MOVE CORRESPONDING WS-GRP1 TO WS-GRP2.
WS-GRP2 = 113322
REFERENCE MODIFICATION:
Reference modification is used to transfer or move partial data from one
variable to other. In reference modification (X:Y)
X Indicates Starting position
Y Indicates length of the field. (Y is optional)
01 WS-LANG1 PIC X(05) VALUE ‘COBOL’.
01 WS-LANG2 PIC X(05).
MOVE WS-LANG1(1:3) TO WS-LANG2 == >
WS-LANG1 = COB
01 WS-DATA1 PIC X(05) VALUE ‘COBOL’.
01 WS-DATA2 PIC X(08).
MOVE WS-DATA1(1:4) TO WS-DATA2 == >
WS-DATA2 = COBObbbb (b is space)
REFERENCE MODIFICATION for Partial Display:
It is also used to display partial data.
01 WS-DATA1 PIC X(20) VALUE ‘COBOL MAINFRAMES’.
DISPLAY ‘ VALUE OF REF MOD :’ WS-DATA1(01:10)
This will display value as VALUE OF REF MOD :COBOL MAIN
ROUNDED OPTION:
This option will round the value as per PIC clause of variable.
01 WS-A PIC 9(02)V99 VALUE 12.34.
01 WS-B PIC 9(02)V99 VALUE 23.35.
01 WS-C PIC 9(02)V9.
ADD WS-A TO WS-B GIVING WS-C ROUNDED. == >
WS-C = 35.7 (35.69 rounded to 35.7)
COMPUTE WS-C ROUNDED = WS-A + WS-B == >
WS-C 35.7
If WS-C PIC 9(02)V99 in that case it will not be rounded to 35.7
it will be 35.69 only.
it will be 35.69 only.
Note:
If last digit is >= 5 Then add 1 35.69 == > 35.7
If last digit is >= 5 Then add 1 35.69 == > 35.7
If last digit is < 5 Then keep as it is 35.64 == > 35.6
ON SIZE ERROR OPTION:
In order to avoid size errors in arithmetic operations ON SIZE ERROR OPTION is
used.
01 WS-AMT1 PIC 9(02) VALUE 90.
01 WS-AMT2 PIC 9(02) VALUE 30.
ADD WS-AMT1 TO WS-AMT2 == >
WS-AMT2 = 20
(Here it will not throw any syntax error it will execute but value WS-AMT2 is not
correct, in order to avoid such scenarios ON SIZE is used.
(Here it will not throw any syntax error it will execute but value WS-AMT2 is not
correct, in order to avoid such scenarios ON SIZE is used.
ADD WS-AMT1 TO WS-AMT2
ON SIZE ERROR PERFORM ERROR-PARA.
ERROR-PARA.
DISPLAY ‘ERROR HAS OCCURRED IN ADDITION’.
Here it will not perform addition, but it will display ERROR HAS OCCURRED IN
ADDITION
INITIALIZE Verb:
Value clause is used to initialize value in WORKING-STORAGE SECTION but
INITIALIZE verb is used in PROCEDURE DIVISION to initialize value to variables.
It will set SPACES to Alphanumeric and Alphabetic variables and ZEROS to
numeric variables.
WORKING-STORAGE SECTION.
01 WS-DATA.
05 WS-A PIC 9(05) VALUE 12345.
05 WS-B PIC X(05) VALUE ‘COBOL’.
PROCEDURE DIVISION.
INITIALIZE WS-DATA. This will set ZEROS to WS-A and SPACES to WS-B.
INITIALIZE WS-A WS-B.
ACCEPT Statement:
This verb or keyword is used to pull data either from JCL or system information
like DATE, TIME, DAY. If FROM is defined in ACCEPT it means that it is fetching
system information like DATE, TIME. If FROM is not present then it is fetching
data from JCL SYSIN statement.
ACCEPT WS-EMP-CODE.
DISPLAY ‘ WS-EMP-CODE ‘ WS-EMP-CODE == >
(COBOL)
ACCEPT WS-DATE FROM DATE (DATE is system date).
DISPLAY ‘ VALUE OF DATE ‘ WS-DATE == > 200407 (YYMMDD)
DISPLAY Statement :
This is used to display value of variable or to display messages.
DISPLAY ‘ VALUE OF WS-EMP-CODE IS ‘ WS-EMP-CODE.
DISPLAY ‘ ERROR HAS OCCURRED ‘.
STOP RUN :
STOP RUN is last executable statement of Main program. It refers ending of
main program.
EXIT PROGRAM:
This is last executable statement of sub-program. It returns control to main
program.
GOBACK:
GOBACK can be coded in main as well as in sub-programs. GOBACK returns
control from where it was transferred.
It is good practice to code GOBACK only in sub-programs.
STANDARD PRACTICE:
MAIN PROG – STOP RUN
SUB PROG - GOBACK
@Sandip Walsinge
----------------------------------------------------------------------------------
CONDITIONS in COBOL:
There are five types of conditions in COBOL
1) Relational Condition
2) Sign Condition
3) Class Condition
4) Conditional Name
5) Compound Condition
1) Relational Condition:
Syntax:
Operand-1 condition operand-2 (Operand can be identifier or Literal)
Operator used here are <, >, =, NOT, EQUAL TO, LESS THAN, GREATER THAN
Conditions are written as below -
1) IS [NOT] GREATER THAN
2) [NOT] >
3) IS [NOT] LESS THAN
4) [NOT] <
5) IS [NOT] EQUAL TO
6) [NOT] =
7) <> (not equal to)
IF WS-A IS NOT GREATER THAN WS-B
DISPLAY ‘ WS-A IS NOT GREATER THAN WS-B’
ELSE
DISPLAY ‘WS-A IS GREATER THAN WS-B’
END-IF
IF WS-A = WS-B
DISPLAY ‘ WS-A IS EQUAL TO WS-B’
ELSE
DISPLAY ‘WS-A IS NOT EQUAL TO WS-B’
END-IF.
2) SIGN Condition:
Syntax:
{Identifier/arithmetic operation} IS [NOT] [POSITIVE/NEGATIVE/ZERO]
01 WS-A PIC S9(05) VALUE -1234.
IF WS-A IS POSTIVIE
DISPLAY ‘WS-A IS POSITIVE’
ELSE
DISPLAY ‘WS-A IS NEGATIVE’
END-IF.
IF WS-A - WS-B IS NEGATIVE
DISPLAY ‘ SUBTRACTION IS NEGATIVE’
END-IF.
3) Class Condition:
Syntax:
{Identifier} IS [NOT] [NUMERIC/ALPHABETIC]
01 WS-A PIC X(05) VALUE ‘COBOL’.
IF WS-A IS NUMERIC
DISPLAY ‘WS-A IS NUMERIC’
ELSE
DISPLAY ‘WS-A IS NOT NUMERIC’
END-IF.
IF WS-A IS ALPHABETIC
DISPLAY ‘ WS-A IS ALPHABETIC’
ELSE
DISPLAY ‘WS-A IS NOT ALPHABETIC’
END-IF.
4) Conditional Name:
01 WS-CREDIT-TYPE PIC X(01).
88 GOLD VALUE ‘G’. Conditional Name
88 PLATINUM VALUE ‘P’. Conditional Name
88 TITIANIUM VALUE ‘T’. Conditional Name
IF GOLD
DISPLAY ‘ CREDIT CARD IS GOLD’
ELSE
IF PLATINIUM
DISPLAY ‘ CREDIT CARD IS PLATINIUM’
ELSE
DISPLAY ‘CREDIT CARD IS TITIANIUM’
END-IF
END-IF.
5) Compound Condition:
In compound condition “AND or “OR” operators are used.
AND [ T T T
T F F
F T F
F F F]
OR [ T T T
T F T
F T T
F F F]
01 EMP-NAME PIC X(05) VALUE ‘SHYAM’.
01 EMP-DEPT PIC X(03) VALUE ‘ICT’.
IF EMP-NAME = ‘SHYAM’ AND EMP-DEPT = ‘ICT’
DISPLAY ‘ SHYAM IS IN ICT’
ELSE
DISPLAY ‘SHYAM IS NOT IN ICT’
END-IF.
IF Statement:
If Condition [statement-1/NEXT SENTENCE/CONTINUE]
Else [statement-2/NEXT SENTENCE/CONTINUE]
NEXT SENTENCE passes control to next sentence.
CONTINUE is Null statement it just passes control to next immediate statement.
01 WS-EMP PIC X(05) VALUE ‘SHYAM’.
IF WS-EMP = ‘SHYAM’
CONTINUE
ELSE
DISPLAY ‘ EMP NAME IS SHYAM’
END-IF
MOVE WS-A TO WS-B (Continue will transfer control here)
DISPLAY ‘ THIS IS END RESULT’
01 WS-EMP PIC X(05) VALUE ‘SHYAM’.
IF WS-EMP = ‘SHYAM’
NEXT SENTENCE
ELSE
DISPLAY ‘ EMP NAME IS SHYAM’
END-IF
MOVE WS-A TO WS-B
ADD WS-AMT TO WS-BAL-AMT
MOVE WS-BAL-AMT TO WS-TRANS-AMT.
DISPLAY ‘ THIS IS NEXT SENTENCE’ (control will be passed here)
Nested if statement means multiple If conditions are coded in one If statement.
Already covered in the example of conditional names.
----------------------------------------------------------------------------------
Evaluate Statement:
EVALUATE statement is used to check conditions. It replaces multiple If conditions
efficiently and effectively. After execution of EVALUATE statement control
automatically transfers to statement followed by END-EVALUATE.
Syntax of Evaluate:
01 WS-CREDIT-TYPE PIC X(01).
88 GOLD VALUE ‘G’.
88 PLATINUM VALUE ‘P’.
88 TITIANIUM VALUE ‘T’.
MOVE ‘T’ TO WS-CREDIT-TYPE
EVALUATE WS-CREDIT-TYPE
WHEN ‘G’
DISPLAY ‘ CREDIT CARD IS GOLD’
WHEN ‘P’
DISPLAY ‘ CREDIT CARD IS PLATINUM’
WHEN ‘T’
DISPLAY ‘ CREDIT CARD IS TITANIUM’
WHEN OTHER
DISPLAY ‘ CREDIT CARD IS NONE OF ABOVE’
END-EVALUATE.
RESULT: CREDIT CARS IS TITANIUM
Examples of Evaluate Statement:
01 WS-GENDER PIC X(01).
01 WS-AGE PIC 9(02).
01 WS-INCOME PIC 9(06).
EVALUATE TRUE ALSO WS-GENDER ALSO WS-AGE
WHEN WS-INCOME < 250000 ALSO M ALSO 20 THRU 60
DISPLAY ‘INCOME TAX RATE IS ZERO’
WHEN WS-INCOME >= 2,50,001 AND < 5,00,00 ALSO M ALSO 20 THRU 60
DISPLAY ‘INCOME TAX RATE IS 5%’
WHEN WS-INCOME >= 5,50,001 AND < 10,00,00 ALSO M ALSO 20 THRU 60
DISPLAY ‘INCOME TAX RATE IS 20%’
END-EVALUATE.
EXIT Statement:
It is a Cobol keyword which does nothing. It is used to indicate end of paragraphs.
PROCEDURE DIVISION.
COMPUTE-PARA.
MOVE 20 WS-A
MOVE 30 WS-B
COMPUTE WS-C = WS-A + WS-B
DISPLAY ‘VALUE OF WS-C’ WS-C
EXIT.
GO TO Statement:
GO TO Transfers control to the respected para which is provided in the instructions but control will
never return to statement followed by GO TO statement. That’s why it is not advisable to use GO TO
in top down programming approach.
PROCEDURE DIVISION.
MOVE 20 TO WS-A
MOVE 30 TO WS-B
GO TO COMPUTE-PARA
DISPLAY ‘VALUE OF ADDITION IS’ WS-C. (This will not execute)
STOP RUN. (This will not execute)
COMPUTE-PARA.
COMPUTE WS-C = WS-A + WS-B
PERFORM Statement:
PERFORM is used to execute either set of instructions or Programs, sections.
It transfers control after execution of PERFORM to next statement followed by
PERFORM.
Syntax1: perform once
PERFORM [ procedure-name-1 [{ THROUGH/THRU} procedure-name-2]]
[imperative-statement-1 END-PERFORM]
OUTLINE PERFORM:
PERFORM PARA-1.
PERFORM PARA-1 THRU PARA-4.
INLINE PERFORM:
PERFORM
DISPLAY ‘THIS IS INLINE PERFORM’
MOVE ‘SHYAM’ TO WS-EMP-NAME
END-PERFORM
Syntax 2: perform n times
PERFORM [ procedure-name-1 [{ THROUGH/THRU } procedure-name-2 ]]
{ identifier-1/integer-1 } TIMES
[ imperative-statement-1 END-PERFORM ]
01 WS-A PIC 9(02) VALUE 20.
OUTLINE PERFORM:
PERFORM DISP-PARA WS-A TIMES.
PERFORM PARA-1 THRU PARA-4 10 TIMES.
INLINE PERFORM:
PERFORM 4 TIMES
DISPLAY ‘THIS IS INLINE PERFORM’
MOVE ‘SHYAM’ TO WS-EMP-NAME
END-PERFORM
Syntax 3: perform until
PERFORM [ procedure-name-1 [{ THROUGH/THRU } procedure-name-2 ]]
[ WITH TEST { BEFORE/AFTER } ]
UNTIL condition-1
[ imperative-statement-1 END-PERFORM ]
01 WS-A PIC 9(02) VALUE 01.
PERFORM PARA-1 UNTIL WS-A > 5
PERFORM PARA-1 THRU PARA-4 UNTIL WS-B > 5
PERFORM PARA-1 WITH TEST AFTER UNTIL WS-A > 5
PERFROM PARA-1 WITH TEST BEFORE UNTIL WS-A > 5
NOTE: TEST BEFORE is Default option.
When condition is false it will execute the code, if it is true it will come out.
Syntax 4:
PERFORM [ procedure-name-1 [{ THROUGH/THRU } procedure-name-2 ]]
[ WITH TEST { BEFORE/AFTER } ]
VARYING { identifier-1 /index-1 }
FROM { identifier-2/index-2/literal-1 }
BY { identifier-3/literal-2 }
UNTIL condition-1
AFTER { identifier-4 /index-3 }
FROM { identifier-5/index-4/literal-3 }
BY { identifier-6/literal-4 }
UNTIL condition-2
[ imperative-statement-1 END-PERFORM ]
01 WS-A PIC 9(02) VALUE 01.
OUTLINE PERFORM:
PERFORM DISP1-PARA
WITH TEST BEFORE
VARYING WS-A FROM 1 BY 1
UNTIL WS-A > 5.
PERFORM DISP1-PARA
WITH TEST BEFORE
VARYING WS-A FROM 1 BY 1
UNTIL WS-A > 5
AFTER WS-B FROM 1 BY 1.
UNTIL WS-B > 5.
----------------------------------------------------------------------------------
Table Handling:
Table or Array is set of similar data items and which has similar data
definition.
In Cobol internal tables or Arrays are defined by using OCCURS clause.
Maximum 7 dimension tables are possible in Cobol language.
OCCURS Clause:
Occurs clause is used to define occurrences of an array. It specifies how many times
data item to be repeated.
1) Occurs clause should not be defined on 01,66,77,88 level numbers.
2) PIC clause may not be always defined with Occurs clause.
3) Data from Array is accessed either with index or subscript.
Maximum bytes that can be stored in Array:
With ADDR(32): 999,999,999 bytes
With ADDR(64): 2,147,483,646 bytes
WORKING-STORAGE SECTION.
01 WS-WEEK. (Single dimensional Array)
05 WS-DATA OCCURS 7 TIMES.
10 DAY-NAME PIC X(04).
01 WS-SUBS PIC 9(02). (WS-SUBS is Subscript)
Above table can store below data:
SUN MON TUE WED THI FRI SAT
Example of Two Dimensional Array:
01 COMP-DATA.
05 DEPT OCCURS 2 TIMES
INDEXED BY INX1.
10 DEPT-NAME PIC X(10).
10 EMP OCCURS 3 TIMES
INDEXED BY INX2.
15 EMP-NAME PIC X(07).
Here INX1 and INX2 are indexes
FINANCE ADMIN
SHYAM ASHWIN
ROHIT PRAVIN
NITIN ANKIT
Difference Between Index and Subscript:
Index Subscript
No Need to define explicitly, it is Need to define explicitly as a variable in
defined by Indexed by Clause Working-Storage Section.
It is displacement It is Occurance
Faster and efficient Slower
It can't be used in arithmetic It can be used in Arithmetic operations and
operations and display statement. display statement.
This can be modified by SET verb, Subscript can be modified by any
SEARCH, PERFORM statements arithmetic operation.
Store data in Array by using PERFORM:
WORKING-STORAGE SECTION.
01 WS-YEAR.
05 WS-MONTHS OCCURS 12 TIMES
PIC X(04).
01 WS-SUBS PIC 9(02) VALUE ZERO.
PERFORM ADD-PARA UNTIL WS-SUBS > 11
Statement to change value of Index:
Syntax:
SET index TO indentifier1/liter-1
SET WS-INDEX TO 1
SET WS-INDEX UP BY 1
SET WS-INDEX DOWN BY 1
Statement to change value of Subscript:
ADD 1 TO WS-SUBSCRIPT
MOVE 1 TO WS-SUBSCRIPT
COMPUTE WS-SUBSCRIPT = WS-A + WS-B
SEARCH in COBOL:
There are two types of search in Cobol program Sequential Search(SEARCH)
and Binary Search(SEARCH ALL).
Sequential Search:
This is sequential search and can be invoked by SEARCH verb.
Data is searched sequentially.
Syntax of SEARCH:
SEARCH identifier-1 [ VARYING { identifier-2 / index-1 } ]
[ AT END imperative-statement-1 ]
{ WHEN condition-1
[END-SEARCH]
NOTE: Identifier-1 is variable name defined by occurs clause.
Sequential Search:
WORKING-STORAGE SECTION.
01 WS-WEEK.
05 WS-DAYS OCCURS 7 TIMES
INDEXED BY INX
PIC X(04).
01 WS-DATA PIC X(28)
VALUE 'SUN MON TUE WED THI FRI SAT ‘.
PROCEDURE DIVISION.
MOVE WS-DATA TO WS-WEEK.
SET INX TO 1
SEARCH WS-DAYS
AT END
DISPLAY 'RECORD NOT FOUND'
WHEN WS-DAYS(INX) = 'MON'
DISPLAY 'DAY IS MONDAY'
END-SEARCH.
Binary Search(SEARCH ALL):
This is Binary search, data is searched dynamically. This search is invoked by
SEARCH ALL. Data should be in sorted order. No need to set index.
Syntax of SEARCH ALL:
SEARCH identifier-1
[ AT END imperative-statement-1 ]
WHEN condition-1 imperative-statement-2/ NEXT SENTENCE
[END-SEARCH]
NOTE: Identifier-1 is variable name defined by occurs clause.
Binary Search(SEARCH ALL):
WORKING-STORAGE SECTION.
01 WS-WEEK.
05 WS-DAYS OCCURS 7 TIMES
ASCENDING KEY IS (ASCENDING or DESCENDING
DAYS-NAME can be used)
INDEXED BY INX.
10 DAYS-NAME PIC X(04).
01 WS-DATA PIC X(28)
VALUE 'FRI MON SAT SUN THU TUE WED ‘.
PROCEDURE DIVISION.
MOVE WS-DATA TO WS-WEEK.
SEARCH ALL WS-DAYS
AT END
DISPLAY 'RECORD NOT FOUND'
WHEN DAYS-NAME(INX) = 'MON'
DISPLAY 'DAY IS MONDAY'
END-SEARCH.
----------------------------------------------------------------------------------
Called Program, Sub-programs:
When a specific functionality need to be executed more than once then it’s better to
write it separately and call it from different programs.
There are two types of programs main(Calling) and sub(Called) programs.
Main Programs:
This programs have CALL statements to call subprograms.
STOP RUN is used to end the program.
Syntax:
CALL identifier-1/literal-1 USING identifier-2, identifier-3.
CALL ‘SUBPROG’ USING identifier-1, identifier-2. (Call by Reference)
CALLL ‘SUBPROG’ USING WS-A, WS-B.
CALL WS-PROG USING WS-A.
CALL ‘SUBPROG’ USING BY CONTENT identifier-1 (Call by Content)
BY CONTENT identifier-2.
CALL ‘SUBPROG’ USING BY CONTENT WS-A,
BY CONTENT WS-B.
CALL WS-PROG USING BY CONTENT WS-A
BY CONTENT WS-B.
Subprograms:
Subprogram or Called programs are called from main programs in order to execute
one functionality in multiple programs.
This program has LINKAGE SECTION to define variables which receive data from
main programs(Calling programs).
USING identifier-1, identifier-2 is used in PROCEDURE DIVISION to receive values
from main programs.
PROCEDURE DIVISION USING WS-A, WS-B.
GOBACK or EXIT PROGRAM is used to stop the program. (It returns control to
main program)
Call By Reference:
When subprogram is called by Call by Reference then values which are sent from
Main program to subprogram will be changed while receiving it from sub program
to main program.
Ex:
MAINPROG
WORKING-STORAGE SECTION.
01 WS-A PIC 9(04) VALUE 1234.
PROCEDURE DIVISION.
CALL ‘SUBPROG’ USING WS-A
SUBPROG
WORKING-STORAGE SECTION.
01 WS-VAL PIC 9(04) VALUE 6789.
LINKAGE SECTION.
01 WS-RC-A PIC 9(04) VALUE ZEROS.
PROCEDURE DIVISION USING WS-RC-A.
MOVE WS-VAL TO WS-RC-A.
VALUE OF WS-A == > 6789
Call By Content:
When subprogram is called by Call by Content then values which are sent from Main
program to subprogram will not be changed while receiving it from sub program to
main program.
Ex:
MAINPROG
WORKING-STORAGE SECTION.
01 WS-A PIC 9(04) VALUE 1234.
PROCEDURE DIVISION.
CALL ‘SUBPROG’ USING BY CONTENT WS-A.
SUBPROG
WORKING-STORAGE SECTION.
01 WS-VAL PIC 9(04) VALUE 6789.
LINKAGE SECTION.
01 WS-RC-A PIC 9(04) VALUE ZEROS.
PROCEDURE DIVISION USING WS-RC-A.
MOVE WS-VAL TO WS-RC-A.
VALUE OF WS-A == > 1234
Static and Dynamic Call:
Subprograms are called by two ways Static & Dynamic call.
Static Call:
Sub Program is called by using Literal(program name). Compiler option NODYNAM.
CALL ‘SUBPROG’ USING WS-A
Dynamic Call :
Sub Program is called by using identifier(variable). Compiler option is DYNAM.
01 WS-PROG PIC X(08) VALUE ‘SUBPROG’.
CALL WS-PROG USING WS-A.
NOTE: Compiler options provided explicitly will override Call statements.
BY Default compiler option is NODYNAM.
Static Call Dynamic Call
This is called by Literal. This is called by Identifier.
NODYNAM Compiler option DYNAM compiler option is used.
is used.
Both Main and sub program Load Both load modules are stored
Modules are stored in one Load separately.
Module.
This is faster as both Load modules This is slower as both Load Modules
are stored in one Load Module. are stored separately.
When changes are made in sub
When changes are made in programs only sub programs need to
subprogram both main and sub be recompiled, no need to recompile
programs need to be recompiled. main program.
----------------------------------------------------------------------------------
String Handling:
In COBOL three verbs are used for string handling.
1) String
2) Unstring
3) Inspect
STRING:
String is used to concatenate two or more strings. Suppose there are three fields
first, middle and last name then String is used to concatenate all these three strings
in one.
Syntax:
STRING identifier-1/Literal-1, identifier-2/Literal-2
Delimited by (identifier-3/Literal-3/SIZE)
INTO identifier-4
END-STRING.
Example of String:
WORKING-STORAGE SECTION.
01 WS-FNAME PIC X(16) VALUE 'SANDIP,ABCD PUNE'.
01 WS-MNAME PIC X(16) VALUE 'VASANTRAO- PQRS'.
01 WS-LNAME PIC X(16) VALUE 'WALSINGE MUMBAI '.
01 WS-FULL-NAME PIC X(40).
PROCEDURE DIVISION.
STRING WS-FNAME DELIMITED BY ','
WS-MNAME DELIMITED BY '-'
WS-LNAME DELIMITED BY ' '
INTO WS-FULL-NAME
END-STRING.
UNSTRING:
UNSTRING is used to split one string into multiple strings. Let’s say full name can be
split into first, middle and last name.
Syntax:
UNSTRING identifier-1
[DELIMITED BY [identifier-2/literal-1/SIZE/SPACE]
OR [identifier-3/literal-2]
INTO identifier-4
DELIMITER IN identifier-5
COUNT IN identifier-6
identifier-7
DELIMITER IN identifier-8
COUNT IN identifier-9
END-STRING.
Example of UNSTRING:
WORKING-STORAGE SECTION.
01 WS-FL1 PIC X(10).
01 WS-FL2 PIC X(10).
01 WS-FL3 PIC X(10).
01 WS-DL1 PIC X(01).
01 WS-DL2 PIC X(01).
01 WS-DL3 PIC X(01).
01 WS-CT1 PIC 9(02).
01 WS-CT2 PIC 9(02).
01 WS-CT3 PIC 9(02).
01 WS-DATA PIC X(20) VALUE '24/04,2020-55’.
PROCEDURE DIVISION.
UNSTRING WS-DATA DELIMITED BY '/' OR ',' OR '-'
INTO WS-FL1 DELIMITER IN WS-DL1 COUNT IN WS-CT1
WS-FL2 DELIMITER IN WS-DL2 COUNT IN WS-CT2
WS-FL3 DELIMITER IN WS-DL3 COUNT IN WS-CT3
END-UNSTRING
INSPECT:
It is used to find out occurrence of characters in data field.
Syntax:
INSPECT identifier-1 TALLYING identifier-2 FOR [ALL/LEADING]
identifier-3/literal-1 [BEFORE/AFTER] INITIAL identifier-4/literal-2.
Example:
01 MY-STRING PIC X(20) VALUE ‘ANANTA KUMAR MOHAN’.
01 TALLY-COUNT PIC 9(02).
INSPECT MY-STRING
TALLYING TALLY-COUNT == > TALLY-COUNT = 5
FOR ALL ‘A’.
INSPECT MY-STRING
TALLYING TALLY-COUNT == > TALLY-COUNT = 1
FOR LEADING ‘A’.
Example of INSPECT:
01 MY-STRING PIC X(20) VALUE ‘ANANTA KUMAR MOHAN’.
01 TALLY-COUNT PIC 9(02).
INSPECT MY-STRING
TALLYING TALLY-COUNT == > TALLY-COUNT = 18
FOR CHARACTERS.
INSPECT MY-STRING
TALLYING TALLY-COUNT == > TALLY-COUNT = 04
FOR ALL ‘A’
BEFORE INITIAL ‘MOHAN’.
INSPECT MY-STRING
TALLYING TALLY-COUNT == > TALLY-COUNT = 01
FOR ALL ‘A’
BEFORE INITIAL ‘MOHAN’
AFTER INITIAL ‘ANANTA’.
PARM Parameter:
This parameter is used to pass value from JCL step to Cobol.
If multiple values to be passed then that should be separated by Comma. Maximum
100 characters can be passed from PARM parameter.
//STEP1 EXEC PGM=TESTPGM,PARM=(COBOL)
LINKAGE SECTION.
01 PARM-DATA.
05 PARM-LENGTH PIC 9(04) COMP.
05 PARM-TEXT PIC X(10).
PROCEDURE DIVISION USING PARM-DATA.
DISPLAY ‘ VALUE OF PARM-TEXT ‘ PARM-TEXT.
Note: PARM-LENGTH – This field will store length of data passed from JCL step.
----------------------------------------------------------------------------------
File Handling :
File:
File is collection of records. Record is collection of fields.
There are five steps to process file in COBOL program.
1) Declaration
2) Layout Definition
3) Open File
4) Perform Operations
5) Close File.
1) Declaration:
File is declared in ENVIRONMENT DIVISION, INPUT-OUTPUT SECTION. FILE-
CONTROL.
ENVIRONMENT DIVISION.
INPUT-OUTPUT SECTION.
FILE-CONTROL.
SELECT file-name ASSIGN TO DDName
ORGANIZATION IS SEQUENTIAL/INDEXED/ RELATIVE
ACCESS MODE IS SEQUENTIAL/RANDOM/DYNAMIC
RECORD KEY IS FILE-KEY1
RELATIVE KEY IS WS-RRN
ALTERNATE RECORD KEY IS FILE-KEY2
FILE STATUS IS identifier-1
1) Declaration:
1.1) SELECT:
SELECT file-name ASSIGN TO DDName
File-name provided here will be used in Cobol program, it is logical file name which is not actual physical name of file.
DDName is from JCL where actual physical file name is specified in JCL.
SELECT INFILE ASSIGN TO DD1 (COBOL STATEMENT)
//DD1 DD DSN=FINANCE.INPUT.FILE,DISP=SHR (JCL STATEMENT)
1) Declaration:
1.2) ORGANIZATION:
There are three types of file organizations, SEQUENTIAL, INDEXED, RELATIVE.
1.2.1) SEQUENTIAL files are flat files and VSAM ESDS(Entry Sequenced Dataset) Files. It is processed sequentially only. It is default organization.
ORGANIZATION IS SEQUENIAL
1.2.2) INDEXED files are VSAM KSDS (Key Sequenced Dataset) files, key is
defined in this file to fetch data randomly or dynamically. It can be processed
sequentially too.
ORGANIZATION IS INDEXED
1.2.3) RELATIVE files are VSAM RRDS(Relative Record Dataset) files, relative
record number is allocated to each record to fetch data randomly or dynamically. It
can be processed sequentially too.
ORGANIZATION IS RELATIVE
1) Declaration:
1.3) ACCESS MODE:
There are three types of Access modes, SEQUENTIAL, RANDOM and DYNAMIC.
1.3.1) SEQUENTIAL:
File is processed sequentially. It is default access mode. Let’s say file has 100
records then all 100 records will be processed one by one in a sequence.
ACCESS MODE IS SEQUENTIAL
1.3.2) RANDOM:
File is processed randomly. Any record can be selected randomly by using KEY
from KSDS file or RRN (relative record number) from RRDS file. Let’s say 50th
record needs to be processed without processing first 49 records.
ACCESS MODE IS RANDOM
1.3.3) DYNAMIC:
File is processed dynamically. It is combination of sequential and random mode.
Let’s say want to process 50 to 100 record sequentially and 50th record should be accessed randomly. START and READ NEXT commands are used for this
operation.
ACCESS MODE IS DYNAMIC
1) Declaration:
1.4) RECORD KEY:
RECORD KEY IS EID ( This is primary key from KSDS)
1.5) ALTERNATE KEY:
ALTERNATE RECORD KEY IS ENAME (This is alternate index key from KSDS or
ESDS can’t be defined with RRDS)
FILE SECTION.
FD INFILE.
01 INREC.
05 EID PIC X(05). - This is Primary key in KSDS file
05 ENAME PIC X(10). - This is alternate index key
05 FILLER PIC X(65)
1.6) RELATIVE KEY:
RELATIVE KEY IS WS-RRN - This is relative record number defined for RRDS file.
WORKING-STORAGE SECTION.
01 WS-RRN PIC 9(02) VALUE 03.
1) Declaration:
1.7) FILE STATUS:
FILE STATUS IS FS1.
WORKING-STORAGE SECTION.
01 FS1 PIC X(02).
File status is stored in variable.
00 – successful operation
10 – end of file
There are many more file status codes.
2) Layout Definition:
Layout of file is defined in FILE SECTION OF WORKING-STORAGE SECTION.
DATA DIVISION.
FILE SECTION.
FD INFILE (FD – File Description)
RECORDING MODE IS F/FB/V/VB
RECORD CONTAINS A [TO B] CHARACTERS
BLOCK CONTAINS X [TO Y] CHARACTERS/RECORDS
LABEL RECORDS ARE OMITTED/STANDARD
DATA RECORD IS FILE-RECORD.
01 FILE-RECORD.
05 EID PIC X(05).
05 ENAME PIC X(10).
05 ECITY PIC X(10).
2) Layout Definition:
2.1) RECORDING MODE:
RECORDING MODE IS F/V - This specifies record format of file.
F - This is used for Fixed file
V - This is used for Variable file
2) Layout Definition:
2.2) RECORD CONTAINS:
RECORD CONTAINS A [TO B] CHARACTERS
This clause specifies length of record.
RECORD CONTAINS 80 CHARACTERS - This is for F OR FB file
RECORD CONTAINS 80 TO 120 CHRACTERS – This is used for V or VB file.
80 is minimum and 120 is maximum length.
2.3) BLOCK CONTAINS:
BLOCK CONTAINS X [TO Y] CHARACTERS/RECORDS
This clause specifies how many records will reside in one block.
BLOCK CONTAINS 800 RECORDS – One block will store 10 records 80 record
length.
BLOCK CONTAINS 0 RECORDS – System will decide optimum size and allocate
block size on it’s own.
2.4) LABEL RECORDS:
LABEL RECORDS ARE OMITTED/STANDARD
STANDARD is used for TAPE and DASD files.
OMITTED is used for Printer files.
2.5) DATA RECORD:
DATA RECORD IS FILE-RECORD
This specifies name of record.
DATA RECORD IS FILE-RECORD.
01 FILE-RECORD.
05 EID PIC X(05).
05 ENAME PIC X(10).
05 ECITY PIC X(10).
3) OPEN FILE:
OPEN FILE statement is defined in PROCEDURE DIVISION.
OPEN opening-mode Logical file-name
Below opening modes are used to open the file –
1) INPUT
2) OUTPUT
3) I-O (INPUT-OUTPUT)
4) EXTEND
3) OPEN FILE
3.1) INPUT MODE:
When file is used as input purpose only then file can be opened in INPUT mode.
Only for READ purpose.
OPEN INPUT file-name.
OPEN INPUT INFILE
3.2) OUTPUT MODE:
When file is used as OUTPUT purpose only to write data, file can be opened in
OUTPUT MODE. Only WRITE purpose.
OPEN OUTPUT file-name.
OPEN OUTPUT OUTFILE
3.3) I-O MODE (INPUT-OUTPUT):
When file is used as INPUT OR OUTPUT, file is opened on I-O MODE. This mode is
used for READ, WRITE, UPDATE OR DELETE operations.
OPEN I-O file-name.
OPEN I-O UPDFILE
3.4) EXTEND MODE:
This mode is used to append records in file. Let’s say file has 10 records and 5 more records needs to be added to the file after 10 records, then file is opened in EXTEND mode. Records are written by using WRITE operation.
OPEN EXTEND file-name.
OPEN EXTEND APDFILE
4) PERFORM OPERATIONS:
Below operations are provided in COBOL to perform different operations on file.
1) READ
2) WRITE
3) REWRITE
4) DELETE
5) START
6) READ NEXT
4.1) READ:
READ operation will read records from file. Single record can be read at a time.
Syntax:
READ file-name
[INTO file-record]
[KEY IS file-key]
[AT END imperative-statement]
[INVALID KEY imperative-statement]
[NOT AT END imperative-statement]
[NOT INVALID KEY imperative-statement]
END-READ.
Syntax for sequential file:
READ file-name
[INTO file-record]
[AT END imperative-statement]
[NOT AT END imperative-statement]
END-READ.
READ INFILE
INTO WS-INREC
AT END
SET EOF TO TRUE
NOT AT END
MOVE INREC TO OUTREC
END-READ.
4.1.1) READ INTO:
This statement reads record into working-storage record. It is preferred as it avoids multiple MOVE statements.
4.1.2) AT END:
It indicates end of the file
4.1.3) NOT AT END:
It indicates that file is not ended there are more records to read.
Syntax for Indexed or Relative file:
READ file-name
[INTO file-record]
[KEY IS file-key]
[INVALID KEY imperative-statement]
[NOT INVALID KEY imperative-statement]
END-READ.
READ INFILE
INTO WS-INREC
KEY IS EID
INVALID KEY
DISPLAY ‘ KEY IS INVALID ‘
NOT INVALID KEY
MOVE INREC TO OUTREC
END-READ.
READ INFILE
INTO WS-INREC
KEY IS EID
INVALID KEY
DISPLAY ‘ KEY IS INVALID ‘
NOT INVALID KEY
MOVE INREC TO OUTREC
END-READ.
4.1.1) KEY IS:
It specifies key of file indexed or relative.
4.1.2) INVALID KEY:
It specifies key is invalid, when key is invalid execute invalid key logic.
4.1.3) NOT INVALID KEY:
It specifies key is valid, execute valid key logic.
4.2) WRITE:
Write statement writes record in file. If files is opened in EXTEND mode then record will be appended. If file is opened in OUTPUT put then record will be written to current position.
Syntax of WRITE:
WRITE file-rec
[FROM ws-rec]
[INVALID KEY imperative statement]
[NOT INVALID KEY imperative statement]
END-WRITE.
4.3) REWRITE:
REWRITE is used to update record which is already present in the file. It is used
only in I-O MODE
Syntax of REWRITE:
REWRITE file-rec
[FROM ws-rec]
[INVALID KEY imperative statement]
[NOT INVALID KEY imperative statement]
END-REWRITE.
4.4) DELETE:
DELETE will delete record from file. To Delete record from file, file should be always opened only in I-O MODE.
Syntax of DELETE:
DELETE file-name
[INVALID KEY imperative statement]
END-REWRITE.
4.5) START:
START places a cursor on particular record on the basis of key in dynamic access
mode. START itself will not read any record, READ NEXT is used along with START command to read the record.
Syntax of START:
START file-name
KEY IS [EQUAL TO/NOT LESS THAN/GREATER THAN] file-key
INVALID KEY [imperative statement]
NOT INVALID KEY [imperative statement]
END-START
NOTE: START is always used with READ NEXT verb.
4.6) READ NEXT:
READ NEXT verb is used to read records sequentially after START command in
DYNAMIC mode. It will read the record at which cursor is placed based on the key.
Syntax of READ NEXT:
READ file-name NEXT RECORD
[INTO file-record]
[AT END imperative-statement]
[NOT AT END imperative-statement]
END-READ.
READ INFILE NEXT RECORD
INTO WS-REC
AT END
SET EOF TO TRUE
NOT AT END
DISPLAY ' RECORD READ ' WS-REC
END-READ
4.5 & 6) START AND READ NEXT combination examples:
INPUT DATA:
21400 SANDIP PUNE
21500 MANDIP MUMBAI
21600 RANDIP KOLKATA
21700 BALDIP CHENNAI
21800 KULJIT BANGLORE
PROGRAM CODE:
PROCEDURE DIVISION.
MOVE ‘21600’ TO EID
START INFILE
KEY IS EQUAL TO EID
INVALID KEY DISPLAY ‘ KEY IS INVALID’
NOT INVALID KEY PERFORM READ-NEXT UNTIL EOF
END-START.
READ-NEXT.
READ INFILE NEXT RECORD
INTO WS-REC
AT END SET EOF TO TRUE
NOT AT END DISPLAY ‘ RECORD READ ‘ WS-REC
END-READ.
OUTPUT OF PROGRAM :
21600 RANDIP KOLKATA
21700 BALDIP CHENNAI
21800 KULJIT BANGLORE
5) CLOSE FILE:
CLOSE FILE verb is used to close all opened files in the program. If we do not close the files, at the time of completion program will close all the opened files, but sometimes due to mishandling of files, they do not get closed and other programs using same files may face error that files are not properly closed. That’s why it is always good practice to close all the files.
Syntax of CLOSE:
CLOSE file-name.
INTERVIEW QUESTION:
1) How to read file in reverse order in Cobol Program.
IDENTIFICATION DIVISION.
PROGRAM-ID. REVERSFI.
ENVIRONMENT DIVISION.
INPUT-OUTPUT SECTION.
FILE-CONTROL.
SELECT INFILE ASSIGN TO DD1
FILE STATUS IS FS1.
DATA DIVISION.
FILE SECTION.
FD INFILE.
01 INREC.
05 EMP-ID PIC X(05).
05 FILLER PIC X(01).
05 EMP-NAME PIC X(20).
05 FILLER PIC X(54).
WORKING-STORAGE SECTION.
01 WS-VAR.
05 SWTICH PIC X(01).
88 EOF VALUE 'Y'.
88 NOT-EOF VALUE 'N'.
05 FS1 PIC X(02) VALUE '00'.
05 CT1 PIC 9(02) VALUE ZEROS.
05 ARR.
10 ARR-OCC OCCURS 0 TO 9999 TIMES
DEPENDING ON CT1
INDEXED BY INX.
15 ARR-REC PIC X(80).
PROCEDURE DIVISION.
SET NOT-EOF TO TRUE.
SET INX TO 1
OPEN INPUT INFILE
PERFORM UNTIL EOF
READ INFILE
AT END
SET EOF TO TRUE
NOT AT END
ADD 1 TO CT1
MOVE INREC TO ARR-REC(INX)
SET INX UP BY 1
END-READ
END-PERFORM.
CLOSE INFILE.
PERFORM VARYING INX FROM CT1 BY -1
UNTIL INX = ZERO
DISPLAY 'ARR-REC: ' ARR-REC(INX)
END-PERFORM.
STOP RUN.
INPUT FROM FILE:
21400 SANDIP
21500 MANDIP
21600 RANDIP
21700 BALDIP
21000 KULDIP
13456 AAAAA
Output of Program:
ARR-REC: 13456 AAAAA
ARR-REC: 21000 KULDIP
ARR-REC: 21700 BALDIP
ARR-REC: 21600 RANDIP
ARR-REC: 21500 MANDIP
ARR-REC: 21400 SANDIP
===================================================================
How to find length of string:
01 WS-VAR PIC X(10) VALUE 'SANDIP '.
01 WS-REV PIC X(10).
01 WS-LEN PIC 9(04) VALUE ZERO.
01 TALLY-COUNT PIC 9(04).
PROCEDURE DIVISION.
MOVE FUNCTION REVERSE(WS-VAR) TO WS-REV
INSPECT WS-REV TALLYING TALLY-COUNT FOR LEADING SPACES
COMPUTE WS-LEN = LENGTH OF WS-VAR - TALLY-COUNT
DISPLAY 'LENGTH OF TEXT FROM WS-VAR: ' WS-LEN
OUTPUT:
LENGTH OF TEXT FROM WS-VAR: 6
No comments:
Post a Comment