Professional Documents
Culture Documents
Overview
COBOL Fundamentals
DAY1
Session Plan
Day 1:
Introduction to COBOL Evolution, Features & Language Fundamentals Program Structure Data description entry
References
M.K.Roy and D. Ghosh Dastidar, COBOL Programming, Tata McGraw Hill, New York, 1973.
Nancy Stern and Robert Stern, COBOL Programming, John Wiley & Sons, New York, 1973.
Newcomer and Lawrence, Programming with Structured COBOL, McGraw Hill Books, New York, 1973.
History of COBOL
1959 United States Department of Defense
1960 - COBOL initial specifications presented by CODASYL (Conference on Data Systems Languages)
BASIC COBOL extended to Visual COBOL ANSI (American National Standards Institute) developed American National Standard (ANS) COBOL ANSI published revised version of (ANS) COBOL Business applications needed to manipulate character as well as numeric data String operations added
COBOL
What does COBOL stand for?
COmmon Business Oriented Language.
COBOL
PROGRAM
Program Structure
Principal portions of a program. There are 4 divisions a) Identification (Required) b) Environment (Optional) Userc) Data (Optional) defined chunk of code which consists of one/more d) Procedure (Required) paragraphs. e.g. defined chunk of code User a) U000-CHECK-LOGone/more which consists of SECTION. b) FILE SECTION. sentences. e.g. A SENTENCE consists of one or a) P000-PRINT-FINAL-TOTALS. more statements and is b) A STATEMENT consists of a PROGRAM-ID. COBOL full and terminated by a verbstop. an e.g. a) MOVE operand or operands. .21 TO VAT-RATE RESERVEDb) e.g. WORDS VAT-AMOUNT = COMPUTE SUBTRACT T-TAX CHARACTERS PRODUCT-COST * VAT-RATE. FROM GROSSPAY GIVING NET-PAY
DIVISIONS
SECTIONS
PARAGRAPHS
SENTENCES
STATEMENTS
Overview
Character
Meaning
Space + * / = $ , ; . " ( ) > < : ' A-Z a-z 0-9 Plus sign Minus sign or hyphen Asterisk Forward slash or solidus Equal sign Currency sign1 Comma Semicolon Decimal point or period Quotation mark2 Left parenthesis Right parenthesis Greater than Less than Colon Apostrophe Alphabet (uppercase) Alphabet (lowercase) Numeric characters
IDENTIFICATION DIVISION
Compiler takes this as Program Identifier. PROGRAM-ID comes immediately after ID Division.
IDENTIFICATION DIVISION. PROGRAM-ID. PROG1. AUTHOR. R.R. BHATT. INSTALLATION. ABC CORP. DATE-WRITTEN. 01-JAN-2005. DATE-COMPILED. 01-JAN-2005. SECURITY. HIGH.
OPTIONAL
ENVIRONMENT DIVISION
ENVIRONMENT DIVISION
CONFIGURATION SECTION
INPUT-OUTPUTT SECTION
DATA DIVISION
The DATA DIVISION is used to describe the data structures used in the program. There are sections in the DATA DIVISION
The two most commonly used components (sections) are a) WORKING-STORAGE SECTION Internal data structures are defined here. b) FILE SECTION File I/O buffer areas are defined here.
DATA DIVISION
DATA DIVISION. FILE SECTION. FD INVENTORY-FILE RECORD CONTAINS 78 CHARACTERS. 01 INVENTORY-REC. 05 IF-PART-NUMBER 05 05 IF-WHSE-LOCS. 10 IF-MAIN-LOC 10 IF-ALT-LOC 05 FD PRINT-FILE. 01 PRINT-REC. 05 05 P-PART-NUMBER 05 05 P-MAIN-LOC 05 05 P-ALT-LOC WORKING-STORAGE SECTION. 01 FLAGS. 05 F-MORE-RECORDS
PIC X(09). PIC X(24). PIC X(06). PIC X(06). PIC X(33).
PROCEDURE DIVISION ..
The PROCEDURE DIVISION consists of the following
Sections
Paragraphs
Sentences
Statements
PROCEDURE DIVISION
Section
Section contain one or more Paragraphs.
PROCEDURE DIVISION. 0001-ACCOUNT-SECTION. 001-ACCOUNT-READ-PARA. READ ACC-FILE AT END MOVE Y TO EOF. MOVE TAX-REDUCT TO TAX-AMOUNT 001-ACCOUNT-VALIDATE-PARA. ADD AMOUNT TO TOT-AMOUNT. ACCEPT EMPLOYEE-SALARY DISPLAY Current Employee Salary EMPLOYEE-SALARY. 001-EXIT-PARA. STOP RUN.
Paragraph
A PARAGRAPH comprises of one or more sentences
A SENTENCE is a combination of one or more statements and is terminated by a full stop.
Sentences
statement
A STATEMENT is a combination of a COBOL verb and one or more operands.
IDENTIFICATION DIVISION. PROGRAM-ID. FIRSTPG. PROCEDURE DIVISION. A0000-MAIN-PARA. DISPLAY ------------------------------- . DISPLAY WELCOME TO COBOL . DISPLAY -------------------------------- . STOP RUN.
Column numbers
Area A
Area B
I D E N T
I F I C A T I O N
A R E A
AREA A *) Between Column 8 to 11 *) Division, Section, Paragraph names, FD entries & 01 level entries must start in Area A
All COBOL DIVISIONs, SECTIONs, paragraphs and some special entries must begin in Area A. All COBOL statements must
Data names
Are named memory locations. Must be described in the DATA DIVISION before they can be used in the PROCEDURE DIVISION. Can be of elementary or group type. Can be subscripted for Arrays. Are user defined words .
DATA NAME
Level number
LEVEL NO
Level Number
01 02 to 49 66 77 88
Purpose
Record description and independent items Fields within records and sub items RENAMES clause Independent items Condition names
Meaning
Numeric Alphabetic Alphanumeric Implicit Decimal Sign bit
9 A X V S
PICTURE 999 PICTURE S999 PICTURE XXXX PICTURE 99V99 PICTURE S9V9
a three digit (+ive only) integer a three digit (+ive/-ive) integer a four character text item or string a +ive real in the range 0 to 99.99 a +ive/-ive real in the range ?
If you wish you can use the abbreviation PIC. Numeric values can have a maximum of 18 (eighteen) digits (i.e. 9 s). The limit on string values is usually system-dependent.
PIC 9(6) is equivalent to PICTURE 999999 PIC 9(6)V99 is equivalent to PIC 999999V99 PICTURE X(10) is equivalent to PIC XXXXXXXXXX PIC S9(4)V9(4) is equivalent to PIC S9999V9999 PIC 9(18) is equivalent to PIC 999999999999999999
We can give a starting value to variables by means of an extension to the picture clause called the value clause.
DATA DIVISION. WORKING-STORAGE SECTION. 01 Num1 PIC 999 VALUE ZEROS. 01 VatRate PIC V99 VALUE .18. 01 StudentName PIC X(10) VALUE SPACES. DATA
Num1 VatRate StudentName
000
.18
Is used to assign an initial value to a elementary data item. The initial value can be numeric literal, non- numeric literal or figurative constant. Is an optional clause.
Literals
Literals are symbols whose value does not change in a program. There are 3 types of literals namely (1) Numeric literals. (2) Non-numeric literals. (3) Figurative constants.
Literals
Figurative constants
ZERO(S) or ZEROES SPACE(S) HIGH-VALUE(S) LOW-VALUE(S) QUOTE(S)
ALL literal
01
StudentName
M I K E
01
StudentName
- - - - - - - - - -
WORKING-STORAGE SECTION. 01 EMPLOYEE-DETAILS PIC X(30). 01 EMPLOYEE-DETAILS. 05 EMP-NUM PIC 9(4). 05 EMP-NAME PIC X(10). 05 EMP-DEPT PIC X(4). 05 EMP-LOC PIC X(12).
A Group item is declared using a level number and a data name. It cannot have a picture clause. Where a group item is the highest item in a data hierarchy it is referred to as a record and uses the level number 01. Picture clauses are NOT specified for group data items because the size of a group item is the sum of the sizes of its subordinate, elementary items and its type is always assumed to be PIC X.
Group item
Sub-Items
Value
1234JyothiS E&R Bangalore
Value
1234JyothiS 1234 JyothiS E&R Bangalore E&R Bangalore
WORKING-STORAGE SECTION. 01 POLICY-DETAILS. 05 POLICY-NO. 10 POLICY-TYP PIC X(4). 10 POLICY-LOC PIC X(2). 10 POLICY-ID PIC X(5). 05 POLICY-TYPE PIC X(10). 05 POLICY-EXPDT PIC X(10).
In COBOL, Level numbers are used to express data hierarchy. The higher the level number, the lower the item is in the hierarchy. So Group items contain sets of elementary items with lower level numbers. At the lowest level the data is completely atomic.
DATA DIVISION. WORKING-STORAGE SECTION. 01 WS-REGNO PIC X(5). 01 WS-NAME. 05 WS-FIRST-NAME PIC A(15). 05 WS-MID-NAME PIC A(15). 05 WS-LAST-NAME PIC A(10). 01 WS-AGE PIC 99V99. 01 WS-SCHOLARSHIP PIC 9(4) VALUE 1000.
Group Items/Records
WORKING-STORAGE SECTION. 01 StudentDetails PIC X(26).
StudentDetails
H E N N E S S Y R M 9 2 3 0 1 6 5 L M 5 1 0 5 5 0 F
Group Items/Records
WORKING-STORAGE SECTION. 01 StudentDetails. 02 StudentName 02 StudentId 02 CourseCode 02 Grant 02 Gender
StudentDetails
H EN N E S S Y RM 9 2 3 0 1 6 5 L M 5 1 0 5 5 0 F
StudentName StudentId CourseCode Grant Gender
Group Items/Records
WORKING-STORAGE SECTION. 01 StudentDetails. 02 StudentName. 03 Surname 03 Initials 02 StudentId 02 CourseCode 02 Grant 02 Gender
StudentDetails H EN N E S S Y RM 9 2 3 0 1 6 5 L M 5 1 0 5 5 0 F
StudentName Surname StudentId Initials CourseCode Grant Gender
MOVE VERB
Overview
MOVE copies data from the source identifier or literal to one or more destination identifiers. MOVE copies data to Group or elementary data items. MOVE always performs LEFT JUSTIFICATION to Character MOVE always perform RIGHT JUSTIFICATION to Numeric data. When data is MOVEd into an item the contents of the item are completely replaced.
MOVEing Data
MOVEing Data
01 Surname R Y A N
PIC X(8).
MOVEing Data
01 Surname F I T Z P
PIC X(8). A T R I C K
01 GrossPay
PIC 9(4)V99.
GrossPay
0 0 0 0 0 0
GrossPay
0 0 1 2
GrossPay
4 0
2 3
4 5 6
7 5 7
01 CountyPop 01 Price
1 2 3 4
CountyPop
0 1
Price
2 4
1 5 4
0 0
Price
3 5 5 2
7 5
Before
After
WS00-OUT1 WS00-OUT2
0000 000000
WS00-OUT1 WS00-OUT2
3456 345678
Before
WS00-OUT3 After
000000
WS00-OUT3
123456
Before
After
WS00-OUT4
00000000
WS00-OUT4
12345678
MOVE .. example
****************************
Output SPOOL
From time to time it may be useful to display messages and data values on the screen. A simple DISPLAY statement can be used to achieve this. A single DISPLAY can be used to display several data items or literals or any combination of these. The WITH NO ADVANCING clause suppresses the carriage return/line feed.
01 CurrentDate
* YYMMDD * YYDDD
01 DayOfYear 01 Day0fWeek
* D (1=Monday)
01 CurrentTime
* HHMMSSss s = S/100
DATA DIVISION. WORKING-STORAGE SECTION. 01 StudentDetails. 02 StudentName. 03 Surname PIC 03 Initials PIC 02 StudentId PIC 02 CourseCode PIC 02 Grant PIC 02 Gender PIC 01 CurrentDate. 02 CurrentYear 02 CurrentMonth 02 CurrentDay 01 DayOfYear. 02 FILLER 02 YearDay 01 CurrentTime. 02 CurrentHour 02 CurrentMinute 02 FILLER
PIC 99. PIC 99. PIC 99. PIC 99. PIC 9(3). PIC 99. PIC 99. PIC 9(4).
PROCEDURE DIVISION. Begin. DISPLAY "Enter student details using template below". DISPLAY "NNNNNNNNNNSSSSSSSCCCCGGGGS ". ACCEPT StudentDetails. ACCEPT CurrentDate FROM DATE. ACCEPT DayOfYear FROM DAY. ACCEPT CurrentTime FROM TIME. DISPLAY "Name is ", Initials SPACE Surname. DISPLAY "Date is " CurrentDay SPACE CurrentMonth SPACE CurrentYear. DISPLAY "Today is day " YearDay " of the year". DISPLAY "The time is " CurrentHour ":" CurrentMinute. STOP RUN.
Overview