0% found this document useful (0 votes)
4 views

COBOL Programming

This document provides an overview of COBOL, covering its history, features, program structure, data types, and coding format. It outlines the objectives for understanding COBOL programming syntax, condition statements, and file processing modes. Additionally, it details the divisions of COBOL programs, including the identification, environment, data, and procedure divisions, along with their respective components and usage.

Uploaded by

mrkcolruyt
Copyright
© © All Rights Reserved
Available Formats
Download as PPT, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
4 views

COBOL Programming

This document provides an overview of COBOL, covering its history, features, program structure, data types, and coding format. It outlines the objectives for understanding COBOL programming syntax, condition statements, and file processing modes. Additionally, it details the divisions of COBOL programs, including the identification, environment, data, and procedure divisions, along with their respective components and usage.

Uploaded by

mrkcolruyt
Copyright
© © All Rights Reserved
Available Formats
Download as PPT, PDF, TXT or read online on Scribd
You are on page 1/ 114

COBOL – An Overview

© Tata Consultancy Services ltd. March 20, 2025 1


Course Pre-requisites
 Some knowledge of information processing concepts and terms.

2
Topics Covered
 Introduction to COBOL
 Program Structure
 Data Types
 Coding Format
 Programming Divisions
 Programming Syntax
 Input/Output Processing
 String Handling
 Report Generation

3
Objectives

To Understand
 The Features of COBOL
 COBOL Programming Syntax
 Condition Statements
 Modes of File Processing
 How to Call other Programs
 Report Generation

4
COBOL LANGUAGE

 COMMON BUSINESS ORIENTED LANGUAGE


 ORIGIN
 CONCEIVED : 1959
 PRELIMINARY VERSION : DEC 1959
 FIRST STANDARD COBOL : 1961
 STANDARD VERSIONS
 APPROVED BY ANSI : 1968, 1974
 LATEST COBOL VERSION : 1985
 9X VERSION EXPECTED
5
SOME COBOL FEATURES
 ENGLISH LIKE
where we employ verbs like copy, add, subtract, move,
compute etc.
 SELF DOCUMENTING
where we can incorporate comments for better
understanding.

6
SOME COBOL FEATURES
 VOLUMINOUS DATA PROCESSING
More complex mathematical manipulations involving large
volumes of data
 PRINCIPLES OF MODULAR DESIGN
Inherent Structured programming principles makes the
program modular.

7
SOME COBOL FEATURES
 I/O OPERATIONS ARE EASILY DESIGNNATED
where the placement of input and output definitions are known.
 SERVES BUSINESS FUNCTIONS
 Programs are compiled and an object program is created for
execution.

8
COBOL PROGRAM STRUCTURE

PROGRAM

DIVISIONS

SECTIONS

PARAGRAPHS

SENTENCES

STATEMENTS

WORDS

9
COBOL WORDS
 RESERVED WORDS
These are the Cobol verbs for specific functions can not be
used as user defined data names.
e.g: TYPE, CR, DR, BLANK, SPACE, ZERO etc.

10
COBOL WORDS
 USER DEFINED WORDS
-The word/Name can be anything adhering to the prescribed
standards and naming conventions.
-Other than Reserved words.
 MAX 30 CHARACTERS
 A-Z
 0-9
 HYPHEN which may not appear as first or last character

11
COBOL DATA
 LITERALS (CONSTANTS)
 DATA-NAMES (VARIABLES)

12
LITERALS

 ALPHANUMERIC - enclosed within quotes max. 160


characters long
 NUMERIC - 0-9 (optional sign & decimal)
 max. 18 digits
 FIGURATIVE CONSTANT
– ZERO,ZEROS,ZEROES
– QUOTE,QUOTES
– SPACE,SPACES
– HIGH-VALUE,LOW-VALUE,ALL

13
DATA NAMES (IDENTIFIERS)
 3 TYPES OF DATA NAMES
 ALPHANUMERIC
 Max 160 characters, Defined with PIC X.
 NUMERIC
 Max 18 digits
 Defined with PIC 9 (S for Sign V for decimal)
 S and V are implicit
 ALPHABETIC
 Max 160 digits, can contain only space & alphabets
 Defined with PIC A

14
COBOL Coding Format

© Tata Consultancy Services ltd. March 20, 2025 15


COBOL CODING FORMAT
 Unlike other languages, Cobol expects us to code the
