COBOL Tutorial $
COBOL Tutorial $
COBOL stands for Common Business Oriented Language and was developed in 1959. US
Department of Defense, in a conference, formed CODASYL (Conference on Data Systems
Language) to develop a language for business data processing needs which is now known as
COBOL.
COBOL is used for writing application programs and we cannot use it to write system software.
The applications like those in defense domain, insurance domain, etc. which require huge data
processing use COBOL language.
Introduction to COBOL
COBOL is a high level language. One must understand the way COBOL works. Computers
only understand machine code, a binary stream of 0s and 1s. COBOL code must be converted
into machine code with the use of compiler. Run program source through a compiler. The
compiler first checks for any syntax errors and then converts it into machine language. The
compiler creates a output file which is known as load module. This output file contains
executable code in form of 0s and 1s.
Evolution of COBOL
During 1950’s, when the businesses were growing in western part of the world, there was a
need to automate the processes for ease of operation and this gave birth to a high level
programming language meant for business data processing.
Importance of COBOL
● COBOL was first widely used high-level programming language. It is an English-like
language which is user friendly. All the instructions can be coded in simple English
words.
● COBOL is also works as a self documenting language.
● COBOL can handle huge data processing.
● COBOL is compatible with its previous versions.
● COBOL has effective error messages and so, resolution of bugs is easier.
Features of COBOL
Standard Language
COBOL is a standard language which can be compiled and executed on many machines like
IBM AS/400, personal computers, etc
Business Oriented
COBOL was designed for business-oriented applications like in financial domain, defense
domain etc. It can handle huge volumes of data because of its advanced file handling
capabilities.
Robust Language
COBOL is a robust language as its numerous debugging and testing tools are available for
almost all computer platforms.
Structured Language
Logical control structures are available in COBOL which makes it easier to read and modify.
COBOL has different divisions so it is easy to debug.
There are many mainframe compiler utilities available to compile a COBOL program. Some
corporate companies use Change Management tools like Endevor, which compiles and stores
every version of the program. This is useful in tracking the changes made to the program.
IGYCRCTL is an IBM COBOL compiler utility. The compiler options are passed using PARM
parameter. In the above example, RMODE instructs the compiler to use relative addressing
mode in the program. The COBOL program is passed using SYSIN parameter and the
copybook is the library used by the program in SYSLIB.
Executing COBOL Programs
Below a JCL example where the program MYPROG is executed using the input file
MYDATA.URMI.INPUT and produces two output files written to the spool.
In the above example, MYCOBB is the COBOL-DB2 program run using IKJEFT01. Please note
that the program name, DB2 Sub-System Id (SSID), DB2 Plan name are passed within the
SYSTSIN DD statement. The DBRM library is specified in the STEPLIB.
Try following example using Try it option available at the top right corner of the below sample
code box:
IDENTIFICATION DIVISION.
PROGRAM-ID. HELLO.
PROCEDURE DIVISION.
DISPLAY 'Hello World'.
STOP RUN.
When you compile and execute the above program it produces the following result:
Hello World
COBOL - Program
Structure
A COBOL program structure consists of divisions as shown in the following image:
You can co-relate the above mentioned terms with COBOL program in following example:
PROCEDURE DIVISION.
A0000-FIRST-PARA SECTION.
FIRST-PARAGRAPH.
ACCEPT WS-ID - Statement-1 -----|
MOVE '10' TO WS-ID - Statement-2 |-- Sentence - 1
DISPLAY WS-ID - Statement-3 -----|
.
Divisions
COBOL program consists of 4 divisions
Identification Division
It is the first and only mandatory division of every COBOL program. The programmer and
compiler use this division to identify the program. In this Division only PROGRAM-ID is
mandatory paragraph. PROGRAM-ID specifies the program name. Program name can consist
of 1 to 30 characters.
Try following example using Try it option available at the top right corner of the below sample
code box:
IDENTIFICATION DIVISION.
PROGRAM-ID. HELLO.
PROCEDURE DIVISION.
DISPLAY 'Welcome to Tutorialspoint'.
STOP RUN.
//SAMPLE JOB(TESTJCL,XXXXXX),CLASS=A,MSGCLASS=C
//STEP1 EXEC PGM=HELLO
When you compile and execute the above program it produces the following result:
Welcome to Tutorialspoint
Environment Division
Environment Division is used to specify input and output files to the program. It consists of two
sections:
● Configuration Section gives information about the system on which the program is
written and executed. It consists of two paragraphs:
● Source Computer : System used to compile the program.
● Object Computer : System used to execute the program.
● Input-Output Section is used to specify information about the files to be used in the
program. It consists of two paragraphs:
● File Control : Gives the ,:
● File Section is used to define the record structure of the file.
● Working-Storage Section is used to declare temporary variables and file structures
which are used in the program.
● Local-Storage Section is same as Working-Storage section. The only difference is that
the variables will be allocated and initialized every time program starts execution.
● Linkage Section is used to describe the data names that are received from external
program.
COBOL PROGRAM
IDENTIFICATION DIVISION.
PROGRAM-ID. HELLO.
ENVIRONMENT DIVISION.
INPUT-OUTPUT SECTION.
FILE-CONTROL.
SELECT FILEN ASSIGN TO INPUT.
ORGANIZATION IS SEQUENTIAL.
ACCESS IS SEQUENTIAL.
DATA DIVISION.
FILE SECTION.
FD FILEN
01 NAME PIC A(25).
WORKING-STORAGE SECTION.
01 WS-STUDENT PIC A(30).
01 WS-ID PIC 9(5).
LOCAL-STORAGE SECTION.
01 LS-CLASS PIC 9(3).
LINKAGE SECTION.
01 LS-ID PIC 9(5).
PROCEDURE DIVISION.
DISPLAY 'Executing COBOL program using JCL'.
STOP RUN.
//SAMPLE JOB(TESTJCL,XXXXXX),CLASS=A,MSGCLASS=C
//STEP1 EXEC PGM=HELLO
//INPUT DD DSN=ABC.EFG.XYZ,DISP=SHR
When you compile and execute the above program it produces the following result:
Procedure Division
Procedure Division is used to include logic of the program. It consists of executable statements
using variables defined in data division. In this division, Paragraph and Section names are
user-defined.
There must be at least one statement in procedure division. Last statement to end the
execution in this division is either STOP RUN which is used in calling programs or EXIT
PROGRAM which is used in called programs.
IDENTIFICATION DIVISION.
PROGRAM-ID. HELLO.
DATA DIVISION.
WORKING-STORAGE SECTION.
01 WS-NAME PIC A(30).
01 WS-ID PIC 9(5) VALUE '12345'.
PROCEDURE DIVISION.
A000-FIRST-PARA.
DISPLAY 'Hello World'.
MOVE 'TutorialsPoint' TO WS-NAME.
DISPLAY "My name is : "WS-NAME.
DISPLAY "My ID is : "WS-ID.
STOP RUN.
//SAMPLE JOB(TESTJCL,XXXXXX),CLASS=A,MSGCLASS=C
//STEP1 EXEC PGM=HELLO
When you compile and execute the above program it produces the following result:
Hello World
My name is : TutorialsPoint
My ID is : 12345
COBOL - Basic
Syntax
Character Set
'Characters' hierarchy is lowest and it cannot be divided further. There are 78 characters in
COBOL Character Set which are shown below:
Character Description
0-9 Numeric
Space
+ Plus Sign
* Asterisk
/ Forward Slash
$ Currency Sign
, Comma
; Semicolon
( Left Parenthesis
) Right Parenthesis
' Apostrophe
= Equal Sign
Coding Sheet
COBOL source program must be written in a format which is acceptable to the compilers.
COBOL programs are written on COBOL coding sheets. There are 80 characters position on
each line of coding sheet.
//SAMPLE JOB(TESTJCL,XXXXXX),CLASS=A,MSGCLASS=C
//STEP1 EXEC PGM=HELLO
When you compile and execute the above program it produces the following result:
Coding Sheet
Character Strings
Character-Strings are formed by combining individual characters. A character string can be a
● Comment
● Literal
● COBOL word
All character strings must be ended with separators. A separator is used to separate character
strings.
Frequently used Separators : Space, Comma, Period, Apostrophe, Left/Right Parenthesis and
Quotation mark.
Comment
A comment is a character string which does not affect the execution of program. It can be any
combination of characters.
Comment Line
Comment Line can be written in any column. The compiler does not check comment line for
syntax and treats it for documentation.
Comment Entry
Comment Entry are the entries in optional paragraphs of Identification Division. They are
written in Area B and programmers use it for reference.
//SAMPLE JOB(TESTJCL,XXXXXX),CLASS=A,MSGCLASS=C
//STEP1 EXEC PGM=HELLO
When you compile and execute the above program it produces the following result:
Comment Line
Literal
Literal is a constant which is directly har coded in the program. In following example "Hello
World" is a literal.
PROCEDURE DIVISION.
DISPLAY 'Hello World'.
Alphanumeric Literal
Alphanumeric Literal are enclosed in quotes or apostrophe. Length can be up to 160
characters. An apostrophe or quote can be a part of literal only if it is paired. Starting and
ending of the literal should be same either apostrophe or quote.
Examples
Valid:
‘This is valid’
"This is valid"
‘This isn’’t invalid’
Invalid:
‘This is invalid”
‘This isn’t valid’
Numeric Literal
Numeric Literal is a combination of digits from 0 to 9, +, - or decimal point. Length can be up to
18 characters. Sign cannot be the rightmost character. Decimal point should not appear at the
end.
Examples
Invalid:
1,00
10.
10.9-
COBOL Word
COBOL Word is a character string which can be a reserved word or user-defined. Length can
be up to 30 characters.
User-Defined
User defined words are used for naming files, data, records, paragraph names and sections.
Alphabets, digits and hyphen are allowed while forming user-defined word. You cannot use
COBOL reserved words
Reserved Words
Reserved words are predefined words in the COBOL language. Different types of reserved
words which we use frequently are as follows:
Figurative Constants
Figurative Description
Constants
HIGH-VALUES One or more characters which will be at the highest position in
descending order
COBOL - Data
Types
Data Division is used to define the variables used in the program. To describe data in COBOL,
one must understand the following terms:
● Data Name
● Level Number
● Picture Clause
● Value Clause
Data Name
Data names must be defined in the Data Division before using them in Procedure Division.
They must be a User-Defined name and Reserved Words cannot be used. Data names gives
reference to the memory locations where actual data is stored. They can be elementary or
group type.
Examples
Valid:
WS-NAME
TOTAL-STUDENTS
A100
100B
Invalid:
MOVE (Reserved Words)
COMPUTE (Reserved Words)
100 (No Alphabet)
100+B (+ is not allowed)
Level Number
Level number is used to specify the level of data in a record. They are used to differentiate
between elementary items and group items. Elementary items can be grouped together to
create Group items.
Level Number Description
● Elementary Items cannot be divided further. Level number, Data name, Picture clause
and Value clause (Optional) are used to describe elementary item.
● Group Items consists of one or more elementary items. Level number, Data name and
Value clause (Optional) are used to describe group item. Group level number is always
01.
Example
DATA DIVISION.
WORKING-STORAGE SECTION.
01 WS-NAME PIC X(25). ---> ELEMENTARY ITEM
01 WS-CLASS PIC 9(2) VALUE '10'. ---> ELEMENTARY ITEM
● Data Type can be numeric, alphabetic or alphanumeric. Numeric consists of only digits 0
to 9. Alphabetic consists of letters A to Z and spaces. Alphanumeric consists of digits,
letters and special characters.
● Sign can be used with numeric data. It can be either + or -.
● Decimal Point Position can be used with numeric data. Assumed position is the
position of decimal point and not included in the data.
● Length defines the number of bytes used by the data item.
Symbol Description
9 Numeric
A Alphabetic
X Alphanumeric
V Implicit Decimal
S Sign
P Assumed Decimal
Example
Following example shows the use of PIC clause:
IDENTIFICATION DIVISION.
PROGRAM-ID. HELLO.
DATA DIVISION.
WORKING-STORAGE SECTION.
01 WS-NUM1 PIC S9(3)V9(2).
01 WS-NUM2 PIC PPP999.
01 WS-NUM3 PIC S9(3)V9(2) VALUE -123.45.
01 WS-NAME PIC A(6) VALUE 'ABCDEF'.
01 WS-ID PIC X(5) VALUE 'A121$'.
PROCEDURE DIVISION.
DISPLAY "WS-NUM1 : "WS-NUM1.
DISPLAY "WS-NUM2 : "WS-NUM2.
DISPLAY "WS-NUM3 : "WS-NUM3.
DISPLAY "WS-NAME : "WS-NAME.
DISPLAY "WS-ID : "WS-ID.
STOP RUN.
//SAMPLE JOB(TESTJCL,XXXXXX),CLASS=A,MSGCLASS=C
//STEP1 EXEC PGM=HELLO
When you compile and execute the above program it produces the following result:
WS-NUM1 : +000.00
WS-NUM2 : .000000
WS-NUM3 : -123.45
WS-NAME : ABCDEF
WS-ID : A121$
Value Clause
Value clause is an optional clause which is used to initialize the data items. The values can be
numeric literal, alphanumeric literal or figurative constant. It can be used with both group and
elementary items.
Example
//SAMPLE JOB(TESTJCL,XXXXXX),CLASS=A,MSGCLASS=C
//STEP1 EXEC PGM=HELLO
When you compile and execute the above program it produces the following result:
WS-NUM1 : 03.5
WS-NAME : ABCD
WS-ID : 00
COBOL - Basic
Verbs
COBOL verbs are used in procedure division for data processing. A statement always start with
a COBOL verb. There are several COBOL verbs with different types of actions as following:
Input / Output Verbs
Input and Output verbs are used to get data from user and display the output of COBOL
programs. The following two verbs are used for this process:
Accept Verb
Accept Verb is used to get data from operating system like date, time and day or directly from
user. If program is accepting data from user then it needs to be passed through JCL. While
getting data from operating system then FROM option is included as shown in below example:
ACCEPT WS-STUDENT-NAME.
ACCEPT WS-DATE FROM SYSTEM-DATE.
Display Verb
Display Verb is used to display output of COBOL program.
DISPLAY WS-STUDENT-NAME.
DISPLAY "System date is : " WS-DATE.
COBOL PROGRAM
IDENTIFICATION DIVISION.
PROGRAM-ID. HELLO.
DATA DIVISION.
WORKING-STORAGE SECTION.
01 WS-STUDENT-NAME PIC X(25).
01 WS-DATE PIC X(10).
PROCEDURE DIVISION.
ACCEPT WS-STUDENT_NAME.
ACCEPT WS-DATE FROM DATE.
DISPLAY "Name : " WS-STUDENT_NAME.
DISPLAY "Date : " WS-DATE.
STOP RUN.
//SAMPLE JOB(TESTJCL,XXXXXX),CLASS=A,MSGCLASS=C
//STEP1 EXEC PGM=HELLO
//INPUT DD DSN=PROGRAM.DIRECTORY,DISP=SHR
//SYSIN DD *
TutorialsPoint
/*
When you compile and execute the above program it produces the following result:
Name : TutorialsPoint
Date : 2014-08-30
Initialize Verb
Initialize verb is used to initialize group item or elementary item. Data names with RENAME
clause cannot be initialized. Numeric data items are replaced by ZEROES. Alphanumeric or
Alphabetic data items are replaced by SPACES. If we include REPLACING term then data
items can be initialized to the given replacing value as shown in below example:
IDENTIFICATION DIVISION.
PROGRAM-ID. HELLO.
DATA DIVISION.
WORKING-STORAGE SECTION.
01 WS-NAME PIC A(30) VALUE 'ABCDEF'.
01 WS-ID PIC 9(5).
01 WS-ADDRESS.
05 WS-HOUSE-NUMBER PIC 9(3).
05 WS-COUNTRY PIC X(15).
05 WS-PINCODE PIC 9(6) VALUE 123456.
PROCEDURE DIVISION.
A000-FIRST-PARA.
INITIALIZE WS-NAME, WS-ADDRESS.
INITIALIZE WS-ID REPLACING NUMERIC DATA BY 12345.
DISPLAY "My name is : "WS-NAME.
DISPLAY "My ID is : "WS-ID.
DISPLAY "Address : "WS-ADDRESS.
DISPLAY "House Number : "WS-HOUSE-NUMBER.
DISPLAY "Country : "WS-COUNTRY.
DISPLAY "Pincode : "WS-PINCODE.
STOP RUN.
//SAMPLE JOB(TESTJCL,XXXXXX),CLASS=A,MSGCLASS=C
//STEP1 EXEC PGM=HELLO
When you compile and execute the above program it produces the following result:
My name is :
My ID is : 12345
Address : 000 000000
Country :
Pincode : 000000
Move Verb
Move verb is used to copy data from source data to destination data. It can be used on both
elementary and group data item. For group data items MOVE CORRESPONDING/CORR is
used. In try it option MOVE CORR is not working but on mainframe server it will work. For
moving data from a string MOVE(x:l) is used where x is the starting position and l is the length.
Data will be truncated if destination data item PIC clause is less than source data item PIC
clause. Also if destination data item PIC clause is more than source data item PIC clause then
ZEROS or SPACES will be added in extra bytes. It will be more clear by viewing the following
example:
IDENTIFICATION DIVISION.
PROGRAM-ID. HELLO.
DATA DIVISION.
WORKING-STORAGE SECTION.
01 WS-NUM1 PIC 9(9).
01 WS-NUM2 PIC 9(9).
01 WS-NUM3 PIC 9(5).
01 WS-NUM4 PIC 9(6).
01 WS-ADDRESS.
05 WS-HOUSE-NUMBER PIC 9(3).
05 WS-COUNTRY PIC X(5).
05 WS-PINCODE PIC 9(6).
01 WS-ADDRESS1.
05 WS-HOUSE-NUMBER1 PIC 9(3).
05 WS-COUNTRY1 PIC X(5).
05 WS-PINCODE1 PIC 9(6).
PROCEDURE DIVISION.
A000-FIRST-PARA.
MOVE 123456789 TO WS-NUM1.
MOVE WS-NUM1 TO WS-NUM2 WS-NUM3.
MOVE WS-NUM1(3:6) TO WS-NUM4.
MOVE 123 TO WS-HOUSE-NUMBER.
MOVE 'INDIA' TO WS-COUNTRY.
MOVE 112233 TO WS-PINCODE.
MOVE WS-ADDRESS TO WS-ADDRESS1.
DISPLAY "WS-NUM1 : " WS-NUM1
DISPLAY "WS-NUM2 : " WS-NUM2
DISPLAY "WS-NUM3 : " WS-NUM3
DISPLAY "WS-NUM4 : " WS-NUM4
DISPLAY "WS-ADDRESS : " WS-ADDRESS
DISPLAY "WS-ADDRESS1 : " WS-ADDRESS1
STOP RUN.
//SAMPLE JOB(TESTJCL,XXXXXX),CLASS=A,MSGCLASS=C
//STEP1 EXEC PGM=HELLO
When you compile and execute the above program it produces the following result:
WS-NUM1 : 123456789
WS-NUM2 : 123456789
WS-NUM3 : 56789
WS-NUM4 : 345678
WS-ADDRESS : 123INDIA112233
WS-ADDRESS1 : 123INDIA112233
Legal Moves
The below table gives the information about the legal moves:
Syntax
ADD A B TO C D
ADD A B C TO D GIVING E
In syntax-1, A, B, C are added and result is stored in C (C=A+B+C). A, B, D are added and
result is stored in D (D=A+B+D).
In syntax-3, Sub-group items with in WS-GROUP1 and WS_GROUP2 are added and result is
stored in WS-GROUP2.
Example
IDENTIFICATION DIVISION.
PROGRAM-ID. HELLO.
DATA DIVISION.
WORKING-STORAGE SECTION.
01 WS-NUM1 PIC 9(9) VALUE 10 .
01 WS-NUM2 PIC 9(9) VALUE 10.
01 WS-NUM3 PIC 9(9) VALUE 10.
01 WS-NUM4 PIC 9(9) VALUE 10.
01 WS-NUMA PIC 9(9) VALUE 10.
01 WS-NUMB PIC 9(9) VALUE 10.
01 WS-NUMC PIC 9(9) VALUE 10.
01 WS-NUMD PIC 9(9) VALUE 10.
01 WS-NUME PIC 9(9) VALUE 10.
PROCEDURE DIVISION.
ADD WS-NUM1 WS-NUM2 TO WS-NUM3 WS-NUM4.
ADD WS-NUMA WS-NUMB WS-NUMC TO WS-NUMD GIVING WS-NUME.
DISPLAY "WS-NUM1 : " WS-NUM1
DISPLAY "WS-NUM2 : " WS-NUM2
DISPLAY "WS-NUM3 : " WS-NUM3
DISPLAY "WS-NUM4 : " WS-NUM4
DISPLAY "WS-NUMA : " WS-NUMA
DISPLAY "WS-NUMB : " WS-NUMB
DISPLAY "WS-NUMC : " WS-NUMC
DISPLAY "WS-NUMD : " WS-NUMD
DISPLAY "WS-NUME : " WS-NUME
STOP RUN.
//SAMPLE JOB(TESTJCL,XXXXXX),CLASS=A,MSGCLASS=C
//STEP1 EXEC PGM=HELLO
When you compile and execute the above program it produces the following result:
WS-NUM1 : 000000010
WS-NUM2 : 000000010
WS-NUM3 : 000000030
WS-NUM4 : 000000030
WS-NUMA : 000000010
WS-NUMB : 000000010
WS-NUMC : 000000010
WS-NUMD : 000000010
WS-NUME : 000000040
Subtract Verb
Subtract Verb is used for subtraction operations. Following are the syntaxes which can be
used:
Syntax
SUBTRACT A B FROM C D
In syntax-3, Sub-group items with in WS-GROUP1 and WS_GROUP2 are subtracted and
result is stored in WS-GROUP2.
Example
IDENTIFICATION DIVISION.
PROGRAM-ID. HELLO.
DATA DIVISION.
WORKING-STORAGE SECTION.
01 WS-NUM1 PIC 9(9) VALUE 10 .
01 WS-NUM2 PIC 9(9) VALUE 10.
01 WS-NUM3 PIC 9(9) VALUE 100.
01 WS-NUM4 PIC 9(9) VALUE 100.
01 WS-NUMA PIC 9(9) VALUE 10.
01 WS-NUMB PIC 9(9) VALUE 10.
01 WS-NUMC PIC 9(9) VALUE 10.
01 WS-NUMD PIC 9(9) VALUE 100.
01 WS-NUME PIC 9(9) VALUE 10.
PROCEDURE DIVISION.
SUBTRACT WS-NUM1 WS-NUM2 FROM WS-NUM3 WS-NUM4.
SUBTRACT WS-NUMA WS-NUMB WS-NUMC FROM WS-NUMD GIVING WS-NUME.
DISPLAY "WS-NUM1 : " WS-NUM1
DISPLAY "WS-NUM2 : " WS-NUM2
DISPLAY "WS-NUM3 : " WS-NUM3
DISPLAY "WS-NUM4 : " WS-NUM4
DISPLAY "WS-NUMA : " WS-NUMA
DISPLAY "WS-NUMB : " WS-NUMB
DISPLAY "WS-NUMC : " WS-NUMC
DISPLAY "WS-NUMD : " WS-NUMD
DISPLAY "WS-NUME : " WS-NUME
STOP RUN.
//SAMPLE JOB(TESTJCL,XXXXXX),CLASS=A,MSGCLASS=C
//STEP1 EXEC PGM=HELLO
When you compile and execute the above program it produces the following result:
WS-NUM1 : 000000010
WS-NUM2 : 000000010
WS-NUM3 : 000000080
WS-NUM4 : 000000080
WS-NUMA : 000000010
WS-NUMB : 000000010
WS-NUMC : 000000010
WS-NUMD : 000000100
WS-NUME : 000000070
Multiply Verb
Multiply Verb is used for multiplication operations. Following are the syntaxes which can be
used:
Syntax
MULTIPLY A BY B C
MULTIPLY A BY B GIVING E
In syntax-1, Multiply A by B and store the result B (B=A*B). Multiply A by C and store the result
C (C=A*C).
Example
IDENTIFICATION DIVISION.
PROGRAM-ID. HELLO.
DATA DIVISION.
WORKING-STORAGE SECTION.
01 WS-NUM1 PIC 9(9) VALUE 10 .
01 WS-NUM2 PIC 9(9) VALUE 10.
01 WS-NUM3 PIC 9(9) VALUE 10.
01 WS-NUMA PIC 9(9) VALUE 10.
01 WS-NUMB PIC 9(9) VALUE 10.
01 WS-NUMC PIC 9(9) VALUE 10.
PROCEDURE DIVISION.
MULTIPLY WS-NUM1 BY WS-NUM2 WS-NUM3.
MULTIPLY WS-NUMA BY WS-NUMB GIVING WS-NUMC.
DISPLAY "WS-NUM1 : " WS-NUM1
DISPLAY "WS-NUM2 : " WS-NUM2
DISPLAY "WS-NUM3 : " WS-NUM3
DISPLAY "WS-NUMA : " WS-NUMA
DISPLAY "WS-NUMB : " WS-NUMB
DISPLAY "WS-NUMC : " WS-NUMC
STOP RUN.
//SAMPLE JOB(TESTJCL,XXXXXX),CLASS=A,MSGCLASS=C
//STEP1 EXEC PGM=HELLO
When you compile and execute the above program it produces the following result:
WS-NUM1 : 000000010
WS-NUM2 : 000000100
WS-NUM3 : 000000100
WS-NUMA : 000000010
WS-NUMB : 000000010
WS-NUMC : 000000100
Divide Verb
Divide Verb is used for division operations. Following are the syntaxes which can be used:
Syntax
DIVIDE A INTO B
Example
IDENTIFICATION DIVISION.
PROGRAM-ID. HELLO.
DATA DIVISION.
WORKING-STORAGE SECTION.
01 WS-NUM1 PIC 9(9) VALUE 5.
01 WS-NUM2 PIC 9(9) VALUE 250.
01 WS-NUMA PIC 9(9) VALUE 100.
01 WS-NUMB PIC 9(9) VALUE 15.
01 WS-NUMC PIC 9(9).
01 WS-REM PIC 9(9).
PROCEDURE DIVISION.
DIVIDE WS-NUM1 INTO WS-NUM2.
DIVIDE WS-NUMA BY WS-NUMB GIVING WS-NUMC REMAINDER WS-REM.
DISPLAY "WS-NUM1 : " WS-NUM1
DISPLAY "WS-NUM2 : " WS-NUM2
DISPLAY "WS-NUMA : " WS-NUMA
DISPLAY "WS-NUMB : " WS-NUMB
DISPLAY "WS-NUMC : " WS-NUMC
DISPLAY "WS-REM : " WS-REM
STOP RUN.
//SAMPLE JOB(TESTJCL,XXXXXX),CLASS=A,MSGCLASS=C
//STEP1 EXEC PGM=HELLO
When you compile and execute the above program it produces the following result:
WS-NUM1 : 000000005
WS-NUM2 : 000000050
WS-NUMA : 000000100
WS-NUMB : 000000015
WS-NUMC : 000000006
WS-REM : 000000010
Compute Statement
Compute statement is used to write arithmetic expressions in COBOL. This is a replacement
for Add, Subtract, Multiply and Divide.
Example
IDENTIFICATION DIVISION.
PROGRAM-ID. HELLO.
DATA DIVISION.
WORKING-STORAGE SECTION.
01 WS-NUM1 PIC 9(9) VALUE 10 .
01 WS-NUM2 PIC 9(9) VALUE 10.
01 WS-NUM3 PIC 9(9) VALUE 10.
01 WS-NUMA PIC 9(9) VALUE 50.
01 WS-NUMB PIC 9(9) VALUE 10.
01 WS-NUMC PIC 9(9).
PROCEDURE DIVISION.
COMPUTE WS-NUMC= (WS-NUM1 * WS-NUM2) - (WS-NUMA / WS-NUMB) + WS-NUM3.
DISPLAY "WS-NUM1 : " WS-NUM1
DISPLAY "WS-NUM2 : " WS-NUM2
DISPLAY "WS-NUM3 : " WS-NUM3
DISPLAY "WS-NUMA : " WS-NUMA
DISPLAY "WS-NUMB : " WS-NUMB
DISPLAY "WS-NUMC : " WS-NUMC
STOP RUN.
//SAMPLE JOB(TESTJCL,XXXXXX),CLASS=A,MSGCLASS=C
//STEP1 EXEC PGM=HELLO
When you compile and execute the above program it produces the following result:
WS-NUM1 : 000000010
WS-NUM2 : 000000010
WS-NUM3 : 000000010
WS-NUMA : 000000050
WS-NUMB : 000000010
WS-NUMC : 000000105
COBOL - Data
Layout
COBOL layout is description of use of each field and values present in it. Following are the
data description entries used in COBOL:
● Redefines Clause
● Renames Clause
● Usage Clause
● Copybooks
Redefines Clause
Redeifnes clause is used to define the same storage with different data description. If one or
more data items are not used simultaneously then the same storage can be utilized for another
data item. So same storage can be referred with different data items.
Syntax
Level numbers of redefined item and redefining item must be the same and it cannot be 66 or
88 level number. Do not use VALUE clause with redefining item. In File Section, do not use
redefines clause with 01 level number. Redefines definition must be the next data description
you want to redefine. Redefining item will always have the same value as redefined item.
Example
IDENTIFICATION DIVISION.
PROGRAM-ID. HELLO.
DATA DIVISION.
WORKING-STORAGE SECTION.
01 WS-DESCRIPTION.
05 WS-DATE1 VALUE '20140831'.
10 WS-YEAR PIC X(4).
10 WS-MONTH PIC X(2).
10 WS-DATE PIC X(2).
05 WS-DATE2 REDEFINES WS-DATE1 PIC 9(8).
PROCEDURE DIVISION.
DISPLAY "WS-DATE1 : "WS-DATE1.
DISPLAY "WS-DATE2 : "WS-DATE2.
STOP RUN.
//SAMPLE JOB(TESTJCL,XXXXXX),CLASS=A,MSGCLASS=C
//STEP1 EXEC PGM=HELLO
When you compile and execute the above program it produces the following result:
WS-DATE1 : 20140831
WS-DATE2 : 20140831
Renames Clause
Renames clause is used to give different name to existing data items. It is used to re-group the
data names and give a new name to them. The new data names can rename across groups or
elementary items. 66 level number is reserved for renames.
Syntax
01 WS-OLD.
10 WS-A PIC 9(12).
10 WS-B PIC X(20).
10 WS-C PIC A(25).
10 WS-D PIC X(12).
66 WS-NEW RENAMES WS-A THRU WS-C.
Renames is possible at same level only. In above example WS-A, WS-B and WS-C are at
same level. Renames definition must be the next data description you want to rename. Do not
use Renames with 01,77 or 66 level number. The data names used for renames must come in
sequence. Data items with occur clause cannot be renamed.
Example
IDENTIFICATION DIVISION.
PROGRAM-ID. HELLO.
DATA DIVISION.
WORKING-STORAGE SECTION.
01 WS-DESCRIPTION.
05 WS-NUM.
10 WS-NUM1 PIC 9(2) VALUE 20.
10 WS-NUM2 PIC 9(2) VALUE 56.
05 WS-CHAR.
10 WS-CHAR1 PIC X(2) VALUE 'AA'.
10 WS-CHAR2 PIC X(2) VALUE 'BB'.
66 WS-RENAME RENAMES WS-NUM2 THRU WS-CHAR2.
PROCEDURE DIVISION.
DISPLAY "WS-RENAME : " WS-RENAME.
STOP RUN.
//SAMPLE JOB(TESTJCL,XXXXXX),CLASS=A,MSGCLASS=C
//STEP1 EXEC PGM=HELLO
When you compile and execute the above program it produces the following result:
WS-RENAME : 56AABB
Usage Clause
Usage clause tells the operating system in which format data is stored. It can not be used with
66 or 88 level number. If usage clause is specified on group then all the elementary items will
have same usage clause. Following are the different options with Usage clause:
Display
Data item is stored in ASCII format and each character will take 1 byte. It is Default usage.
COMPUTATIONAL / COMP
Data item is stored in binary format. In this data items must be integer.
COMP-1
Data item is similar to Real or Float and is represented as a single precision floating point
number. Internally data is stored in hexadecimal format. In this do not specify PIC clause. Here
1 word is equal to 4 bytes.
COMP-2
Data item is similar to Long or Double and is represented as double precision floating point
number. Internally data is stored in hexadecimal format. In this do not specify PIC clause. Here
2 word is equal to 8 bytes.
COMP-3
Data item is stores in pack decimal format. Each digit occupies half a byte (1 nibble) and sign is
stored at right most nibble.
Copybooks
A COBOL copybook is selection of code that defines data structures. If a particular data
structure is used in many programs then instead of writing same data structure again we can
use copybooks. We use COPY statement to include copybook in program. COPY statement is
used in Working-Storage Section.
DATA DIVISION.
WORKING-STORAGE SECTION.
COPY ABC.
Here ABC is copybook name. Below data items in ABC copybook can be used inside program.
01 WS-DESCRIPTION.
05 WS-NUM.
10 WS-NUM1 PIC 9(2) VALUE 20.
10 WS-NUM2 PIC 9(2) VALUE 56.
05 WS-CHAR.
10 WS-CHAR1 PIC X(2) VALUE 'AA'.
10 WS-CHAR2 PIC X(2) VALUE 'BB'.
COBOL - Condition
Statements
Condition statements are used to change the execution flow depending on certain conditions
specified by the programmer. Conditional statements will always evaluate to true or false.
Conditions are used in IF, Evaluate and Perform statements. The different types of conditions
are as follows:
● IF Condition Statement
● Relation Condition
● Sign Condition
● Class Condition
● Condition-Name Condition
● Negated Condition
● Combined Condition
IF Condition Statement
IF statement checks for conditions. If condition is True the IF block is executed and if condition
is false ELSE block is executed.
END-IF is used to end the IF block. To end the IF block period can be used instead of END-IF.
But it is always preferable to use END-IF for multiple IF blocks.
Nested-IF : IF blocks appearing inside another IF block. There is no limit to the depth of nested
IF statements.
Syntax
Following is the syntax for If condition statements:
IF [condition] THEN
[COBOL statements]
ELSE
[COBOL statements]
END-IF.
Example
IDENTIFICATION DIVISION.
PROGRAM-ID. HELLO.
DATA DIVISION.
WORKING-STORAGE SECTION.
01 WS-NUM1 PIC 9(9).
01 WS-NUM2 PIC 9(9).
01 WS-NUM3 PIC 9(5).
01 WS-NUM4 PIC 9(6).
PROCEDURE DIVISION.
A000-FIRST-PARA.
MOVE 25 TO WS-NUM1 WS-NUM3.
MOVE 15 TO WS-NUM2 WS-NUM4.
IF WS-NUM1 > WS-NUM2 THEN
DISPLAY 'IN LOOP 1 - IF BLOCK'
IF WS-NUM3 = WS-NUM4 THEN
DISPLAY 'IN LOOP 2 - IF BLOCK'
ELSE
DISPLAY 'IN LOOP 2 - ELSE BLOCK'
END-IF
ELSE
DISPLAY 'IN LOOP 1 - ELSE BLOCK'
END-IF.
STOP RUN.
//SAMPLE JOB(TESTJCL,XXXXXX),CLASS=A,MSGCLASS=C
//STEP1 EXEC PGM=HELLO
When you compile and execute the above program it produces the following result:
IN LOOP 1 - IF BLOCK
Relation Condition
Relation condition is used to evaluate true and false for two numeric values. Algebraic
comparison of numeric fields is done regardless of size and usage clause.
If two non-numeric operands of equal size are compared then characters are compared from
left with the corresponding positions till the end is reached. The operand containing the greater
character is declared greater.
If two non-numeric operands of unequal size are compared then the shorter data item is
appended with spaces at the end till the size of the operands become equal and then
compared according to the rules mentioned in previous point.
Syntax
Following is the syntax for Relation condition statements:
[IS] [NOT]
Example
IDENTIFICATION DIVISION.
PROGRAM-ID. HELLO.
DATA DIVISION.
WORKING-STORAGE SECTION.
01 WS-NUM1 PIC 9(9).
01 WS-NUM2 PIC 9(9).
PROCEDURE DIVISION.
A000-FIRST-PARA.
MOVE 25 TO WS-NUM1.
MOVE 15 TO WS-NUM2.
IF WS-NUM1 IS GREATER THAN OR EQUAL TO WS-NUM2 THEN
DISPLAY 'WS-NUM1 IS GREATER THAN WS-NUM2'
ELSE
DISPLAY 'WS-NUM1 IS LESS THAN WS-NUM2'
END-IF.
STOP RUN.
//SAMPLE JOB(TESTJCL,XXXXXX),CLASS=A,MSGCLASS=C
//STEP1 EXEC PGM=HELLO
When you compile and execute the above program it produces the following result:
Sign Condition
Sign condition is used to check the sign of numeric operand. The operand can be :
Syntax
Following is the syntax for Sign condition statements:
[IS] [NOT]
Example
IDENTIFICATION DIVISION.
PROGRAM-ID. HELLO.
DATA DIVISION.
WORKING-STORAGE SECTION.
01 WS-NUM1 PIC S9(9) VALUE -1234.
01 WS-NUM2 PIC S9(9) VALUE 123456.
PROCEDURE DIVISION.
A000-FIRST-PARA.
IF WS-NUM1 IS POSITIVE THEN
DISPLAY 'WS-NUM1 IS POSITIVE'.
IF WS-NUM1 IS NEGATIVE THEN
DISPLAY 'WS-NUM1 IS NEGATIVE'.
IF WS-NUM1 IS ZERO THEN
DISPLAY 'WS-NUM1 IS ZERO'.
IF WS-NUM2 IS POSITIVE THEN
DISPLAY 'WS-NUM2 IS POSITIVE'.
STOP RUN.
//SAMPLE JOB(TESTJCL,XXXXXX),CLASS=A,MSGCLASS=C
//STEP1 EXEC PGM=HELLO
When you compile and execute the above program it produces the following result:
WS-NUM1 IS NEGATIVE
WS-NUM2 IS POSITIVE
Class Condition
Sign condition is used to check if operand contains only alphabets or numeric data . Spaces
are considered in ALPHABETIC, ALPHABETIC-LOWER, ALPHABETIC-UPPER.
Syntax
Following is the syntax for Class condition statements:
[IS] [NOT]
Example
IDENTIFICATION DIVISION.
PROGRAM-ID. HELLO.
DATA DIVISION.
WORKING-STORAGE SECTION.
01 WS-NUM1 PIC X(9) VALUE 'ABCD '.
01 WS-NUM2 PIC 9(9) VALUE 123456789.
PROCEDURE DIVISION.
A000-FIRST-PARA.
IF WS-NUM1 IS ALPHABETIC THEN
DISPLAY 'WS-NUM1 IS ALPHABETIC'.
IF WS-NUM1 IS NUMERIC THEN
DISPLAY 'WS-NUM1 IS NUMERIC'.
IF WS-NUM2 IS NUMERIC THEN
DISPLAY 'WS-NUM1 IS NUMERIC'.
STOP RUN.
//SAMPLE JOB(TESTJCL,XXXXXX),CLASS=A,MSGCLASS=C
//STEP1 EXEC PGM=HELLO
When you compile and execute the above program it produces the following result:
WS-NUM1 IS ALPHABETIC
WS-NUM1 IS NUMERIC
Condition-name Condition
A condition name is a user-defined name. It contains set of values specified by the user. It
behaves like Boolean variables. They are defined with 88 level number. It will not have a PIC
clause.
Syntax
Following is the syntax for user-defined condition statements:
Example
IDENTIFICATION DIVISION.
PROGRAM-ID. HELLO.
DATA DIVISION.
WORKING-STORAGE SECTION.
01 WS-NUM PIC 9(3).
88 PASS VALUES ARE 041 THRU 100.
88 FAIL VALUES ARE 000 THRU 40.
PROCEDURE DIVISION.
A000-FIRST-PARA.
MOVE 65 TO WS-NUM.
IF PASS
DISPLAY 'Passed with ' WS-NUM ' marks'.
IF FAIL
DISPLAY 'FAILED with ' WS-NUM 'marks'.
STOP RUN.
//SAMPLE JOB(TESTJCL,XXXXXX),CLASS=A,MSGCLASS=C
//STEP1 EXEC PGM=HELLO
When you compile and execute the above program it produces the following result:
Negated Condition
Negate condition is given by using NOT keyword. If condition is true and we have given NOT in
front of it then its final value will be false.
Syntax
Following is the syntax for Negated condition statements:
IF NOT [CONDITION]
COBOL Statements
END-IF.
Example
IDENTIFICATION DIVISION.
PROGRAM-ID. HELLO.
DATA DIVISION.
WORKING-STORAGE SECTION.
01 WS-NUM1 PIC 9(2) VALUE 20.
01 WS-NUM2 PIC 9(9) VALUE 25.
PROCEDURE DIVISION.
A000-FIRST-PARA.
IF NOT WS-NUM1 IS LESS THAN WS-NUM2 THEN
DISPLAY 'IF-BLOCK'
ELSE
DISPLAY 'ELSE-BLOCK'
END-IF.
STOP RUN.
//SAMPLE JOB(TESTJCL,XXXXXX),CLASS=A,MSGCLASS=C
//STEP1 EXEC PGM=HELLO
When you compile and execute the above program it produces the following result:
ELSE-BLOCK
Combined Condition
Combined condition contains two or more conditions by use of logical operators AND or OR.
Syntax
Following is the syntax for combined condition statements:
Example
IDENTIFICATION DIVISION.
PROGRAM-ID. HELLO.
DATA DIVISION.
WORKING-STORAGE SECTION.
01 WS-NUM1 PIC 9(2) VALUE 20.
01 WS-NUM2 PIC 9(2) VALUE 25.
01 WS-NUM3 PIC 9(2) VALUE 20.
PROCEDURE DIVISION.
A000-FIRST-PARA.
IF WS-NUM1 IS LESS THAN WS-NUM2 AND WS-NUM1=WS-NUM3 THEN
DISPLAY 'Both condition OK'
ELSE
DISPLAY 'Error'
END-IF.
STOP RUN.
When you compile and execute the above program it produces the following result:
Both condition OK
Evaluate Verb
Evaluate verb is replacement of series of IF-ELSE statement. It can be used to evaluate more
than one condition. It is similar to SWITCH statement in C programs.
Example
IDENTIFICATION DIVISION.
PROGRAM-ID. HELLO.
DATA DIVISION.
WORKING-STORAGE SECTION.
01 WS-A PIC 9 VALUE 0.
PROCEDURE DIVISION.
MOVE 3 TO WS-A.
EVALUATE TRUE
WHEN WS-A > 2
DISPLAY 'WS-A GREATER THAN 2'
WHEN WS-A < 0
DISPLAY 'WS-A LESS THAN 0'
WHEN OTHER
DISPLAY 'INVALID VALUE OF WS-A'
END-EVALUATE.
STOP RUN.
//SAMPLE JOB(TESTJCL,XXXXXX),CLASS=A,MSGCLASS=C
//STEP1 EXEC PGM=HELLO
When you compile and execute the above program it produces the following result:
● Perform Thru
● Perform Until
● Perform Times
● Perform Varying
Perform Thru
Perform Thru is used to execute a series of paragraph by giving the first and last paragraph
names in the sequence. After executing the last paragraph control will be returned back.
In-line Perform
Statements inside the PERFORM will be executed till END-PERFORM is reached.
Syntax
PERFORM
DISPLAY 'HELLO WORLD'
END-PERFORM.
Out-of-line Perform
Statement is executed in one paragraph and then control is transferred to other paragraph or
section.
Syntax
Example
IDENTIFICATION DIVISION.
PROGRAM-ID. HELLO.
PROCEDURE DIVISION.
A-PARA.
PERFORM DISPLAY 'IN A-PARA'
END-PERFORM.
PERFORM C-PARA THRU E-PARA.
B-PARA.
DISPLAY 'IN B-PARA'.
STOP RUN.
C-PARA.
DISPLAY 'IN C-PARA'.
D-PARA.
DISPLAY 'IN D-PARA'.
E-PARA.
DISPLAY 'IN E-PARA'.
//SAMPLE JOB(TESTJCL,XXXXXX),CLASS=A,MSGCLASS=C
//STEP1 EXEC PGM=HELLO
When you compile and execute the above program it produces the following result:
IN A-PARA
IN C-PARA
IN D-PARA
IN E-PARA
IN B-PARA
Perform Until
In perform until, paragraph will be executed until the given condition becomes true. With test
before is default condition and it indicates condition is checked before the execution of
statements in paragraph.
Syntax
Example
IDENTIFICATION DIVISION.
PROGRAM-ID. HELLO.
DATA DIVISION.
WORKING-STORAGE SECTION.
01 WS-CNT PIC 9(1) VALUE 0.
PROCEDURE DIVISION.
A-PARA.
PERFORM B-PARA WITH TEST AFTER UNTIL WS-CNT>3.
STOP RUN.
B-PARA.
DISPLAY 'WS-CNT : 'WS-CNT.
ADD 1 TO WS-CNT.
//SAMPLE JOB(TESTJCL,XXXXXX),CLASS=A,MSGCLASS=C
//STEP1 EXEC PGM=HELLO
When you compile and execute the above program it produces the following result:
WS-CNT : 0
WS-CNT : 1
WS-CNT : 2
WS-CNT : 3
Perform Times
In perform times, paragraph will be executed the number of times specified.
Syntax
Example
IDENTIFICATION DIVISION.
PROGRAM-ID. HELLO.
PROCEDURE DIVISION.
A-PARA.
PERFORM B-PARA 3 TIMES.
STOP RUN.
B-PARA.
DISPLAY 'IN B-PARA'.
//SAMPLE JOB(TESTJCL,XXXXXX),CLASS=A,MSGCLASS=C
//STEP1 EXEC PGM=HELLO
When you compile and execute the above program it produces the following result:
IN B-PARA
IN B-PARA
IN B-PARA
Perform Varying
In perform varying, paragraph will be executed till the condition in Until phrase becomes true.
Syntax
Example
IDENTIFICATION DIVISION.
PROGRAM-ID. HELLO.
DATA DIVISION.
WORKING-STORAGE SECTION.
01 WS-A PIC 9 VALUE 0.
PROCEDURE DIVISION.
A-PARA.
PERFORM B-PARA VARYING WS-A FROM 1 BY 1 UNTIL WS-A=5
STOP RUN.
B-PARA.
DISPLAY 'IN B-PARA ' WS-A.
//SAMPLE JOB(TESTJCL,XXXXXX),CLASS=A,MSGCLASS=C
//STEP1 EXEC PGM=HELLO
When you compile and execute the above program it produces the following result:
IN B-PARA 1
IN B-PARA 2
IN B-PARA 3
IN B-PARA 4
GO TO Statement
GO TO statement is used to change the flow of execution of the program. In GO TO
statements transfer goes only in forward direction. It is used to exit the paragraph. The different
types of GO TO statements are :
Unconditional GO TO
GO TO para-name.
Conditional GO TO
Example
IDENTIFICATION DIVISION.
PROGRAM-ID. HELLO.
DATA DIVISION.
WORKING-STORAGE SECTION.
01 WS-A PIC 9 VALUE 2.
PROCEDURE DIVISION.
A-PARA.
DISPLAY 'IN A-PARA'
GO TO B-PARA.
B-PARA.
DISPLAY 'IN B-PARA '.
GO TO C-PARA D-PARA DEPENDING ON WS-A.
C-PARA.
DISPLAY 'IN C-PARA '.
D-PARA.
DISPLAY 'IN D-PARA '.
STOP RUN.
//SAMPLE JOB(TESTJCL,XXXXXX),CLASS=A,MSGCLASS=C
//STEP1 EXEC PGM=HELLO
When you compile and execute the above program it produces the following result:
IN A-PARA
IN B-PARA
IN D-PARA
COBOL - String
Handling
String handling statements in COBOL are used to do multiple functional operations on strings.
Following are the string handling statements:
● Inspect
● String
● Unstring
Inspect
Inspect verb is used to count or replace characters in a string. String operations can be
performed on alphanumeric, numeric or alphabetic. Inspect operations are performed from left
to right character. The options used for the string operations are as follows:
Tallying
Tallying option is used to count the string characters.
Syntax
INSPECT input-string
TALLYING output-count FOR ALL CHARACTERS
Example
IDENTIFICATION DIVISION.
PROGRAM-ID. HELLO.
DATA DIVISION.
WORKING-STORAGE SECTION.
01 WS-CNT1 PIC 9(2) VALUE 0.
01 WS-CNT2 PIC 9(2) VALUE 0.
01 WS-STRING PIC X(15) VALUE 'ABCDACDADEAAAFF'.
PROCEDURE DIVISION.
INSPECT WS-STRING TALLYING WS-CNT1 FOR ALL CHARACTERS.
DISPLAY "WS-CNT1 : "WS-CNT1.
INSPECT WS-STRING TALLYING WS-CNT2 FOR ALL 'A'.
DISPLAY "WS-CNT2 : "WS-CNT2
STOP RUN.
//SAMPLE JOB(TESTJCL,XXXXXX),CLASS=A,MSGCLASS=C
//STEP1 EXEC PGM=HELLO
When you compile and execute the above program it produces the following result:
WS-CNT1 : 15
WS-CNT2 : 06
Replacing
Replacing option is used to replace the string characters.
Syntax
● input-string : The string whose characters are to be replaced from char1 to char2
Example
IDENTIFICATION DIVISION.
PROGRAM-ID. HELLO.
DATA DIVISION.
WORKING-STORAGE SECTION.
01 WS-STRING PIC X(15) VALUE 'ABCDACDADEAAAFF'.
PROCEDURE DIVISION.
DISPLAY "OLD STRING : "WS-STRING.
INSPECT WS-STRING REPLACING ALL 'A' BY 'X'.
DISPLAY "NEW STRING : "WS-STRING.
STOP RUN.
//SAMPLE JOB(TESTJCL,XXXXXX),CLASS=A,MSGCLASS=C
//STEP1 EXEC PGM=HELLO
When you compile and execute the above program it produces the following result:
String
String verb is used to concatenate the strings. Using STRING statement, two or more strings of
characters can be combined to form a longer string. Delimited By clause is compulsory.
Syntax
Example
IDENTIFICATION DIVISION.
PROGRAM-ID. HELLO.
DATA DIVISION.
WORKING-STORAGE SECTION.
01 WS-STRING PIC A(30).
01 WS-STR1 PIC A(15) VALUE 'Tutorialspoint'.
01 WS-STR2 PIC A(7) VALUE 'Welcome'.
01 WS-STR3 PIC A(7) VALUE 'To AND'.
01 WS-COUNT PIC 99 VALUE 1.
PROCEDURE DIVISION.
STRING WS-STR2 DELIMITED BY SIZE
WS-STR3 DELIMITED BY SPACE
WS-STR1 DELIMITED BY SIZE
INTO WS-STRING
WITH POINTER WS-COUNT
ON OVERFLOW DISPLAY 'OVERFLOW!'
END-STRING.
DISPLAY 'WS-STRING : 'WS-STRING.
DISPLAY 'WS-COUNT : 'WS-COUNT.
STOP RUN.
//SAMPLE JOB(TESTJCL,XXXXXX),CLASS=A,MSGCLASS=C
//STEP1 EXEC PGM=HELLO
When you compile and execute the above program it produces the following result:
WS-STRING : WelcomeToTutorialspoint
WS-COUNT : 25
Unstring
Unstring verb is used to split one string into multiple sub-strings. Delimited By clause is
compulsory.
Syntax
Following is the syntax for Unstring verb:
Example
IDENTIFICATION DIVISION.
PROGRAM-ID. HELLO.
DATA DIVISION.
WORKING-STORAGE SECTION.
01 WS-STRING PIC A(30) VALUE 'WELCOME TO TUTORIALSPOINT'.
01 WS-STR1 PIC A(7).
01 WS-STR2 PIC A(2).
01 WS-STR3 PIC A(15).
01 WS-COUNT PIC 99 VALUE 1.
PROCEDURE DIVISION.
UNSTRING WS-STRING DELIMITED BY SPACE
INTO WS-STR1, WS-STR2, WS-STR3
END-UNSTRING.
DISPLAY 'WS-STR1 : 'WS-STR1.
DISPLAY 'WS-STR2 : 'WS-STR2.
DISPLAY 'WS-STR3 : 'WS-STR3.
STOP RUN.
//SAMPLE JOB(TESTJCL,XXXXXX),CLASS=A,MSGCLASS=C
//STEP1 EXEC PGM=HELLO
When you compile and execute the above program it produces the following result:
WS-STR1 : WELCOME
WS-STR2 : TO
WS-STR3 : TUTORIALSPOINT
COBOL - Table
Processing
Arrays in COBOL are known as tables. An array is a linear data structure and is collection of
individual data items of same type. Data items of a table are internally sorted.
Table Declaration
Table is declared in Data Division. Occurs clause is used to define a table. Occurs clause
indicates the repetition of data name definition. Occurs clause can be used only with level
numbers starting from 02 to 49. Do not use occurs clause with Redefines. Description of one
dimensional and two dimensional table is as follows:
01 WS-TABLE.
05 WS-A PIC A(10) OCCURS 10 TIMES.
Example
IDENTIFICATION DIVISION.
PROGRAM-ID. HELLO.
DATA DIVISION.
WORKING-STORAGE SECTION.
01 WS-TABLE.
05 WS-A PIC A(10) VALUE 'TUTORIALS' OCCURS 5 TIMES.
PROCEDURE DIVISION.
DISPLAY "ONE-D TABLE : "WS-TABLE.
STOP RUN.
//SAMPLE JOB(TESTJCL,XXXXXX),CLASS=A,MSGCLASS=C
//STEP1 EXEC PGM=HELLO
When you compile and execute the above program it produces the following result:
Syntax
01 WS-TABLE.
05 WS-A OCCURS 10 TIMES.
10 WS-B PIC A(10).
10 WS-C OCCURS 5 TIMES.
15 WS-D PIC X(6).
Example
IDENTIFICATION DIVISION.
PROGRAM-ID. HELLO.
DATA DIVISION.
WORKING-STORAGE SECTION.
01 WS-TABLE.
05 WS-A OCCURS 2 TIMES.
10 WS-B PIC A(10) VALUE ' TUTORIALS'.
10 WS-C OCCURS 2 TIMES.
15 WS-D PIC X(6) VALUE ' POINT'.
PROCEDURE DIVISION.
DISPLAY "TWO-D TABLE : "WS-TABLE.
STOP RUN.
//SAMPLE JOB(TESTJCL,XXXXXX),CLASS=A,MSGCLASS=C
//STEP1 EXEC PGM=HELLO
When you compile and execute the above program it produces the following result:
Subscript
Table individual elements can be accessed by using subscript. Subscript valus can range from
1 to the number of times the table occurs. Subscript can be any positive number. Subscript
does not require any declaration in data division. It is automatically created with occurs clause.
Example
IDENTIFICATION DIVISION.
PROGRAM-ID. HELLO.
DATA DIVISION.
WORKING-STORAGE SECTION.
01 WS-TABLE.
05 WS-A OCCURS 3 TIMES.
10 WS-B PIC A(2).
10 WS-C OCCURS 2 TIMES.
15 WS-D PIC X(3).
PROCEDURE DIVISION.
MOVE '12ABCDEF34GHIJKL56MNOPQR' TO WS-TABLE.
DISPLAY 'WS-TABLE : ' WS-TABLE.
DISPLAY 'WS-A(1) : ' WS-A(1).
DISPLAY 'WS-C(1,1) : ' WS-C(1,1).
DISPLAY 'WS-C(1,2) : ' WS-C(1,2).
DISPLAY 'WS-A(2) : ' WS-A(2).
DISPLAY 'WS-C(2,1) : ' WS-C(2,1).
DISPLAY 'WS-C(2,2) : ' WS-C(2,2).
DISPLAY 'WS-A(3) : ' WS-A(3).
DISPLAY 'WS-C(3,1) : ' WS-C(3,1).
DISPLAY 'WS-C(3,2) : ' WS-C(3,2).
STOP RUN.
When you compile and execute the above program it produces the following result:
WS-TABLE : 12ABCDEF34GHIJKL56MNOPQR
WS-A(1) : 12ABCDEF
WS-C(1,1) : ABC
WS-C(1,2) : DEF
WS-A(2) : 34GHIJKL
WS-C(2,1) : GHI
WS-C(2,2) : JKL
WS-A(3) : 56MNOPQR
WS-C(3,1) : MNO
WS-C(3,2) : PQR
Index
Table elements can also be accessed using index. An index is a displacement of element from
the start of the table. An index is declared with Occurs clause using INDEXED BY clause. The
value of index can be changed using SET statement and PERFORM Varying option.
Syntax
01 WS-TABLE.
05 WS-A PIC A(10) OCCURS 10 TIMES INDEXED BY I.
Example
IDENTIFICATION DIVISION.
PROGRAM-ID. HELLO.
DATA DIVISION.
WORKING-STORAGE SECTION.
01 WS-TABLE.
05 WS-A OCCURS 3 TIMES INDEXED BY I.
10 WS-B PIC A(2).
10 WS-C OCCURS 2 TIMES INDEXED BY J.
15 WS-D PIC X(3).
PROCEDURE DIVISION.
MOVE '12ABCDEF34GHIJKL56MNOPQR' TO WS-TABLE.
PERFORM A-PARA VARYING I FROM 1 BY 1 UNTIL I >3
STOP RUN.
A-PARA.
PERFORM C-PARA VARYING J FROM 1 BY 1 UNTIL J>2.
C-PARA.
DISPLAY WS-C(I,J).
//SAMPLE JOB(TESTJCL,XXXXXX),CLASS=A,MSGCLASS=C
//STEP1 EXEC PGM=HELLO
When you compile and execute the above program it produces the following result:
ABC
DEF
GHI
JKL
MNO
PQR
Set Statement
Set statement is used to change the index value. Set verb is used to initialize, increment or
decrement the index vale. It is used with Search and Search All to locate elements in table.
Syntax
SET I J TO positive-number
SET I TO J
SET I TO 5
SET I J UP BY 1
SET J DOWN BY 5
Example
IDENTIFICATION DIVISION.
PROGRAM-ID. HELLO.
DATA DIVISION.
WORKING-STORAGE SECTION.
01 WS-TABLE.
05 WS-A OCCURS 3 TIMES INDEXED BY I.
10 WS-B PIC A(2).
10 WS-C OCCURS 2 TIMES INDEXED BY J.
15 WS-D PIC X(3).
PROCEDURE DIVISION.
MOVE '12ABCDEF34GHIJKL56MNOPQR' TO WS-TABLE.
SET I J TO 1.
DISPLAY WS-C(I,J).
SET I J UP BY 1.
DISPLAY WS-C(I,J).
STOP RUN.
//SAMPLE JOB(TESTJCL,XXXXXX),CLASS=A,MSGCLASS=C
//STEP1 EXEC PGM=HELLO
When you compile and execute the above program it produces the following result:
ABC
JKL
Search
Search is a linear search method, which is used to find elements inside the table. It can be
performed on sorted as well as unsorted table. It is used only for tables declared by Index
phrase. It starts with initial value of index. If element is not found, then index is automatically
incremented by 1 and it continues till end of table.
Example
IDENTIFICATION DIVISION.
PROGRAM-ID. HELLO.
DATA DIVISION.
WORKING-STORAGE SECTION.
01 WS-TABLE.
05 WS-A PIC X(1) OCCURS 18 TIMES INDEXED BY I.
01 WS-SRCH PIC A(1) VALUE 'M'.
PROCEDURE DIVISION.
MOVE 'ABCDEFGHIJKLMNOPQR' TO WS-TABLE.
SET I TO 1.
SEARCH WS-A
AT END DISPLAY 'M NOT FOUND IN TABLE'
WHEN WS-A(I)=WS-SRCH
DISPLAY 'LETTER M FOUND IN TABLE'
END-SEARCH.
STOP RUN.
//SAMPLE JOB(TESTJCL,XXXXXX),CLASS=A,MSGCLASS=C
//STEP1 EXEC PGM=HELLO
When you compile and execute the above program it produces the following result:
Search All
Seach All is binary search method, which is used to find elements inside the table. Table must
be in sorted order for Search All option. The index does not require initialization. In binary
search table is divided into two halves and determines in which half element is present. This
process repeats till element is found or end is reached.
Example
IDENTIFICATION DIVISION.
PROGRAM-ID. HELLO.
DATA DIVISION.
WORKING-STORAGE SECTION.
01 WS-TABLE.
05 WS-RECORD OCCURS 10 TIMES ASCENDING KEY IS WS-NUM INDEXED BY I.
10 WS-NUM PIC 9(2).
10 WS-NAME PIC A(3).
PROCEDURE DIVISION.
MOVE '12ABC56DEF34GHI78JKL93MNO11PQR' TO WS-TABLE.
SEARCH ALL WS-RECORD
AT END DISPLAY 'RECORD NOT FOUND'
WHEN WS-NUM(I)=93
DISPLAY 'RECORD FOUND '
DISPLAY WS-NUM(I)
DISPLAY WS-NAME(I)
END-SEARCH.
When you compile and execute the above program it produces the following result:
RECORD FOUND
93
MNO
COBOL - File
Handling
The concept of files in COBOL is different from that in C/C++. While learning the basics of 'File'
in COBOL, the concepts of both languages should not be co-related. Simple text files cannot
be used in COBOL, instead PS(Physical Sequential) and VSAM files are used. PS files will
be discussed in this module.
To understand file handling in COBOL, one must know the basic terms. These terms only serve
to understand the fundamentals of file handling. Further in-depth terminology would be
discussed in 'File Handling Verbs' module. Following are the basic terms:
● Field
● Record
● Physical Record
● Logical Record
● File
Below example will help in understanding these terms:
Field
Field is used to indicate the data stored about an element. It represents a single element as
shown in above example like student id, name, marks, total marks and percentage. The
number of characters in any field is known as field size like student name can have 10
characters. Fields can have following attributes:
● Primary keys are those fields that are unique to each record and are used to identify a
particular record. For example, in students marks file, each student will be having
unique student id which forms the primary key.
● Secondary keys are unique or non-unique fields that are used to search for related
data. For example, in students marks file, full name of student can be used as
secondary key when student id in not known.
● Descriptors fields are usually used to describe an entity. For example, in students
marks file, marks, percentage fields adds meaning to the record are known descriptors.
Record
Record is a collection of fields that is used to describe an entity. One or more fields together
form a record. For example, in students marks file, student id, name, marks, total marks and
percentage form one record. The cumulative size of all the fields in a record is known as record
size. The records present in a file may be of fixed length or variable length.
Physical Record
Physical record is the information which exists on the external device. It is also known block.
Logical Record
Logical record is the information used by the program. In COBOL program, only one record can
be handled at any point of time and it is called as logical record.
File
File is a collection of related records. For example the student marks file consists of records of
all the students.
COBOL - File
Organization
File organization indicates how the records are organized in a file. There are different types of
organizations for files so as to increase their efficiency of accessing the records. Following are
the types of file organization schemes:
The syntaxes, in this module, mentioned along with their respective terms only refer to their
usage in the program. The complete programs using these syntaxes would be discussed in
'File handling Verbs' module.
● Records can be read in sequential order. For reading 10th record, all the first 9 records
should be read.
● Records are written in sequential order. In between new records cannot be inserted.
New record is always inserted at the end of file.
● After placing a record into sequential file, deletion, shorten or lengthen of record is not
possible.
● Order of the records, once inserted can never be changed.
● Updation of record is possible. A record can be overwritten, if the new record length is
same as old record length.
● Sequential output files are good option for printing.
Syntax
INPUT-OUTPUT SECTION.
FILE-CONTROL.
SELECT file-name ASSIGN TO dd-name-jcl
ORGANIZATION IS SEQUENTIAL
● Records can be read in sequential order just like in sequential file organization.
● Records can be accessed randomly if primary key is known. Index file is used to get the
address of record and then record is fetched from data file.
● Sorted index is maintained in this file system which relates the key value to the position
of the record in the file
● Alternate index can also be created to fetch the records.
Syntax
INPUT-OUTPUT SECTION.
FILE-CONTROL.
SELECT file-name ASSIGN TO dd-name-jcl
ORGANIZATION IS INDEXED
RECORD KEY IS primary-key
ALTERNATE RECORD KEY IS rec-key
● Records can be read in sequential order just like in sequential and indexed file
organization.
● Records can be accessed using relative key. Relative key represents the record’s
location relative to the address of the start of the file.
● Records can be inserted using relative key. Relative address is calculated using relative
key.
● Relative file provides the fastest access to the records.
● Main disadvantage of this file system is that if some intermediate records are missing,
they will also occupy space.
Syntax
INPUT-OUTPUT SECTION.
FILE-CONTROL.
SELECT file-name ASSIGN TO dd-name-jcl
ORGANIZATION IS INDEXED
RELATIVE KEY IS rec-key
COBOL - File
Access Mode
Till now, file organization schemes have been discussed. For each file organization scheme,
different access modes can be used. Following are the types of file access modes:
● Sequential Access
● Random Access
● Dynamic Access
The syntaxes, in this module, mentioned along with their respective terms only refer to their
usage in the program. The complete programs using these syntaxes would be discussed in
next module.
Sequential Access
When access mode is Sequential, the method of record retrieval changes as per selected file
organization.
● For Sequential Files, records are accessed in the same order in which they were
inserted.
● For Indexed Files, the parameter used to fetch records are the record key values.
● For Relative Files, relative record keys are used to retrieve the records.
Syntax
Following is the syntax for Sequential access mode:
ENVIRONMENT DIVISION.
INPUT-OUTPUT SECTION.
FILE-CONTROL.
SELECT file-name ASSIGN TO dd-name
ORGANIZATION IS SEQUENTIAL
ACCESS MODE IS SEQUENTIAL
ENVIRONMENT DIVISION.
INPUT-OUTPUT SECTION.
FILE-CONTROL.
SELECT file-name ASSIGN TO dd-name
ORGANIZATION IS INDEXED
ACCESS MODE IS SEQUENTIAL
RECORD KEY IS rec-key1
ALTERNATE RECORD KEY IS rec-key2
ENVIRONMENT DIVISION.
INPUT-OUTPUT SECTION.
FILE-CONTROL.
SELECT file-name ASSIGN TO dd-name
ORGANIZATION IS RELATIVE
ACCESS MODE IS SEQUENTIAL
RELATIVE KEY IS rec-key1
Random Access
When access mode is RANDOM, the method of record retrieval changes as per selected file
organization.
● For Indexed Files , records are accessed according to the value placed in a key field
which can be primary or alternate key. There can be one or more alternate indexes.
● For Relative Files , records are retrieved through relative record keys.
Syntax
ENVIRONMENT DIVISION.
INPUT-OUTPUT SECTION.
FILE-CONTROL.
SELECT file-name ASSIGN TO dd-name
ORGANIZATION IS INDEXED
ACCESS MODE IS RANDOM
RECORD KEY IS rec-key1
ALTERNATE RECORD KEY IS rec-key2
ENVIRONMENT DIVISION.
INPUT-OUTPUT SECTION.
FILE-CONTROL.
SELECT file-name ASSIGN TO dd-name
ORGANIZATION IS RELATIVE
ACCESS MODE IS RANDOM
RELATIVE KEY IS rec-key1
Dynamic Access
Dynamic access supports both sequential and random access in the same program. With
dynamic access, one file definition is used to perform both sequential and random processing
like accessing some records in sequential order and others records by their keys.
With relative and indexed files, dynamic access mode allows you to switch back and forth
between sequential access mode and random access mode while reading a file by using the
NEXT phrase on the READ statement. NEXT and READ functionalities will be discussed in
next module.
Syntax
ENVIRONMENT DIVISION.
INPUT-OUTPUT SECTION.
FILE-CONTROL.
SELECT file-name ASSIGN TO dd-name
ORGANIZATION IS SEQUENTIAL
ACCESS MODE IS DYNAMIC
RECORD KEY IS rec-key1
ALTERNATE RECORD KEY IS rec-key2
ENVIRONMENT DIVISION.
INPUT-OUTPUT SECTION.
FILE-CONTROL.
SELECT file-name ASSIGN TO dd-name
ORGANIZATION IS RELATIVE
ACCESS MODE IS DYNAMIC
RELATIVE KEY IS rec-key1
COBOL - File
Handling Verbs
File handling verbs are used to perform operations on files. Following are the file handling
verbs:
● Open
● Read
● Write
● Rewrite
● Delete
● Start
● Close
Open Verb
Open is the first file operation that must be performed. If Open is successful, then only further
operations are possible on file. After opening file the variables in file structure are available for
processing. FILE STATUS variable is updated after each file operation. File can be opened in
following modes:
Syntax
Mode Description
Input Input mode is used if file already existing. In this mode we can only read the file,
no other operations are allowed on file.
Output Output mode is used to insert records in files. If Sequential file is is used and
file is holding some records, then the existing records will be deleted first and
then new records will be inserted in the file. In case of Indexed file or Relative
file, this will not happen.
Extend Extend mode is used to append records in Sequential file. In this mode, records
are inserted at the end. If file access mode is Random or Dynamic, then extend
mode cannot be used.
I-O Input-Output mode is used to read and rewrite the records of file.
Read Verb
Read verb is used to read the file records. The function of read is to fetch records from a file. At
each read verb, only one record can be read into the file structure. To do read operation, open
file in INPUT or I-O mode. At each read statement, the file pointer is incremented and hence
the successive records are read.
● NEXT RECORD is optional and is specified when an indexed sequential file is being
read sequentially.
● INTO clause is optional. ws-file-structure is defined in Working-Storage Section to get
the values from READ statement.
● AT END condition becomes True when end of file is reached.
Example to read an existing file using line sequential organization. This program can be
compiled and executed using Try it option where it will display all the records present in the
file.
IDENTIFICATION DIVISION.
PROGRAM-ID. HELLO.
ENVIRONMENT DIVISION.
INPUT-OUTPUT SECTION.
FILE-CONTROL.
SELECT STUDENT ASSIGN TO 'input.txt'
ORGANIZATION IS LINE SEQUENTIAL.
DATA DIVISION.
FILE SECTION.
FD STUDENT.
01 STUDENT-FILE.
05 STUDENT-ID PIC 9(5).
05 NAME PIC A(25).
WORKING-STORAGE SECTION.
01 WS-STUDENT.
05 WS-STUDENT-ID PIC 9(5).
05 WS-NAME PIC A(25).
01 WS-EOF PIC A(1).
PROCEDURE DIVISION.
OPEN INPUT STUDENT.
PERFORM UNTIL WS-EOF='Y'
READ STUDENT INTO WS-STUDENT
AT END MOVE 'Y' TO WS-EOF
NOT AT END DISPLAY WS-STUDENT
END-READ
END-PERFORM.
CLOSE STUDENT.
STOP RUN.
When you compile and execute the above program it produces the following result:
20003 Mohtashim M.
Example to read an existing file using Indexed organization. This program can be compiled
and executed using JCL on Mainframes where it will display all the records present in the file.
On Mainframes server we do not use text files instead we use PS files. Let's assume that file
present on Mainframes have same content as input.txt file in above example.
IDENTIFICATION DIVISION.
PROGRAM-ID. HELLO.
ENVIRONMENT DIVISION.
INPUT-OUTPUT SECTION.
FILE-CONTROL.
SELECT STUDENT ASSIGN TO IN1
ORGANIZATION IS INDEXED
ACCESS IS RANDOM
RECORD KEY IS STUDENT-ID
FILE STATUS IS FS.
DATA DIVISION.
FILE SECTION.
FD STUDENT.
01 STUDENT-FILE.
05 STUDENT-ID PIC 9(5).
05 NAME PIC A(25).
WORKING-STORAGE SECTION.
01 WS-STUDENT.
05 WS-STUDENT-ID PIC 9(5).
05 WS-NAME PIC A(25).
PROCEDURE DIVISION.
OPEN INPUT STUDENT.
MOVE 20005 TO STUDENT-ID.
READ STUDENT RECORD INTO WS-STUDENT-FILE
KEY IS STUDENT-ID
INVALID KEY DISPLAY 'Invalid Key'
NOT INVALID KEY DISPLAY WS-STUDENT-FILE
END-READ.
CLOSE STUDENT.
STOP RUN.
//SAMPLE JOB(TESTJCL,XXXXXX),CLASS=A,MSGCLASS=C
//STEP1 EXEC PGM=HELLO
//IN1 DD DSN=STUDENT-FILE-NAME,DISP=SHR
When you compile and execute the above program it produces the following result:
Write Verb
Write verb is used to insert records in file. Once the record is written, it is no longer available in
record buffer. Before inserting records into the file, move values into the record buffer and then
perform write verb. Write statement can be used with FROM option to directly write records
from working storage variables. From is an optional clause. If access mode is Sequential, then
to write records file must open in Output mode or Extend mode. If access mode is Random or
Dynamic then, then to write records file must open in Output mode or I-O mode.
Syntax
Following is the syntax to read record when file organization is sequential:
Following is the syntax to read record when file organization is Indexed or Relative:
WRITE record-buffer [FROM ws-file-structure]
INVALID KEY DISPLAY 'Invalid Key'
NOT INVALID KEY DISPLAY 'Record Inserted'
END-WRITE.
Example to insert a new record in a new file when organization is Sequential. This program will
add records in the file.
IDENTIFICATION DIVISION.
PROGRAM-ID. HELLO.
ENVIRONMENT DIVISION.
INPUT-OUTPUT SECTION.
FILE-CONTROL.
SELECT STUDENT ASSIGN TO OUT1
ORGANIZATION IS SEQUENTIAL
ACCESS IS SEQUENTIAL
FILE STATUS IS FS.
DATA DIVISION.
FILE SECTION.
FD STUDENT
01 STUDENT-FILE.
05 STUDENT-ID PIC 9(5).
05 NAME PIC A(25).
05 CLASS PIC X(3).
WORKING-STORAGE SECTION.
01 WS-STUDENT.
05 WS-STUDENT-ID PIC 9(5).
05 WS-NAME PIC A(25).
05 WS-CLASS PIC X(3).
PROCEDURE DIVISION.
OPEN EXTEND STUDENT.
MOVE 1000 TO STUDENT-ID.
MOVE 'Tim' TO NAME.
MOVE '10' TO CLASS.
WRITE STUDENT-FILE
END-WRITE.
CLOSE STUDENT.
STOP RUN.
//SAMPLE JOB(TESTJCL,XXXXXX),CLASS=A,MSGCLASS=C
//STEP1 EXEC PGM=HELLO
//OUT1 DD DSN=OUTPUT-FILE-NAME,DISP=(NEW,CATALOG,DELETE)
When you compile and execute the above program it will add a new record to output file.
1000 Tim 10
Rewrite Verb
Rewrite verb is used to update the records. File should be opened in I-O mode for rewrite
operations. It can be used only after successful Read operation. Rewrite verb overwrites the
last record read.
Syntax
Following is the syntax to read record when file organization is sequential:
Following is the syntax to read record when file organization is Indexed or Relative:
Example to update an existing record which we have inserted in previous Write step:
IDENTIFICATION DIVISION.
PROGRAM-ID. HELLO.
ENVIRONMENT DIVISION.
INPUT-OUTPUT SECTION.
FILE-CONTROL.
SELECT STUDENT ASSIGN TO IN1
ORGANIZATION IS INDEXED
ACCESS IS RANDOM
RECORD KEY IS STUDENT-ID
FILE STATUS IS FS.
DATA DIVISION.
FILE SECTION.
FD STUDENT
01 STUDENT-FILE.
05 STUDENT-ID PIC 9(4).
05 NAME PIC A(12).
05 CLASS PIC X(3).
WORKING-STORAGE SECTION.
01 WS-STUDENT.
05 WS-STUDENT-ID PIC 9(5).
05 WS-NAME PIC A(25).
05 WS-CLASS PIC X(3).
PROCEDURE DIVISION.
OPEN I-O STUDENT.
MOVE '1000' TO STUDENT-ID.
READ STUDENT
KEY IS STUDENT-ID
INVALID KEY DISPLAY ‘KEY IS NOT EXISTING’
END-READ.
MOVE 'Tim Dumais' TO NAME.
REWRITE STUDENT-FILE
END-REWRITE.
CLOSE STUDENT.
STOP RUN.
//SAMPLE JOB(TESTJCL,XXXXXX),CLASS=A,MSGCLASS=C
//STEP1 EXEC PGM=HELLO
//IN1 DD DSN=OUTPUT-FILE-NAME,DISP=SHR
When you compile and execute the above program it will update the record:
Delete Verb
Delete verb can be performed only on Indexed and Relative files and file must be opened in I-O
mode. In Sequential file organization, records cannot be deleted. The record last read by the
Read statement is deleted in case of sequential access mode. In random access mode, specify
record key and then perform Delete statement.
Syntax
Following is the syntax to delete a record:
IDENTIFICATION DIVISION.
PROGRAM-ID. HELLO.
ENVIRONMENT DIVISION.
INPUT-OUTPUT SECTION.
FILE-CONTROL.
SELECT STUDENT ASSIGN TO OUT1
ORGANIZATION IS INDEXED
ACCESS IS RANDOM
RECORD KEY IS STUDENT-ID
FILE STATUS IS FS.
DATA DIVISION.
FILE SECTION.
FD STUDENT
01 STUDENT-FILE.
05 STUDENT-ID PIC 9(4).
05 NAME PIC A(12).
05 CLASS PIC X(3).
WORKING-STORAGE SECTION.
01 WS-STUDENT.
05 WS-STUDENT-ID PIC 9(5).
05 WS-NAME PIC A(25).
05 WS-CLASS PIC X(3).
PROCEDURE DIVISION.
OPEN I-O STUDENT.
MOVE '1000' TO STUDENT-ID.
DELETE STUDENT RECORD
INVALID KEY DISPLAY 'Invalid Key'
NOT INVALID KEY DISPLAY 'Record Deleted'
END-DELETE.
CLOSE STUDENT.
STOP RUN.
//SAMPLE JOB(TESTJCL,XXXXXX),CLASS=A,MSGCLASS=C
//STEP1 EXEC PGM=HELLO
//OUT1 DD DSN=OUTPUT-FILE-NAME,DISP=SHR
When you compile and execute the above program it produces the following result:
Record Deleted
Start Verb
Start verb can be performed only on Indexed and Relative files. It is used to place the file
pointer at a specific record. The access mode must be Sequential or Dynamic. File must be
opened in I-O or Input mode.
Syntax
Following is the syntax to place the pointer at a specific record:
START file-name KEY IS [=, >, <, NOT, <= or >=] rec-key
INVALID KEY DISPLAY 'Invalid Key'
NOT INVALID KEY DISPLAY 'File Pointer Updated'
END-START.
Close Verb
Close verb is used to close the file. After performing Close statement, variables in file structure
will not be available for processing. The link between program and file is lost.
Syntax
Following is the syntax to close the file:
CLOSE file-name.
COBOL -
Subroutines
Cobol subroutine is a program which can be compiled independently but cannot be executed
independently. There are two types of subroutines, internal subroutines like Perform
statements inside COBOL program and another is external subroutines like CALLverb.
Call Verb
COBOL verb is used to transfer control from one program to another program. The program
which contains CALL verb is Calling Program and the program being called is known as
Called Program. Calling program execution will halt until the called program finishes the
execution. Exit Program statement is used in Called program to transfer control back.
● Linkage Section must be defined in the called program. It consists of data elements
passed in the program. The data items should not have Value clause. PIC clause must
be compatible with the variables passed through calling program.
● Procedure Division Using has list of variables passed from the calling program and
the order must be same as mentioned in the Call verb.
● Exit Program statement is used in Called program to transfer control back. It must be
the last statement in called program
● By Reference
● By Content
By Reference
If the values of variables in the called program are modified, then their new values will reflect in
the calling program. If BY clause is not specified, then variables are always passed by
reference.
Syntax
Following is the syntax of calling subroutine by reference:
Example
Following example is the MAIN calling program and UTIL is called program:
IDENTIFICATION DIVISION.
PROGRAM-ID. MAIN.
DATA DIVISION.
WORKING-STORAGE SECTION.
01 WS-STUDENT-ID PIC 9(4) VALUE 1000.
01 WS-STUDENT-NAME PIC A(15) VALUE 'Tim'.
PROCEDURE DIVISION.
CALL 'UTIL' USING WS-STUDENT-ID, WS-STUDENT-NAME.
DISPLAY 'Student Id : ' WS-STUDENT-ID
DISPLAY 'Student Name : ' WS-STUDENT-NAME
STOP RUN.
Called Program
IDENTIFICATION DIVISION.
PROGRAM-ID. UTIL.
DATA DIVISION.
LINKAGE SECTION.
01 LS-STUDENT-ID PIC 9(4).
01 LS-STUDENT-NAME PIC A(15).
PROCEDURE DIVISION USING LS-STUDENT-ID, LS-STUDENT-NAME.
DISPLAY 'In Called Program'.
MOVE 1111 TO LS-STUDENT-ID.
EXIT PROGRAM.
//SAMPLE JOB(TESTJCL,XXXXXX),CLASS=A,MSGCLASS=C
//STEP1 EXEC PGM=MAIN
When you compile and execute the above program it produces the following result:
In Called Program
Student Id : 1111
Syntax
Following is the syntax of calling subroutine by content:
Example
Following example is the MAIN calling program and UTIL is called program:
IDENTIFICATION DIVISION.
PROGRAM-ID. MAIN.
DATA DIVISION.
WORKING-STORAGE SECTION.
01 WS-STUDENT-ID PIC 9(4) VALUE 1000.
01 WS-STUDENT-NAME PIC A(15) VALUE 'Tim'.
PROCEDURE DIVISION.
CALL 'UTIL' USING BY CONTENT WS-STUDENT-ID, BY CONTENT WS-STUDENT-NAME.
DISPLAY 'Student Id : ' WS-STUDENT-ID
DISPLAY 'Student Name : ' WS-STUDENT-NAME
STOP RUN.
Called Program
IDENTIFICATION DIVISION.
PROGRAM-ID. UTIL.
DATA DIVISION.
LINKAGE SECTION.
01 LS-STUDENT-ID PIC 9(4).
01 LS-STUDENT-NAME PIC A(15).
PROCEDURE DIVISION USING LS-STUDENT-ID, LS-STUDENT-NAME.
DISPLAY 'In Called Program'.
MOVE 1111 TO LS-STUDENT-ID.
EXIT PROGRAM.
//SAMPLE JOB(TESTJCL,XXXXXX),CLASS=A,MSGCLASS=C
//STEP1 EXEC PGM=MAIN
When you compile and execute the above program it produces the following result:
In Called Program
Student Id : 1000
Types of Call
There are two types of call:
● Static Call occurs when a program is compiled with the NODYNAM compiler option. A
static called program is loaded into storage at compile time.
● Dynamic Call occurs when program is compiled with the DYNAM and NODLL compiler
option. A dynamic called program is loaded into storage at run-time.
COBOL - Internal
Sort
Sorting of data in a file or merging of two or more files is a common necessity in almost all
business oriented applications. Sorting is used for arranging records either in ascending or
descending order, so that sequential processing can be performed. There are two techniques
which are used for sorting files in COBOL:
● External Sort is used to sort files by using SORT utility in JCL's. We have discussed
this in JCL module. As of now, we will focus on internal Sort.
● Internal Sort is used to sort files with in COBOL program. SORT verb is used to sort a
file.
Sort Verb
Three files are used in Sort process in COBOL:
● Input File is the file which we have to sort either in Ascending order or Descending
order.
● Work File is used to hold records while Sort process is in progress. Input file records
are transferred to work file for sorting process. This file should be defined in
File-Section under SD entry.
● Output File is the file which we get after sorting process. It is the final output of Sort
verb.
Syntax
Following is the syntax to Sort a file:
● Opens work-file in the I-O mode, input-file in the INPUT mode and output-file in the
OUTPUT mode.
● Transfers the records present in input-file to work-file.
● Sorts SORT-FILE on ascending/descending sequence by rec-key.
● Transfers the sorted records from work-file to the output-file.
● Closes the input-file and output-file and deletes the work-file.
Example
In below example INPUT is input file which need to be sorted in ascending order:
IDENTIFICATION DIVISION.
PROGRAM-ID. HELLO.
ENVIRONMENT DIVISION.
INPUT-OUTPUT SECTION.
FILE-CONTROL.
SELECT INPUT ASSIGN TO IN.
SELECT OUTPUT ASSIGN TO OUT.
SELECT WORK ASSIGN TO WRK.
DATA DIVISION.
FILE SECTION.
FD INPUT.
01 INPUT-STUDENT.
05 STUDENT-ID-I PIC 9(5).
05 STUDENT-NAME-I PIC A(25).
FD OUTPUT.
01 OUTPUT-STUDENT.
05 STUDENT-ID-O PIC 9(5).
05 STUDENT-NAME-O PIC A(25).
SD WORK.
01 WORK-STUDENT.
05 STUDENT-ID-W PIC 9(5).
05 STUDENT-NAME-W PIC A(25).
PROCEDURE DIVISION.
SORT WORK ON ASCENDING KEY STUDENT-ID-O
USING INPUT GIVING OUTPUT.
DISPLAY 'Sort Successful'.
STOP RUN.
//SAMPLE JOB(TESTJCL,XXXXXX),CLASS=A,MSGCLASS=C
//STEP1 EXEC PGM=HELLO
//IN DD DSN=INPUT-FILE-NAME,DISP=SHR
//OUT DD DSN=OUTPUT-FILE-NAME,DISP=SHR
//WRK DD DSN=&&TEMP
When you compile and execute the above program it produces the following result:
Sort Successful
Merge Verb
Two or more identically sequenced files are combined using Merge statement. Files used in the
merge process:
Syntax
Following is the syntax to Merge two or more files:
● Opens work-file in the I-O mode, input-files in the INPUT mode and output-file in the
OUTPUT mode.
● Transfers the records present in input-files to work-file.
● Sorts SORT-FILE on ascending/descending sequence by rec-key.
● Transfers the sorted records from work-file to the output-file.
● Closes the input-file and output-file and deletes the work-file.
Example
In below example INPUT1 and INPUT2 are input files which need to be merged in ascending
order:
IDENTIFICATION DIVISION.
PROGRAM-ID. HELLO.
ENVIRONMENT DIVISION.
INPUT-OUTPUT SECTION.
FILE-CONTROL.
SELECT INPUT1 ASSIGN TO IN1.
SELECT INPUT2 ASSIGN TO IN2.
SELECT OUTPUT ASSIGN TO OUT.
SELECT WORK ASSIGN TO WRK.
DATA DIVISION.
FILE SECTION.
FD INPUT1.
01 INPUT1-STUDENT.
05 STUDENT-ID-I1 PIC 9(5).
05 STUDENT-NAME-I1 PIC A(25).
FD INPUT2.
01 INPUT2-STUDENT.
05 STUDENT-ID-I2 PIC 9(5).
05 STUDENT-NAME-I2 PIC A(25).
FD OUTPUT.
01 OUTPUT-STUDENT.
05 STUDENT-ID-O PIC 9(5).
05 STUDENT-NAME-O PIC A(25).
SD WORK.
01 WORK-STUDENT.
05 STUDENT-ID-W PIC 9(5).
05 STUDENT-NAME-W PIC A(25).
PROCEDURE DIVISION.
MERGE WORK ON ASCENDING KEY STUDENT-ID-O
USING INPUT1, INPUT2 GIVING OUTPUT.
DISPLAY 'Merge Successful'.
STOP RUN.
//SAMPLE JOB(TESTJCL,XXXXXX),CLASS=A,MSGCLASS=C
//STEP1 EXEC PGM=HELLO
//IN1 DD DSN=INPUT1-FILE-NAME,DISP=SHR
//IN2 DD DSN=INPUT2-FILE-NAME,DISP=SHR
//OUT DD DSN=OUTPUT-FILE-NAME,DISP=SHR
//WRK DD DSN=&&TEMP
When you compile and execute the above program it produces the following result:
Merge Successful
COBOL - Database
Interface
As of now, we have learnt use of files in COBOL. Now, we will discuss how a COBOL program
interacts with DB2. This involves usage of following terms:
● Embedded SQL
● DB2 Application Programming
● Host Variables
● SQLCA
● SQL Queries
● Cursors
Embedded SQL
Embedded SQL statements are used in COBOL programs to perform standard SQL
operations. Embedded SQL statements are preprocessed by SQL processor before application
program is compiled. COBOL is known as Host Language. COBOL-DB2 applications are
those applications which include both COBOL and DB2.
Embedded SQL statements works like normal SQL statements with some minor changes like
output of query is directed to pre-defined set of variables which are referred as Host Variables.
An additional INTO clause is placed in SELECT statement.
DB2 Application Programming
Following are rules while coding a COBOL-DB2 program:
● All the SQL statements must be delimited between EXEC SQL and END-EXEC.
● SQL statements must be coded in Area B.
● All the tables that are used in a program must be declared in Working-Storage Section.
This is done by using INCLUDE statement.
● All SQL statements other than INCLUDE and DECLARE TABLE must appear in
Procedure Division.
Host Variables
Host Variables are used for receiving data from the table or inserting data to the table. Host
variables must be declared for all values that are to be passed between program and DB2.
They are declared in Working-Storage Section.
Host variables cannot be group items but they may be grouped together in host structure. They
cannot be Renamed or Redefined. Using host variables with SQL statements, prefix them with
a colon (:).
Syntax
Following is the syntax to declare host variables and including tables in Working-Storage
section:
DATA DIVISION.
WORKING-STORAGE SECTION.
EXEC SQL
INCLUDE table-name
END-EXEC.
EXEC SQL BEGIN DECLARE SECTION
END-EXEC.
01 STUDENT-REC.
05 STUDENT-ID PIC 9(4).
05 STUDENT-NAME PIC X(25).
05 STUDENT-ADDRESS X(50).
EXEC SQL END DECLARE SECTION
END-EXEC.
SQLCA
SQLCA is a SQL communication area through which DB2 passes feedback of SQL execution
to the program. It tells the program whether execution was successful or not. There are number
of pre-defined variables under SQLCA like SQLCODE which contains the error code. Value
'000' in SQLCODE states a successful execution.
Syntax
Following is the syntax to declare SQLCA in Working-Storage section:
DATA DIVISION.
WORKING-STORAGE SECTION.
EXEC SQL
INCLUDE SQLCA
END-EXEC.
SQL Queries
We will discuss about using SQL queries in COBOL-DB2 program. Lets assume we have one
table named as 'Student' which contains Student-Id, Student-Name and Student-Address.
IDENTIFICATION DIVISION.
PROGRAM-ID. HELLO.
DATA DIVISION.
WORKING-STORAGE SECTION.
EXEC SQL
INCLUDE SQLCA
END-EXEC.
EXEC SQL
INCLUDE STUDENT
END-EXEC.
EXEC SQL BEGIN DECLARE SECTION
END-EXEC.
01 WS-STUDENT-REC.
05 WS-STUDENT-ID PIC 9(4).
05 WS-STUDENT-NAME PIC X(25).
05 WS-STUDENT-ADDRESS X(50).
EXEC SQL END DECLARE SECTION
END-EXEC.
PROCEDURE DIVISION.
EXEC SQL
SELECT STUDENT-ID, STUDENT-NAME, STUDENT-ADDRESS
INTO :WS-STUDENT-ID, :WS-STUDENT-NAME, WS-STUDENT-ADDRESS FROM STUDENT
WHERE STUDENT-ID=1004
END-EXEC.
IF SQLCODE=0
DISPLAY WS-STUDENT-RECORD
ELSE DISPLAY 'Error'
END-IF.
STOP RUN.
//SAMPLE JOB(TESTJCL,XXXXXX),CLASS=A,MSGCLASS=C
//STEP001 EXEC PGM=IKJEFT01
//STEPLIB DD DSN=MYDATA.URMI.DBRMLIB,DISP=SHR
//SYSPRINT DD SYSOUT=*
//SYSUDUMP DD SYSOUT=*
//SYSOUT DD SYSOUT=*
//SYSTSIN DD *
DSN SYSTEM(SSID)
RUN PROGRAM(HELLO) PLAN(PLANNAME) -
END
/*
When you compile and execute the above program it produces the following result:
1004 Chulbul Pandey Lucknow
IDENTIFICATION DIVISION.
PROGRAM-ID. HELLO.
DATA DIVISION.
WORKING-STORAGE SECTION.
EXEC SQL
INCLUDE SQLCA
END-EXEC.
EXEC SQL
INCLUDE STUDENT
END-EXEC.
EXEC SQL BEGIN DECLARE SECTION
END-EXEC.
01 WS-STUDENT-REC.
05 WS-STUDENT-ID PIC 9(4).
05 WS-STUDENT-NAME PIC X(25).
05 WS-STUDENT-ADDRESS X(50).
EXEC SQL END DECLARE SECTION
END-EXEC.
PROCEDURE DIVISION.
MOVE 1005 TO WS-STUDENT-ID.
MOVE 'TutorialsPoint' TO WS-STUDENT-NAME.
MOVE 'Hyderabad' TO WS-STUDENT-ADDRESS.
EXEC SQL
INSERT INTO STUDENT(STUDENT-ID, STUDENT-NAME, STUDENT-ADDRESS)
VALUES (:WS-STUDENT-ID, :WS-STUDENT-NAME, WS-STUDENT-ADDRESS)
END-EXEC.
IF SQLCODE=0
DISPLAY 'Record Inserted Successfully'
DISPLAY WS-STUDENT-REC
ELSE DISPLAY 'Error'
END-IF.
STOP RUN.
//SAMPLE JOB(TESTJCL,XXXXXX),CLASS=A,MSGCLASS=C
//STEP001 EXEC PGM=IKJEFT01
//STEPLIB DD DSN=MYDATA.URMI.DBRMLIB,DISP=SHR
//SYSPRINT DD SYSOUT=*
//SYSUDUMP DD SYSOUT=*
//SYSOUT DD SYSOUT=*
//SYSTSIN DD *
DSN SYSTEM(SSID)
RUN PROGRAM(HELLO) PLAN(PLANNAME) -
END
/*
When you compile and execute the above program it produces the following result:
Record Inserted Successfully
IDENTIFICATION DIVISION.
PROGRAM-ID. HELLO.
DATA DIVISION.
WORKING-STORAGE SECTION.
EXEC SQL
INCLUDE SQLCA
END-EXEC.
EXEC SQL
INCLUDE STUDENT
END-EXEC.
EXEC SQL BEGIN DECLARE SECTION
END-EXEC.
01 WS-STUDENT-REC.
05 WS-STUDENT-ID PIC 9(4).
05 WS-STUDENT-NAME PIC X(25).
05 WS-STUDENT-ADDRESS X(50).
EXEC SQL END DECLARE SECTION
END-EXEC.
PROCEDURE DIVISION.
MOVE 'Bangalore' TO WS-STUDENT-ADDRESS.
EXEC SQL
UPDATE STUDENT SET STUDENT-ADDRESS=:WS-STUDENT-ADDRESS
WHERE STUDENT-ID=1003
END-EXEC.
IF SQLCODE=0
DISPLAY 'Record Updated Successfully'
ELSE DISPLAY 'Error'
END-IF.
STOP RUN.
//SAMPLE JOB(TESTJCL,XXXXXX),CLASS=A,MSGCLASS=C
//STEP001 EXEC PGM=IKJEFT01
//STEPLIB DD DSN=MYDATA.URMI.DBRMLIB,DISP=SHR
//SYSPRINT DD SYSOUT=*
//SYSUDUMP DD SYSOUT=*
//SYSOUT DD SYSOUT=*
//SYSTSIN DD *
DSN SYSTEM(SSID)
RUN PROGRAM(HELLO) PLAN(PLANNAME) -
END
/*
When you compile and execute the above program it produces the following result:
Record Updated Successfully
IDENTIFICATION DIVISION.
PROGRAM-ID. HELLO.
DATA DIVISION.
WORKING-STORAGE SECTION.
EXEC SQL
INCLUDE SQLCA
END-EXEC.
EXEC SQL
INCLUDE STUDENT
END-EXEC.
EXEC SQL BEGIN DECLARE SECTION
END-EXEC.
01 WS-STUDENT-REC.
05 WS-STUDENT-ID PIC 9(4).
05 WS-STUDENT-NAME PIC X(25).
05 WS-STUDENT-ADDRESS X(50).
EXEC SQL END DECLARE SECTION
END-EXEC.
PROCEDURE DIVISION.
MOVE 1005 TO WS-STUDENT-ID.
EXEC SQL
DELETE FROM STUDENT
WHERE STUDENT-ID=:WS-STUDENT-ID
END-EXEC.
IF SQLCODE=0
DISPLAY 'Record Deleted Successfully'
ELSE DISPLAY 'Error'
END-IF.
STOP RUN.
//SAMPLE JOB(TESTJCL,XXXXXX),CLASS=A,MSGCLASS=C
//STEP001 EXEC PGM=IKJEFT01
//STEPLIB DD DSN=MYDATA.URMI.DBRMLIB,DISP=SHR
//SYSPRINT DD SYSOUT=*
//SYSUDUMP DD SYSOUT=*
//SYSOUT DD SYSOUT=*
//SYSTSIN DD *
DSN SYSTEM(SSID)
RUN PROGRAM(HELLO) PLAN(PLANNAME) -
END
/*
When you compile and execute the above program it produces the following result:
Record Deleted Successfully
Cursors
Cursors are used to handle multiple row selections at a time. They are data structure which
hold all the results of a query. They can be defined in Working-Storage Section or Procedure
Division. Following are the operations associated with Cursor:
● Declare
● Open
● Close
● Fetch
Declare Cursor
Cursor declaration can be done in Working-Storage Section or Procedure Division. First
statement is DECLARE statement which is non-executable statement.
EXEC SQL
DECLARE STUDCUR CURSOR FOR
SELECT STUDENT-ID, STUDENT-NAME, STUDENT-ADDRESS FROM STUDENT
WHERE STUDENT-ID >:WS-STUDENT-ID
END-EXEC.
Open
Before using a cursor, Open statement must be performed. The Open statement prepares the
SELECT for execution.
EXEC SQL
OPEN STUDCUR
END-EXEC.
Close
Close statement releases all the memory occupied by the cursor. It is mandatory to close a
cursor before ending a program.
EXEC SQL
CLOSE STUDCUR
END-EXEC.
Fetch
Fetch statement identifies the cursor and puts the value in INTO clause. A Fetch statement is
coded in loop as we get one row at a time.
EXEC SQL
FETCH STUDCUR
INTO :WS-STUDENT-ID, :WS-STUDENT-NAME, WS-STUDENT-ADDRESS
END-EXEC.
Following example shows the usage of cursor to fetch all records from STUDENT table:
IDENTIFICATION DIVISION.
PROGRAM-ID. HELLO.
DATA DIVISION.
WORKING-STORAGE SECTION.
EXEC SQL
INCLUDE SQLCA
END-EXEC.
EXEC SQL
INCLUDE STUDENT
END-EXEC.
EXEC SQL BEGIN DECLARE SECTION
END-EXEC.
01 WS-STUDENT-REC.
05 WS-STUDENT-ID PIC 9(4).
05 WS-STUDENT-NAME PIC X(25).
05 WS-STUDENT-ADDRESS X(50).
EXEC SQL END DECLARE SECTION
END-EXEC.
EXEC SQL
DECLARE STUDCUR CURSOR FOR
SELECT STUDENT-ID, STUDENT-NAME, STUDENT-ADDRESS FROM STUDENT
WHERE STUDENT-ID >:WS-STUDENT-ID
END-EXEC.
PROCEDURE DIVISION.
MOVE 1001 TO WS-STUDENT-ID.
PERFORM UNTIL SQLCODE = 100
EXEC SQL
FETCH STUDCUR
INTO :WS-STUDENT-ID, :WS-STUDENT-NAME, WS-STUDENT-ADDRESS
END-EXEC
DISPLAY WS-STUDENT-REC
END-PERFORM
STOP RUN.
//SAMPLE JOB(TESTJCL,XXXXXX),CLASS=A,MSGCLASS=C
//STEP001 EXEC PGM=IKJEFT01
//STEPLIB DD DSN=MYDATA.URMI.DBRMLIB,DISP=SHR
//SYSPRINT DD SYSOUT=*
//SYSUDUMP DD SYSOUT=*
//SYSOUT DD SYSOUT=*
//SYSTSIN DD *
DSN SYSTEM(SSID)
RUN PROGRAM(HELLO) PLAN(PLANNAME) -
END
/*
When you compile and execute the above program it produces the following result:
1001 Mohtashim M. Hyderabad
COBOL - Interview
Questions
Dear readers, these COBOL Interview Questions have been designed especially to get you
acquainted with the nature of questions you may encounter during your interview for the
subject of COBOL Programming Language. As per my experience, good interviewers hardly
planned to ask any particular question during your interview, normally questions start with
some basic concept of the subject and later they continue based on further discussion and
what you answer:
Q: What is the mode in which you will OPEN a file for writing?
A: To write a file, open file in either OUTPUT or EXTEND mode.
Q: What will happen if you code GO BACK instead of STOP RUN in a stand alone COBOL
program?
A: The program will go in an infinite loop.
Q: What is the difference between Perform With Test After and Perform With Test Before?
A: If TEST BEFORE is specified, the condition is tested at the beginning of each repeated
execution of the specified PERFORM range. If TEST AFTER is specified, the condition is
tested at the end of the each repeated execution of the PERFORM range. The range is
executed at least once in TEST AFTER.
Q: Why is it necessary that file needs to be opened in I-O mode for REWRITE?
A: Before the REWRITE is performed, the record must be open and read from the file.
Therefore, the file must be opened in I-O mode for rewrite functionality.