1/41
Looks like no tags are added yet.
Name | Mastery | Learn | Test | Matching | Spaced |
---|
No study sessions yet.
What is COBOL?
Common Business Oriented Language. Developed around 1950s. High level programming language for business applications. English like, imperative, procedural, object oriented since 2002. Around 80% of world businesses run on COBOL like govt, banks. over 60% of organizations use cobol. 60 million patient data, 95% ATM transactions, 96% vacations booking, etc.
latest release of IBM COBOL - Enterprise COBOL for z/OS - Version 6 Release 4 - 2022-05-27
|
it has the following area in the pgm:
column numbers: 1 to 6 - Reserved for line numbers.
Indicator - 7 - It can have Asterisk (*) indicating comments, Hyphen (-) indicating continuation and Slash ( / ) indicating form feed.
Area A: 8 to 11 column - All COBOL divisions, sections, paragraphs and some special entries must begin in Area A.
Area B: 12 to 72 - All COBOL statements must begin in area B.
Identification area: 72 to 80 - It can be used as needed by the programmer.
Structure of COBOL program
IDENTIFICATION DIVISION - supply info about the program such as program name, name of the programmer
ENVIRONMENT DIVISION - describes the environment in which the program will run, such as the source and object computer.
DATA DIVISION - provides the description of data to be processed by the program such as input and output files
PROCEDURE DIVISION - code of the program.
Each division has sections which has paragraphs which has statements
division → sections → paragraphs → statements
HELLO world
We have to at least give 2 divisions to execute a Hello world program. These are:
IDENTIFICATION DIVISION
PROCEDURE DIVISION
Divisions have to be given at position 8 and statements have to be given at position 12.
IDENTIFICATION DIVISION. *identifying the program
PROGRAM-ID. HELLOWORLD. *name of the program
PROCEDURE DIVISION. *real code starts from here
DISPLAY “HELLO WORLD“. *real code of the program
STOP RUN. *end the program here
IDENTIFICATION DIVISION.
First division. Used to identify a program. PROGRAM-ID is required.
program-id max 8 char on IBM mainframe. can be alphabets, numbers and hyphen. should not start with hyphen.
can have other optional fields like-
Author. KIRTHIKA
INSTALLATION. MTH.
REMARKS. PRACTICE CODE.
DATE-COMPILED. 12-MAY-2025.
DATE-WRITTEN. 12-MAY-2025.
SECURITY. NORMAL.
These can also be mentioned in the comments.
WORKING-STORAGE SECTION.
This is given in the DATA DIVISION.
Used to identify the data associated with a program such as the variables of the program.
DATA DIVISION.
WORKING-STORAGE SECTION.
01 MY_NAME PIC XXXX.
To define a variable give a 2 digit number called level number in AREA A (8 to 11), followed by the variable name in AREA B (12 to 72).
Level numbers are used to tell whether the variables are related or not. Related variables are grouped together. Unrelated variables can be given level number 77..
Variable name can be a max of 30 characters.
Variables are called Data names or data items in COBOL.
It can have alphabets, digits or hyphen.
Name should not start or end with hyphen.
PICTURE is used to tell the type of the variable.
9 means - numerical
X means alphanumeric
DATA DIVISION.
WORKING-STORAGE SECTION.
01 MY-NAME.
05 FIRST-NAME PIC X.
05 LAST-NAME PIC X.
77 AGE PICTURE 99.
PICTURE clause
it is used to tell whether the data item (variable) can contain numeric, alphabets etc.
It is given after the name of the data item.
item type Characters Meaning eg
Alphanumeric X Any Character PIC X.
PIC XXX.
PIC X(3).
Numeric 9 Digit PIC 99
S Sign PIC S999
V Assumed Decimal point PIC S9(5)V99
Number edited 9 Digit PIC 99
Z Zero suppressed digit PIC ZZ9
, Inserted comma PIC ZZZ,ZZZ
. Inserted decimal point PIC ZZ,ZZZ.99. don’t give zzv99 decimal will not come use “.” to insert decimal.
- Minus sign if negative PIC ZZZ,ZZZ-
Each character or digit require only one byte of storage.
VALUE Clause
Can be used to give a starting value to a variable. The most common way is to assign a figurative constant to a variable for example ZEROES or SPACES. Initializing the variables (data item).
DATA DIVISION.
WORKING-STORAGE SECTION.
01 INCOME PIC 9(2) VALUE ZEROES.
GROUP items
Related data items could be grouped together. Level numbers are used for this grouping. The top item is called group item and the child items are called elementary items.
Group item cannot have a picture clause.
Group item must begin in AREA A.
Level numbers from 01 to 49 can be used.
DATA DIVISION.
WORKING-STORAGE SECTION.
01 NAME.
05 FIRST-NAME PIC X(10).
05 LAST-NAME PIC X(10).
Data items unrelated to each other can be given level numbers of 77.
Coding the PROCEDURE division
PROCEDURE DIVISION.
000-CALC-TOTAL-MARKS.
PERFORM 100-CALCULATE-PHYSICS-MARKS
PERFORM 100-CALCULATE-BIOLOGY-MARKS
PERFORM 100-CALCULATE-MATH-MARKS
STOP RUN.
100-CALCULATE-PHYSICS-MARKS.
code
100-CALCULATE-MATH-MARKS.
code
Procedure names can have a maximum of 30 characters.
It can contain digits, alphabets and hyphen but the name should not start or end with hyphen.
Procedure names should start in Area A and the statements should be in AREA B.
Statements
Display statement to display data on the screen or terminal.
MOVE is used to move literal or sending field to a receiving field.
If sending field is smaller then zeroes (for numeric) and spaces (for alphanumeric) will be added.
If sending field is larger then value will be truncated. For number right to left will to taken eg: N1 PIC 99. MOVE 123 to N1. N1 will be 23 and alphanumeric left to right eg: V1 pic XX. MOVE ‘KIR’ to V1. V1 will be KI.
ACCEPT is to get input from the user.
PERFORM: Used to execute a procedure. Code can be divided into a set of paragraphs known as procedures. Control is passed to the procedure named in the PERFORM statement. After all the statements in the procedure are executed then the return comes back to the next statement after the perform.
STOP RUN: is used to end the execution of the program. It is coded only once in a program. It is generally the last statement in the main procedure.
Datasets needed for executing COBOL on z/os
Dataset for writing COBOL program.
Dataset for writing compile JCL and run JCL.
Dataset for storing load module - record format - undefined, record length = 0
first 6 columns for seq numbers then divisions will come so we can give this to start every line from column 8 instead of 1 - num on std cob then F10. Sequence number will be automatically generated.
COBOL program compilation
2 step process:
cobol program (high level language) is compiled(Uses IGYCRCTL utility) to object module (machine code and non-executable). If any copybooks are there it will be applied in this step. Error will be in compiler output in the spool.
link edit- all the sub programs used by the main program will be linked to the main program and load module(executable) will be generated. Uses HEWL/ IEWL/ IEWBLINK etc - binder programs. Error will be in Link editor output in the spool.
IBM provides a cataloged procedure IGYWCL which does the whole process in one JCL. This is located at IGY*.SIGYPROC
Following DD statements are required by the procedure IGYWCL:
SYSIN → Location of COBOL pgm
SYSLIB → location of copybooks
SYSLMOD → Output load module library
Then run execution JCL to run the code
EXEC PGM=HELLO
JOBLIB
SYSIN if any
SYSOut = *
ADD statement and COMPUTE statement
ADD A TO B → COMPUTE B=B+A
ADD 1 TO B → COMPUTE B=B+1
ADD A TO B GIVING C → COMPUTE C=B+A
ADD A B C TO D → COMPUTE D=A+B+C
We can use ON SIZE ERROR and ROUNDED on our results:
ADD A TO B ROUNDED → COMPUTE B ROUNDED=B+A
ADD A TO B ON SIZE ERROR DISPLAY “ERROR“ → COMPUTE B=B+A ON SIZE ERROR DISPLAY “ERROR“
SUBTRACT statement
SUBTRACT A FROM B → compute can be used as COMPUTE B=B-A
SUBTRACT A B C FROM D → compute can be used as COMPUTE D = D - (A + B + C) or COMPUTE D = D - A - B - C
SUBTRACT A FROM B GIVING C→ compute can be used as COMPUTE C=B-A
MULTIPLY statement
MULTIPLY A BY B → COMPUTE B = B * A
MULTIPLY A BY B GIVING C→ COMPUTE C = B * A
MULTIPLY A BY B ON SIZE ERROR DISPLAY “ERROR“
NOT ON SIZE ERROR DISPLAY “NO ERROR“ B.
DIVIDE STATEMENT
DIVIDE A INTO B → COMPUTE B=B/A
DIVIDE A BY B GIVING C → COMPUTE C=A/B
DIVIDE A BY B GIVING C REMAINDER D → NO CORRESPONDING COMPUTE
COMPUTE STATEMENT
USED TO DO ARITHMETIC OPERATIONS IN COBOL PGM:
COMPUTE B = B + A
COMPUTE B = B - A
COMPUTE B = B * A
COMPUTE B = B / A
COMPUTE B = B ** A
COMPUTE B ROUNDED = B + A
COMPUTE B = B + A ON SIZE ERROR DISPLAY “ERROR“
ORDER OF PRECEDENCE OF MATHEMATICAL STATEMENTS
EXPONENTIAL (**) DONE FROM LEFT TO RIGHT
MULTIPLICATION (*) AND DIVISION (/) DONE FROM LEFT TO RIGHT
ADDITION(+) AND SUBTRACTION(-) DONE FROM LEFT TO RIGHT
IF PARENTHESIS EXISTS THEN EVERYTHING IN THE BRACKET IS SOLVED FIRST.
FUNCTIONS IN COBOL
There are over 42 inbuilt functions in COBOL.
they are also called intrinsic functions
These were added in 1989 to the COBOL-85 standard. These are of 6 types.
SYNTAX: FUNCTION function-name(arguments)
Mathematical such as SUM, SQRT → eg: FUNCTION SUM(13,22) or FUNCTION SUM(13 22)
Statistical → MEAN, MAX, MIN
Character → REVERSE, LENGTH eg: FUNCTION LENGTH(‘abcdef’) → 6
Date → CURRENT-DATE
Financial → PRESENT-VALUE
Trigonometric → SIN, COS
Functions can be used within a COMPUTE statement:
COMPUTE C = FUNCTION MAX(13,22,89) + FUNCTION SUM(29 22) / 2
MATHEMATICAL FUNCTIONS
SUM → FUNCTION SUM(13 22 89) O/P:
SQRT → FUNCTION SQRT(9) O/P: 3
REM → FUNCTION REM(12 2) O/P: 0 REMAINDER - WE CAN GIVE DECIMAL
MOD → FUNCTION MOD(12 2) O/P: 0 HERE ALSO WE CAN GIVE DECIMAL BUT REM IS USED FOR THAT
FACTORIAL → FUNCTION FACTORIAL(5) O/P: 120
LOG → FUNCTION LOG(1) O/P: 0
LOG10 → FUNCTION LOG10(1) O/P: 0
INTEGER → FUNCTION INTEGER(4.5) O/P: 4 IT WILL TAKE LESSER VALUE
FUNCTION INTEGER(-4.2) O/P: -5
INTERGER-PART FUNCTION INTEGER-PART(4.5) O/P: 4 IT WILL IGNORE ANYTHING AFTER DECIMAL AND DISPLAY
FUNCTION INTEGER-PART(-4.2) O/P: -4
NUMVAL → FUNCTION NUMVAL(" 10 ") O/P: 000000010 - WILL REMOVE SAPCES AND GIVE THE NUMERICAL VALUE WILL NOT REMOVE $
NUMVAL-C → FUNCTION NUMVAL-C("$ 10 "). O/P: 000000010 - WILL REMOVE SAPCES AND GIVE THE NUMERICAL VALUE WILL ALSO REMOVE $
RANDOM → FUNCTION RANDOM(5) O/P: 0.2747455962350339 - WILL GENERATE RANDOM VALUE IN DECIMAL ALWAYS
STATISTICAL FUNCTIONS
MEAN
MEDIAN
STANDARD-DEVIATION
VARIANCE
RANGE
MID-RANGE
MAX
MIN
ORD-MAX
ORD-MIN
CHARACTER FUNCTIONS
LENGTH
REVERSE
UPPER-CASE
LOWER-CASE
ORD - finds the ordinal position of the collating sequence
CHAR - finds the character based on the ordinal position
FINANCIAL FUNCTIONS
ANNUITY: FUNCTION ANNUITY(INTEREST-RATE NUMBER-OF-PERIODS)
used to find the installment value for the loan
PRESENT-VALUE: FUNCTION PRESENT-VALUE(INTEREST-RATE PAYMENT-VALUE1 PAYMENT-VALUE2..)
used to find the present value for the series of future payments
TRIGONOMETRIC FUNCTIONS
SIN
COS
TAN
ASIN
ACOS
ATAN
IF STATEMENT
IF CONDITION
……………….
……………….
ELSE
……………….
……………….
END-IF.
NESTED IF
IF CONDITION
……………….
……………….
ELSE IF CONDITION
……………….
……………….
ELSE
IF CONDTION
……………….
ELSE
……………….
END-IF.
PERFORM UNTIL STATEMENT
PERFORM PROCEDURE-NAME UNTIL CONDITION
……………….
……………….
STOP RUN.
Used to execute a procedure in a loop.
Control is passed to the procedure named in the PERFORM statement.
After all the statements in the PROCEDURE are executed then the condition is checked again.
The procedure keeps on executing until the condition becomes true.
PERFORM WITH TEST AFTER
PERFORM DISPLAY-PARA
WITH TEST AFTER
UNTIL I > 5
DISPLAY-PARA.
DISPLAY “HELLO WORLD“
COMPUTE I = I +1
The default option is WITH TEST BEFORE.
TEST AFTER: execute the code first then check condition
TEST BEFORE: check the condition first then execute the code
PERFORM with VARYING clause
PERFORM COMPOUND-INTEREST-CAL
VARYING YEARS FROM 1 BY 1
UNTIL YEARS > 5
PERFORM WITH TIMES CLAUSE
To execute multiple times:
PERFORM COMPUND-INTEREST-CALCULATION 5 TIMES
PERFORM COMPOUND-INTEREST-CALC NO-OF-YEARS TIMES
EVALUATE statement
The conditions are checked in sequence. Once the conditions becomes true the EVALUATE is exited.
EVALUATE AGE
WHEN 15 DISPLAY “YOU ARE 15“
WHEN 13 DISPLAY “YOU ARE 13“
WHEN 22 DISPLAY “YOU ARE 22“
WHEN 29 DISPLAY “YOU ARE 29“
WHEN OTHER DISPLAY “YOU ARE NON OF THE ABOVE“
END-EVALUATE.
EVALUATE TRUE
WHEN AGE > 18 DISPLAY “YOU ARE AN ADULT“
END-EVALUATE.
EVALUATE GENDER ALSO AGE > 18
WHEN “FEMALE” ALSO TRUE DISPLAY “YOU ARE ELIGIBLE“
END-EVALUATE.
More than 2 conditions check:
EVALUATE GENDER ALSO AGE-GROUP ALSO CITIZENSHIP
WHEN "FEMALE" ALSO "ADULT" ALSO "USA"
DISPLAY "You are eligible"
WHEN OTHER
DISPLAY "Not eligible"
END-EVALUATE.
AND cannot be used ALSO is the syntax.
Make sure the number of ALSO
s match between EVALUATE
and the corresponding WHEN
.
All conditions are checked side by side — it’s like comparing rows in a table.
REFERENCE MODIFICATION
MOVE FULL-DATE(3:2) TO MONTH
MOVE FULL-DATE(5:4) TO MONTH
Reference modification can be used to refer a portion of a field.
Syntax:
FIeldname(offset:Length)
If length is omitted then all the characters after offset are selected.
STRING STATEMENT
Used to concatenate two or more fields together.
Delimit by size will send the whole field.
Pointer clause determines the starting position on the receiving field.
Overflow clause will get executed when more characters are sent to the receiving field than it can hold.
STRING FIRST-NAME DELIMITED BY SIZE
LAST-NAME DELIMITED BY SIZE
“ABC” DELIMITED BY SIZE
INTO FULL-NAME
WITH POINTER POINTER-FIELD
ON OVERFLOW DISPLAY “OVERFLOW IS THERE”
NOT ON OVERFLOW DISPLAY “NO OVERFLOW IS THERE”
SIZE will copy whole field with the allocated space. we can delimit it by “ “ or “ “ or till any char “A“ or “$” etc.
If the sting in which we are placing the concatenated string is smaller size than the concatenated string then overflow will occur and the message will be displayed.
Pointer will determine the place from where the copied string will be placed in th output string. pointer pic 9(2) value 4 will point to position 4 of the output field and the strings will be copied in that position.
UNSTRING STATEMENT
UNSTRING FULL-NAME DELIMITED BY “ “
INTO FIRST-NAME MIDDLE-NAME LAST-NAME
TALLYING IN COUNTER1
WITH POINTER POINTER-FIELD
ON OVERFLOW DISPLAY “OVERFLOW IS THERE”
NOT ON OVERFLOW DISPLAY “NO OVERFLOW IS THERE”
END_UNSTRING.
Used to divide a field into two or more fields
Pointer clause determines the starting position in the sending field- if we give pointer-field as 5 then the unstring will happen from position 5.
Overflow clause will get executed when more characters are sent to the receiving field than it can hold.
TALLYING counts the number of receiving fields: means in how many variables was the data copied.
INSPECT STATEMENT
INSPECT can be used to count, replace and convert characters in a string.
FULL-NAME= “KIRTHIKA“
INSPECT FULL-NAME
TALLYING COUNTER1 FOR CHARACTERS BEFORE SPACES. o/p:8
INSPECT FULL-NAME
TALLYING COUNTER1 FOR CHARACTERS AFTER “H“. o/p: 4
INSPECT FULL-NAME
TALLYING COUNTER1 FOR ALL “,”.
EXAM-NAME = “***TAY****SWIFT”
INSPECT EXAM-NAME
TALLYING COUNTER1 FOR LEADING “*”. o/p: 3
INSPECT FULL-NAME
TALLYING COUNTER1 FOR ALL “I” BEFORE “T”. o/p: 1 - how many I are there before T.
TALLYING COUNTER1 FOR ALL “I” AFTER “T”. o/p: 1 - how many I are there AFTER T.
INSPECT STATEMENT with REPLACE clause
Replace can replace characters in a string.
INSPECT FULL-NAME
REPLACING ALL “,” BY “ “.
INSPECT FULL-NAME
REPLACING CHARACTERS BY “0“ AFTER “.”.
INSPECT FULL-NAME
REPLACING LEADING “*” BY ZERO.
INSPECT FULL-NAME
REPLACING ALL “CR” BY “ “.
INSPECT FULL-NAME
TALLYING COUNTER1 FOR LEADING “0”
REPLACING LEADING “0” BY “*”. → this will counter the leading 0 and replace it by *.
INSPECT with CONVERTING clause
INSPECT FULL-NAME
CONVERTING “,” TO “ “.
INSPECT FULL-NAME
CONVERTING “ABCDEFGHIJKLMNOPQRSTUVWXYZ” TO “abcdefghijklmnopqrstuvwxyz“. → will convert all upper case to lower case
similar to replace but no need for all keyword.
TABLE in COBOL
An array in Cobol is called as table. Index starts from 1.
01 WEEK-TABLE.
02 DAY-NAME PIC X(10) OCCURS 7 TIMES. → starts from 1 to 7
MOVE ‘MONDAY’ TO DAY-NAME(1).
MOVE ‘TUESDAY’ TO DAY-NAME(2).
MOVE ‘WEDNESDAY’ TO DAY-NAME(3).
01 WEEK-TABLE VALUE “MONDAY TUESDAY WEDNESDAY “. → this will assign values to DAY-NAME based on length- first 10 will go to DAY-NAME(1) and so on.
02 DAY-NAME PIC X(10) OCCURS 7 TIMES.
Display full table:
PERFORM VARYING I FROM 1 BY 1 UNTIL I >7
DISPLAY DAY-NAME(I)
END-PERFORM.
MULTI LEVEL TABLE
01 STUDENT-TABLE.
05 STUDENTS OCCURS 30 TIMES.
10 STUDENT-NAME PIC X(15).
10 SUBJECTS PIC 9(3) OCCURS 3 TIMES.
Moving values:
MOVE “TAYLOR 13 22 89“ TO STUDENT-TABLE.
or
MOVE “TAYLOR 13 22 89“ TO STUDENTS(1).
DISPLAY STUDENT-TABLE.
DISPLAY STUDENTS(1).
DISPLAY SUBJECTS(1 1). → student 1’s first subject
DISPLAY SUBJECTS(1 2). → student 1’s second subject
Search a TABLE using PERFORM
01 WEEK-TABLE.
05 DAY-NAME PIC X(10) OCCURS 7 TIMES.
01 TO-BE-FOUND PIC X(10) VALUE “FRIDAY“
PERFORM VARYING I FROM 1 BY 1 UNTIL I >7
IF DAY-NAME(I) = TO-BE-FOUND
DISPLAY DAY-NAME(I)
END-IF
END-PERFORM.
Reading a line from Sequential file