program in a specific format. The start and end
positions/columns are clearly defined.
 It is possible that the compiler may throw the entire program
with errors unless the rules are followed.
 Understand Area A and Area B.

16
COBOL CODING FORMAT
 COLUMNS 1-6
 Ignored by the compiler
 1-3 page number and 4-6 line number
 COLUMN 7
 comment *
 continuation -
 page break /
 debug D

17
 COLUMNS 8 - 11
 Area A
 Division names, Section names,Paragraph names
 COLUMNS 12 - 72
 Area B
 COBOL code is written here
 COLUMNS 73-80
 Ignored by compiler

18
COBOL CODING FORMAT
 There are Divisions, sections, paragraphs, sentences,
statements and words in Cobol.
 Division, section names are predefined and same names
are to be coded.
 Paragraph names can be any name as per the TCS
prescribed standards or as per the standards provided by
the client.

19
COBOL CODING FORMAT

|--A--|------------------------B---------------------------------------------
1…...8.10……………………………………………………….72…….80
DIVISIONS.
SECTIONS
PARAGRAPHS
SENTENCES CONSISTING OF STATEMENTS AND
WORDS

20
COBOL CODING FORMAT
 Identification Division will provide the information to identify the
program-id, author, dates and the purpose and function of the
program.

 Environment Division will provide information in two parts such as


details of configuration of the computer and Input-Output section will
provide details of inputs and outputs used .

 Data Division will provide the database structures in file section,


memory variable definitions in working-storage section and
parameters definitions in linkage-section.

 Procedure Division will have the entire functional logic based on


business rules specified.

21
DIVISIONS OF COBOL
 DIVISIONS
 IDENTIFICATION DIVISION
 ENVIRONMENT DIVISION
 DATA DIVISION
 PROCEDURE DIVISION

22
IDENTIFICATION DIVISION

PROGRAM-ID. PROGRAM-NAME.
[AUTHOR. Comment entry]
[INSTALLATION. Comment entry]
[DATE-WRITTEN. Comment entry]
DATE-COMPILED. Comment entry]
SECURITY. Comment entry]

23
ENVIRONMENT DIVISION
 2 SECTIONS
 CONFIGURATION SECTION
 INPUT-OUTPUT SECTION

CONFIGURATION SECTION
SOURCE-COMPUTER
OBJECT-COMPUTER.
SPECIAL-NAMES.

24
INPUT-OUTPUT SECTION
 CONSISTS OF 2 SECTIONS
 FILE-CONTROL.
 Names each file : All files consisting of inputs, outputs
are to be defined here.
 Identifies file medium
 I-O CONTROL.
 Specifies file location on multiple file reel
 shared memory areas

25
INPUT-OUTPUT SECTION
 FILE-CONTROL.
Syntax:
Select infil assign to infile.
Logical File Name : File name referred throughout the program for open,
read, write and close. (infil)
Physical File Name: File name referred in JCL and Shell Scripts to link to
the logical file name in the program.(infile)

26
INPUT-OUTPUT SECTION

Disk Storage
 Select EMPFILE assign to EMPFILE

 FD EMPFILE.
 01 EMPREC.
 05 EMPNO Central Storage
 05 EMPNAME
 05 EMPDESGCODE EMPREC (RECORD)
 05 EMPBASIC
Field Field Field Field Field
 05 EMPPROJ

27
FILE CONTROL PARA
SELECT logical file name ASSIGN TO ddname
ORGANIZATION IS SEQUENTIAL
INDEXED
RELATIVE
ACCESS IS SEQUENTIAL
RANDOM
DYNAMIC
RECORD KEY IS data-name-1
ALTERNATE KEY IS data-name-2
FILE STATUS IS file-stat

28
FILE CONTROL PARA

Important points to note for indexed files:


1. If the organizations is indexed and access is random- records
can be accessed randomly and not sequentially.
2. If the organization is indexed and access is dynamic - records
can be accessed randomly as well as sequentially.

29
FILE CONTROL PARA

FILE-STATUS
Used to check the following:
1. To check the existence of a file while open, close.
2. To check the existence of a record.
3. To check the invalid keys

30
DATA DIVISION

 FILE SECTION
 WORKING-STORAGE SECTION
 LINKAGE SECTION

31
DATA DIVISION
 LEVEL STRUCTURE
 Inherent in the logical record
 Each basic subdivision is an elementary item
 Elementary items may form part of one or more groups
 Level numbers : 01 - 49
 : 66 77 88

32
DATA DIVISION

PICTURE CLAUSE
Picture clause is used to specify the type of field, its size, and its form.
 PIC used in Cobol is the abbreviated form of PICTURE.
 PIC or PICTURE is a reserved word.
 PIC or PICTURE can not be used as a user defined data name.
 PIC is followed by type and length of field in brackets

33
DATA DIVISION
 PICTURE CLAUSE
 Specified for every elementary item
 Numeric Data item - 9 P S V ( )
 Alphabetic data item - A B ( )
 Alphanumeric data item - X ( )

34
PICTURE CLAUSES
 EXAMPLES :
 9(4)V99 = numeric value with decimals.
 X(20) = alphanumeric values
 A(10) = aliphatic values
 S9(4)V99 = signed field
 B(4) = number of blanks

35
FILE SECTION
The rules for defining a file structure.
 File description is denoted by FD followed by the logical file name.
 Record name will have to be at 01 level. The name defined at 01 level represents
the entire record.
 The segments or fields of record are defined with levels which is greater than 01. (In
general it will be multiples of 5.)
 Values can not be assigned to any field in the file lay out. Exception is 88 level
which is explained in the subsequent PPT.

36
FILE SECTION
Example for defining file layout.
FD EMPMAS.
01 EMPREC.
05 EMP-NO PIC 9(06).
05 EMP-NAME.
10 FIRST-NAME PIC X(15).
10 MIDDLE-NAME PIC X(20).
10 LAST-NAME PIC X(20).
(Naming standards are not followed here)

37
FILE SECTION
 FD ENTRY FOR EACH FILE
 FOLLOWED BY RECORD DESCRIPTION
(begins in 01 level)
 File record naming convention :
 ex: XNN-TTTT-REC
 XNN : First 3 characters of file name
 TTTT: Name of record ( 1 to 22 characters)
 REC : Should appear as it is
 DO1-CONTROL-REC
 P09-PRINT-REC
38
WORKING-STORAGE SECTION

 Succeeds File Section


 Used For Intermediate Storage
 E.G.,
01 W-MEM-DETAILS.
05 W-MEM-NUM PIC 9(10)
05 W-MEM-ADDRESS.
10 W-MEM-ADR-LINE1 PIC X(30).
10 W-MEM-ADR-LINE2 PIC X(30).
Please see notes below.
39
VALUE CLAUSE
 Assigns Initial Value To A Data Item
 Syntax : Value Is literal
 literal can be
 NUMERIC
 NON-NUMERIC IN QUOTES
 FIGURATIVE CONSTANT

Examples:
05 ws-basic pic 9(05) value 20000.
05 ws-code pic x(05) value ‘CD2000’.
05 ws-variable-pay pic 9(05) value zero.

40
EDITING CHARACTERS

 NUMERIC EDITING DATA


 Z ZERO SUPPRESSION
 * , + - $ CR DB PERIOD
 BLANK , ZERO, SLASH,BLANK WHEN ZERO
 ALPHABETIC
 BLANK INSERTION
 ALPHANUMERIC
 BLANK, ZERO , SLASH INSERTION

41
CONDITION NAMES
 APPEARS IN LEVEL 88.
 CONSIDER A DATA ITEM SALARY
01 SALARY PIC 9(5)V99 .
TO CHECK FOR VALIDITY,
IF SALARY > 8000 AND SALARY < 25000
CAN BE USED.
USING CONDITION-NAMES
01 SALARY PIC 9(5)V99.
88 C-VALID-SALARY VALUE 8000 THRU 25000
IF C-VALID-SALARY CAN BE USED.

42
CONDITION NAMES - CONTINUED

 TO CHECK FOR GRADE


01 GRADE PIC X.
88 C-VALID-GRADE VALUE ‘A’,’B’,’C’.
88 C-GRADE-A VALUE ‘A’
88 C-GRADE-B VALUE ‘B’
88 C-GRADE-C VALUE ‘C’
IF C-VALID-GRADE(to check for A or B or C)
IF C-GRADE-B (to check for grade B alone)

43
REDEFINES
THIS CLAUSE ALLOWS THE SAME AREA OF MEMORY TO BE REFERENCED
BY MORE THAN ONE DATA-NAME WITH DIFFERENT FORMATS AND
SIZES.

<LEVEL> <DATA-NAME-1>
REDEFINES <DATA-NAME-2>
 THIS CLAUSE MUST IMMEDIATELY FOLLOW <DATA-NAME-1>
 LEVEL NOS OF <NAME-1> AND <NAME-2> MUST BE IDENTICAL AND NOT
66 OR 88.

44
REDEFINES
 AN EXAMPLE:
01 DAYS-IN-WEEK.
05 DAY-NAMES PIC X(21) VALUE “SUNMONTUE...”.
05 W-NAMES REDEFINES DAY-NAMES.
10 W-DAYS PIC X(3) OCCURS 7 TIMES.
The days can be accessed as W-DAYS(1),W-DAYS(2),ETC.

45
REDEFINES
NOTE 1: AT 01 LEVEL, REDEFINES & OCCURS CANNOT BE COMBINED.

NOTE 2: ORIGINAL DATA NAME AND REDEFINED DATA NAMES SHOULD BE


DIFFERENT.

NOTE 3: NEED TO BE CAUTIOUS WHICH NAME IS BEING USED IN


PROCEDURE DIVISION.

NOTE 4: REMEMBER THAT BOTH WILL HAVE SAME VALUE UNLESS NEW
VALUES ARE ASSIGNED BEFORE MOVING TO OTHER ENTITY.

46
USAGE CLAUSE
FORMS OF INTERNAL REPRESENTATION
 DISPLAY
 COMP
 COMP1
 COMP2
 COMP3
 SYNTAX:
USAGE IS DISPLAY
COMP-1

47
USAGE CLAUSE CONTINUED

 COMP
 MAINTAINED IN BINARY FORM
NO.OF 9’S LENGTH IN BYTES
1 TO 4 2
5 TO 9 4
10 TO 18 8

 COMP-1
 ONE WORD IN FLOATING POINT FORMAT
48
USAGE CLAUSE CONTINUED
 COMP-2
 Two words in floating point form
 no pic clause for COMP-1 & COMP-2.

 COMP-3
 4 binary digits representing single decimal digit
 Each digit and sign occupy 1/2 byte

49
LINKAGE SECTION
 Requires only when other programs are called to perform specific functions.

 This section appears in called program.

 It is almost like performing some Para with in a program

 Parameters are defined in called program under this section

 All parameters are defined at 01 level.

 Ensure that the program-id in called program is same as that of the program
name called from main program.

50
LINKAGE SECTION
 Example:
working-storage section.
-----
linkage section
01 x pic 99.
01 y pic 99.
01 z pic 99.
Note: Please see CALL verb for further details.

51
PROCEDURE DIVISION
 STRUCTURE
 SECTION - composed of paragraphs
 PARAGRAPH - composed of sentences (identified by
Para name).

 Eg., 0100-READ-INPUT
 SENTENCE-Composed of one or more statements and
terminated by Period.

52
COBOL VERBS
 CLASSIFICATION
 INPUT OUTPUT
 ARITHMETIC
 PROCEDURE BRANCHING
 DATA MOVEMENT
 ENDING
 CONDITIONAL
 TABLE HANDLING
 COMPILER DIRECTING
 STRING-HANDLING
 OTHERS
53
INPUT OUTPUT VERBS
 ACCEPT
 USED TO READ DATA
 SYNTAX
 ACCEPT identifier FROM mnemonic-name
DATE
TIME
DAY

54
DISPLAY
 USED TO DISPLAY DATA
 SYNTAX
DISPLAY identifier-1 ,identifier2
literal-1 ,literal2

 OPERANDS SHOULD BE USAGE DISPLAY

55
ARITHMERIC VERBS

OPERATIONS :
 ADD
 SUBTRACT
 MULTIPLY
 DIVIDE
 COMPUTE

56
ADD VERB
 ADD A TO B

 ADD A TO B ROUNDED

 ADD A TO B GIVING C ROUNDED


ON SIZE ERROR PERFORM ERR-PARA

 ADD A TO B C D GIVING E F G

 ADD A B C TO D

 ADD CORR G1 TO G2 (CORR IS THE SHORT FORM OF


CORRESPONDING)

57
MULTIPLY,DIVIDE,SUBTRACT
MULTIPLY
 MULTIPLY A BY B GIVING C ON SIZE
ERROR PERFORM ERROR-PARA

DIVIDE
DIVIDE A BY B GIVING C REMAINDER D

SUBTRACT
Also has many formats. Similar to Add.

COMPUTE
COMPUTE PRICE = RATE * QTY - DISCOUNT

58
BRANCHING
 PERFORM
 GO TO (Unconditional branching)
 EXIT (to exit from a paragraph)

59
PERFORM

 PERFORM PARA-1 THRU PARA-N


 PERFORM PARA-1 10 TIMES
 PERFORM PARA-1 UNTIL C-FILE-END
 PERFORM PARA-1 VARYING I FROM 1 BY 1 UNTIL I >
10
AFTER PARA-2 FROM 1 BY 1 UNTIL J >5

60
IN-LINE PERFORM
 To sum a series for eg., 1+2+3+4+5+….+N

MOVE 0 TO SUM-OF-DIGITS
MOVE 1 TO COUNTER
PERFORM UNTIL COUNTER > N
ADD COUNTER TO SUM-OF-DIGITS
ADD 1 TO COUNTER
END-PERFORM.

61
PERFORM

PERFORM PROCESS-DETAIL
WITH TEST AFTER UNTIL C-FILE-END

PERFORM PROCESS-DETAIL
WITH TEST BEFORE UNTIL C-FILE-END

62
DATA TRANSFER

Data transfer in Cobol is done using ‘MOVE’ verb though


values in sending field remain unaffected.
 Receiving field should not be smaller than sending field
to avoid truncation.
 Move of Aliphatic or alphanumeric value is left justified.
 Move of Numeric values is right justified.

63
DATA MOVEMENT VERBS
 MOVE id-1 To id-2 id-3 id-n
lit-1
 MOVE CORR G-1 TO G-2

64
CATEGORY OF RECEIVING DATA ITEM
CATEGORY OF ALPHABETIC ALPHANUMERIC NUMERIC
SENDING DATA ALPHANUMERIC INTEGER;
ITEM EDITED NUMERIC
NONINTEGER
NUMERIC
EDITED
ALPHABETIC YES YES NO
ALPHANUMERIC YES YES NO

ALPHANUMERIC NO YES NO
EDITED
NUMERIC INTEGER NO YES YES
NUMERIC NO NO YES
NONINTEGER
NUMERIC EDITED NO YES NO

65
ENDING
 STOP RUN
 RETURNS CONTROLTO OPERATING SYSTEM

 STOP lit-1
 TEMPORARILY HALTS A PROGRAM

 EXIT PROGRAM
 RETURNS FROM A SUB PROGRAM TO A MAIN
PROGRAM

66
CONDITIONAL EXPRESSIONS
 RELATION
RELATIONAL OPERATORS
< = >= <= NOT =

 CLASS
NUMERIC ALPHABETIC

 SIGN
POSITIVE NEGATIVE ZERO

67
ABBREVIATED COMPOUND CONDITIONS

 IF A=B AND A=C is equivalent to


IF A=B AND C

 A>B OR NOT (A>C) OR (A>D) is equivalent to


A>B OR NOT C OR D

68
TABLE HANDLING
 Eliminates need for separate entries for related data.
 OCCURS clause defines tables of contiguous data items
 Can be specified for elementary & group items but not
for 01 77 66 88 levels.
ONE DIMENSION TABLE
01 STUDENT-REC.
05 STUDENT-NAME PIC X(10).
05 STUDENT-MARKS OCCURS 10 TIMES PIC 999V99.
MARKS CAN BE ACCESSED AS STUDENT-MARKS(1) ETC.,

69
TABLE HANDLING
 ONE DIMENSION TABLE
01 STUDENT-REC.
05 STUDENT-NAME PIC X(10).
05 STUDENT-MARKS OCCURS 10 TIMES PIC 999V99.
MARKS CAN BE ACCESSED AS STUDENT-MARKS(1) ETC.,
 TWO DIMENSION TABLE
01 STUDENT-REC.
05 STUDENT-NAME PIC X(10).
05 STUDENT-MARKS OCCURS 10 TIMES.
10 MARKS-INT-EXT OCCURS 10 TIMES.
15 MARKS-INT PIC 99.
15 MARKS-EXT PIC 99.
MARKS CAN BE ACCESSED AS STUDENT-MARKS(1,1) ETC.,

70
OCCURS SYNTAX

 FORMAT-1
OCCURS integer-2 TIMES {ASCENDING/DESCENDING}
KEY IS data-name-1 [data-name-2] …
[INDEXED BY index-name-1 [,index-name-2] …..
 FORMAT-2
OCCURS integer-1 to integer-2 TIMES
DEPENDING ON data-name-1 {ASCENDING/DESCENDING}
KEY IS [data-name-2] …
[INDEXED BY index-name-1 [,index-name-2] …..

71
INDEX
 INDEX IS A STACK VARIABLE
 MUST BE INITIALISED BEFORE IT CAN BE USED- BY
USING SET VERB
EG.,
SET INDEX-1 TO 5
SET INDEX-1 UP BY 1
SET INDEX-1 DOWN BY 1

72
SEARCH
 To search a table element
 Two search options
 SEARCH VARYING
 SEARCH ALL - may be carried out of sorted tables.

73
SEARCH
 SEARCH id-1 VARYING {index-1/ id-2}
AT END statement-1
WHEN Condition-1 stmt-2
 SEARCH ALL id-1
AT END stmt-1
WHEN Condition-1 NEXT SENTENCE

74
SEARCH
77 NAME PIC X(20)
01 SAVE-BNK-ACCT.
05 ACCT-TBL OCCURS 400 TIMES INDEXED BY A1.
10 ACCT-NO PIC 9(6)
10 ACCT-NAME PIC X(20)
10 ACCT-AMT PIC 9(6)V99.
SET A1 TO 1.
SEARCH ACCT-TBL AT END DISPLAY “NOT FOUND”
WHEN NAME=ACCT-NAME(A1)
DISPLAY “NAME FOUND”
DISPLAY ACCT-NO “ “ ACCT-AMT.

75
SEARCH ALL
01 EMP-TABLE.
05 E-NAME-NO OCCURS 40 TIMES
ASCENDING KEY IS E-NAME
INDEXED BY INDX-1.
10 E-NAME PIC X(10).
10 E-NO PIC 9(10).

SEARCH ALL E-NAME-NO AT END


PERFORM NO-FIND-PARA
WHEN E-NAME(INDX-1)=EMPL-NAME
MOVE E-NO(INDX-1) TO EMPL-NO.

NOTE: TABLE HAS TO BE SORTED ON E-NAME.

76
COMPILER DIRECTING
 COPY VERB
 SYNTAX : COPY “FILE”

77
Input/Output Processing

© Tata Consultancy Services ltd. March 20, 2025 78


FILE I-Os
 OPEN
 CLOSE
 READ
 WRITE
 REWRITE
 DELETE
 START

79
OPEN
 MODES OF OPEN
 INPUT
 OUTPUT
 I-O
 EXTEND

80
OPEN
 MODES OF OPEN
 When files are opened in input mode, records can only be
read

 When files are opened in output mode, records can only be


written

 When files are opened in input-output (I-O) mode, records can


read and write

 When files are opened in Extend mode, records can only be


appended to the existing file

81
OPEN, CLOSE VERBS
 OPEN <mode> LOGICAL-FILE-NAME

 CLOSE LOGICAL-FILE-NAME

82
HOW TO CHOOSE THE MODE?
OPERATION SEQUENTIAL INDEXED

READ INPUT, I/O INPUT,I/O

WRITE OUTPUT,EXTEND I/O,OUTPUT

REWRITE I/O I/O

83
READ

 FORMAT-1
READ file-name AT END imperative statement

 FORMAT-2
READ file-name INVALID KEY imperative statement

 FORMAT-3
READ file-name NEXT RECORD INTO identifier
AT END imperative statement

84
READ

 FORMAT-1
READ EMPFILE AT END MOVE ‘Y’ TO EOF.

 EOF to be defined in working-storage section assigning value ‘N’.

 AT END clause is determined by the system and Y is moved to EOF


and no further process takes place.

85
READ
 FORMAT-1
READ EMPFILE AT END MOVE ‘Y’ TO EOF.
EOF to be defined in working-storage section assigning value ‘N’.

 FORMAT-2
READ EMPMAS INVALID KEY GO TO PARA-EXIT

 FORMAT-3
READ file-name NEXT RECORD INTO identifier
AT END imperative statement

86
WRITE

 FORMAT-1
WRITE record-name [FROM id-1]
{BEFORE/AFTER} ADVANCING {int-1, LINE,PAGE}

 FORMAT-2
WRITE record-name INVALID KEY imperative-statement

87
REWRITE
 REWRITE record-name INVALID KEY imperative statement

88
DELETE
 DELETE file-name RECORD
INVALID KEY imperative statement

Note 1: DELETE can not be used for a sequential file.


Note 2: DELETE is used for Indexed files only when the file is
opened in I-O mode.

89
START
 ENABLES POSITIONING OF POINTER AT A SPECIFIED POINT IN AN
INDEXED FILE

 FILE SHOULD BE OPENED IN INPUT OR I-O MODE

 ACCESS SHOULD BE SEQUENTIAL OR DYNAMIC

 START DOES NOT READ THE RECORD

START file-name KEY IS {= > NOT < }


INVALID KEY imperative statement

90
STRING HANDLING

 INSPECT
Provides ability to tally, replace characters in a Data item

FORMAT-1
INSPECT id-1 TALLYING id-2 FOR
{ALL id-3/LEADING lit-1/CHARACTERS} {BEFORE/AFTER}
INITIAL {id-4/lit-2}

91
INSPECT …contd
 MY-STRING X(20) CHANDRA BABU NAIDU
 TALLY-COUNT 9(2) VALUE 08

INSPECT MY-STRING TALLYING TALLY-COUNT FOR ALL “A”. 12


INSPECT MY-STRING TALLYING TALLY-COUNT FOR LEADING “C” 09
INSPECT MY-STRING TALLYING TALLY-COUNT FOR ALL “AND” BEFORE INITIAL
“R” AFTER INITIAL “H”. 09
INSPECT QTY REPLACING LEADING SPACES BY ZEROS

INSPECT Y TALLYING CNT FOR ALL “L” REPLACING LEADING “A” BY “E” AFTER
INITIAL “L”
Y(BEFORE) LATTER Y(AFTER) LETTER CNT 1

92
STRING
01 NAME.
05 F-NAME PIC X(10).
05 M-NAME PIC X(10).
05 S-NAME PIC X(20).
01 OUT-REC PIC X(60).
01 A PIC X VALUE SPACES.
STRING S-NAME DELIMITED BY SPACES
A DELIMITED BY SIZE
F-NAME DELIMITED BY SPACES
A DELIMITED BY SIZE
M-NAME DELIMITED BY SPACES
INTO OUT-REC.

93
UNSTRING
01 FIELD-1 PIC X(15).
01 FIELD-2 PIC X(15).
01 FIELD-3 PIC X(15).
01 CMP-NAME PIC X(25)
VALUE IS “TATA CONSULTANCY SERVICES”.
UNSTRING CMP-NAME DELIMITED BY “ “
INTO FIELD-1 FIELD-2 FIELD-3.
FIELD CONTENTS
FIELD-1 TATA
FIELD-2 CONSULTANCY
FIELD-3 SERVICES

94
OTHER VERBS
 SORT
 RELEASE
 RETURN
 CALL
 IF AND NESTED IF
 EVALUATE

95
SORT VERB

 SYNTAX
SORT SORT-FILE {ASCENDING/DESCENDING} KEY
data-name-1
[USING input-file/INPUT PROCEDURE IS
section-1 thru section-2]
[GIVING output-file/OUTPUT PROCEDURE IS
section-3 thru section-4]

96
RELEASE
 Transfers records to the initial phase of a sort operation

 RECORD NAME MUST BE AN SD ENTRY

 MAY NOT BE USED OUTSIDE AN INPUT PROCEDURE

 ONLY RELEASED RECORDS ARE SORTED

 SYNTAX
RELEASE record-name [FROM identifier]

97
RETURN

 Obtains sorted records from the final phase of sort


 Execution causes the next record to be made available
 SYNTAX
RETURN file-name RECORD [INTO identifier ] AT END
imperative statement

98
CALL
 SYNTAX
CALL {id-1/lit-1} [USING data-name-1 [,data-name-2]…]
[ ;ON OVERFLOW imp-stmt]
PROCEDURE DIVISION
[USING data-name-1 [,data-name-2]…]
LINKAGE SECTION
01 level entries
EXIT PROGRAM

99
CALL
Calling Program:
IDENTIFICATION DIVISION.
PROGRAM-ID. TRNG1.
------------------------------
------------------------------
DATA DIVISION.
----
01 A PIC 99.
01 B PIC 99.
01 A-PLUS-B PIC 999.
01 STATUS-CODE PIC X(4).
----
PROCEDURE DIVISION.
CALL ‘TRNG2’ USING A, B, A-PLUS-B, STATUS-CODE.
IF STATUS-CODE = ‘O.K’
PERFORM A-PARA
ELSE
PERFORM B-PARA
END-IF.

100
CALL
Called Program:

IDENTIFICATION DIVISION.
PROGRAM-ID. TRNG2.
------------------------------
------------------------------
DATA DIVISION.
----
LINKAGE SECTION
01 A PIC 99.
01 B PIC 99.
01 A-PLUS-B PIC 999.
01 STATUS-CODE PIC X(4).
PROCEDURE DIVISION USING A, B, A-PLUS-B, STATUS-CODE.
COMPUTE-PARA.
IF A IS NUMERIC AND B IS NUMERIC
COMPUTE A-PLUS-B = A + B
MOVE ‘O.K’ TO STATUS-CODE
ELSE
MOVE SPACES TO STATUS-CODE
END-IF.
CALL-END-PARA.
EXIT PROGRAM.

101
IF AND ELSE
 The most important verb in any language.
 Used for conditional checks
 Example:
if designation-code > 06
perform process-para1
thru process-para1-exit
else
perform process-para2
thru process-para2-exit
end-if

102
IF AND ELSE
Note1: Every If expression may have else to provide
alternate action for false condition.

Note2: Period at the end of statement containing ‘if’ passes


the control to the next statement after period.( see notes)

Note3: End-if (scope terminator) at the end of statement


nullifies current if statement.

103
Conditional statement-flow

false true
condition

Statement-2 Statement-1

Next sentence
104
NESTED IF

Points to Note:
 Combination of several ‘IF and ELSE’ statements.

 Used for checking several conditions to pass the control to respective


modules.

 Used for branching complicated calculations.

 First ELSE pertains to the preceding if and next else pertains to the IF just
preceding the inner IF.

105
NESTED IF

IF---IF---IF---ELSE---ELSE---ELSE---

106
EVALUATE

 This verb is added in the modified Cobol version 85.


 Replace IF with EVALUATE to avoid complications of
NESTED IF
 END-EVALUATE is the scope terminator.
 Powerful and fast as compared to IF.

107
EVALUATE

 Example 1:
EVALUATE DESG-CODE
WHEN 01 PERFORM 01-PARA
WHEN 02 PERFORM 02-PARA
WHEN 03 PERFORM 03-PARA
WHEN OTHER PERFORM ERROR-PARA
END-EVALUATE

108
EVALUATE

 Example 2:
EVALUATE TRUE
WHEN 01-ADD PERFORM 01-PARA
WHEN 02-ADD PERFORM 02-PARA
WHEN 03-ADD PERFORM 03-PARA
WHEN OTHER PERFORM ERROR-PARA
END-AVALUATE
 Note: 01-ADD, 02-ADD and 03-ADD are 88 level condition names.

109
EVALUATE
 Example 3:
EVALUATE DESG-CODE AND BASIC-PAY
WHEN 01 THRU 03 ALSO 10000 THRU
15000 PERFORM 0103-PARA
WHEN 04 THRU 06 ALSO 15001 THRU
20000 PERFORM 0406-PARA
WHEN OTHER PERFORM ERROR-PARA
END-AVALUATE

110
REPORT GENERATION
 Requirements
 1. input data.
 2. formatting the report.
Input is however obtained from input files. Formatting a print
report in Cobol needs special attention.

111
REPORT GENERATION

 Centralize Headings on 80 columnar stationary


01 heading-1.
05 filler pic x(25) value spaces.
05 filler pic x(25) value
‘Training Cobol Batch No : ‘.
05 batch-no pic z(04)b.
 spacing between two data items need to be defined with number of
fillers required.

112
Summary
 Introduction to
 COBOL Features

 Coding Format & Programming Syntax

 Programming Divisions

 File processing

113
References

 ‘COBOL Unleashed’ by Jon Wessler and et.al, Macmillan/Pearson


Technology
 COBOL books and courses in Skillport (http://tcs.skillport.com)
 ‘Structured COBOL Programming’ by Stern & Stern, John Wiley,
New York
 ‘COBOL Programming including MS COBOL & COBOL 85’ by
Roy & Dashtidar, II edition, Tata McGraw-Hill

114

You might also like