Introduction to COBOL COBOL is an acronym which

Introduction to COBOL

COBOL is an acronym which stands for COMMON BUSINESS ORIENTED LANGUAGE COBOL is used for developing typically file oriented BUSINESS applications

Suitable for batch processing applications like Payroll, Telephone, Electricity Billing and Financial Accounting etc. , Batch processing is also known as periodical processing

With CICS (Customer Information Control System) as front end, COBOL is suitable for Online applications like Railway Reservation, Online Banking etc. COBOL is not suitable for developing system programs like OS, drivers COBOL is one of the oldest computer languages in use (it was developed around the end of the 1950 s).

ADVANTAGES OF COBOL o Higher Level Language o English Like Language o Self documenting Language o File handling capability o Easy to write and available across several platforms o Largely Machine independent

COBOL was developed in 1959 by a group called CODASYL (COnference on DAta SYstems Language) Later ANSI standards were introduced o COBOL – 68 o o COBOL – 74 COBOL – 85 COBOL – 2003 Enterprise COBOL

FUTURE OF COBOL o o o COBOL will remain an important future also language in Billions of lines of COBOL source are currently in use with millions added each year Since 1000 s of COBOL programmers retire every year there is a continuous requirement of trained man power

FUTURE OF COBOL n Trained man power in COBOL for Maintenance and Development of COBOL based applications

COMPARISON OF IBM COBOL COMPILERS

EXTENSIONS FOR OOCOBOL, C INTEROPERABLITY COBOL 74 STD. COBOL 85 STD. DBCS, IMPROVED CICS INTERFACE, 31 BIT ADDRESSING , SAA SUPPORT, ETC. COBOL 74 STD. OS/VS COBOL II INTRINSIC FUNCTIONS, SUPPORT FOR LANGUAGE ENVIRONMENT COBOL 85 STD. DBCS, IMPROVED CICS INTERFACE, 31 BIT ADDRESSING, SAA SUPPORT, ETC. COBOL 74 STD. COBOL/370 COBOL FOR MVS & VM IBM's latest host COBOL compiler

- For writing a COBOL program you have to be provided with program specification by your BUSINESS ANALYST - ANALYST studies a customer’s requirement for an application package and prepares an over all design and finally comes out with a detailed specification for every program to be developed under an application

The specification will contain the details of INPUT/OUTPUT file descriptions, OUTPUT report layouts and descriptions, processing specifications, calculation methods etc. , A set of test data is required to ensure that the programs work as intended by the designer and customer

A COBOL Program is divided into o o o DIVISION SECTION PARAGRAPH SENTENCE STATEMENT WORDS CHARACTERS

COBOL words are two categories, reserved words and user defined words USER defined words can be variables declared in DATA DIVISION and PARAGRAPH NAMES in PROCEDURE DIVISION, Which we will see later

COBOL RESERVED WORDS The COBOL language has over 300 words which have a special significance to the compiler are called RESERVED words USER defined variables must not coincide with RESERVED words Reserved Word Examples below shown: DIVISION, SECTION, STORAGE, ADD, ACCEPT, STOP, COMMA, DIVIDE, DYNAMIC, LINKAGE , READ , WRITE ETC. ,

COBOL Coding Format Columns | | | 1 | 2 | 3 | 4 | 5 | 6 | 7 | 8 | 9 | 10 | 11 | 12 | 13 | 14 | 15 | 16 |……………| 71 | 72 | |----- 1 ------| |------3 ---|------------------| 4 Rows 2 Indicator Area Characters 1. 2. 3. * (Asterisk) - Comment / (Slash) - Comment + page feed - (Hyphen) - String Continuation 1. 2. 3. 4. Sequence Number Area Indicator Area ‘A’ or Margin ‘A’ Area ‘B’ or Margin ‘B’

Margin A is to be used for writing o o o DIVISION NAMES SECTION NAMES PARAGRAPH NAMES 01 77 LEVEL ENTRIES FD ENTRIES Margin B is to be used for writing o o o 02 -49 66 88 LEVEL ENTRIES SELECT STATEMENTS PROCEDURE DIVISION STATEMENTS AND SENTENCES

Four Divisions of COBOL DIVISIONS/SECTIONS identify various parts of a COBOL program File declarations, descriptions, temporary variables, processing logic, communication with other programs etc are handled by various DIVISONS/SECTIONS of a COBOL program

Four Divisions of COBOL There are four DIVISIONS in all. IDENTIFICATION DIVISION ( MANDATORY ) ENVIRONMENT DIVISION ( OPTIONAL ) DATA DIVISION ( OPTIONAL ) PROCEDURE DIVISION ( MANDATORY )

Functions of the four divisions n The IDENTIFICATION DIVISION is used to specify the name of program and programmer. n The ENVIRONMENT DIVISION is used to define the data sets that are used by a program and links the data sets to the corresponding DD names of JCL

. Functions of the four divisions n As the name suggests, the DATA DIVISION is used to provide the complete descriptions of data sets including record description and field description and working storage variables. n The PROCEDURE DIVISION contains executable instructions ( statements containing VERBS ) to process the data contained in DATA DIVISION.

Functions of the four divisions COBOL provides a means for specifying sequence, selection and iteration constructs to code your processing logic.

COBOL coding rules n COBOL coding lines start in Margin A or Margin B n DIVISION, SECTION, PARAGRAPH names start in Margin A n FD entries, 01, 77 level entries, Procedure division paragraph names start in Margin A. n Where as STATEMENTS, 02 -49, 66, 88 level entries start in Margin B. n Again, Procedure division sentences, statements start in Margin B.

A SAMPLE COBOL program ( just see the structure - don’t worry about the details) IDENTIFICATION DIVISION. PROGRAM-ID. FIRST 001. AUTHOR. SRG. DATA DIVISION. WORKING-STORAGE SECTION. 77 NUM 1 PIC 9 VALUE ZEROS. 77 NUM 2 PIC 9 VALUE ZEROS. 77 RESULT PIC 99 VALUE ZEROS. 77 WS-NAME PIC X(10) VALUE ‘MAPLES’. PROCEDURE DIVISION. CALC-RTN 1. ACCEPT NUM 2. MULTIPLY NUM 1 BY NUM 2 GIVING RESULT. DISPLAY "RESULT IS = ", RESULT. STOP RUN.

Another SAMPLE COBOL program. IDENTIFICATION DIVISION. PROGRAM-ID. TEST 1. AUTHOR. SRG. ENVIRONMENT DIVISION. INPUT-OUTPUT SECTION. FILE-CONTROL. SELECT STUDENT-FILE ASSIGN TO DD 1 ORGANIZATION SEQUENTIAL ACCESS SEQUENTIAL. DATA DIVISION. FILE SECTION. FD STUDENT-FILE. 01 STUDENT-REC. 02 ST-EC PIC 9(4). 02 ST-ENAME PIC X(15). 02 ST-BASIC-PAY PIC 9(4)V 99. Continued. .

WORKING-STORAGE SECTION. 77 WS-CTR PIC 9(5) VALUE 123. 01 WS-REC. 02 WS-EC PIC 9(4). PROCEDURE DIVISION. DISPLAY …………. . OPEN ………. . RTN 1. READ …………AT END CLOSE …… NOT AT END MOVE ………. COMPUTE …………… ADD ………. WRITE ………… GO TO RTN 1 END-READ.

IDENTIFICATION DIVISION - IT IS THE SMALLEST AND LEAST SIGNIFICANT DIVISION - IT IS USED TO IDENTIFY THE PROGRAM AND AUTHOR - IT DOES NOT HAVE SECTIONS - IT CONTAINS ONLY PARAGRAPHS IDENTIFICATION DIVISION PROGRAM-ID. TEST 123. AUTHOR. RAJ. INSTALLATION. MAPLES. DATE-WRITTEN. 21 -08 -1999. DATE-COMPILED. 23 -08 -1999.

IDENTIFICATION DIVISION PROGRAM-ID is the only required paragraph for this division Program name can be 1 – 30 characters with at least one Alphabet, but Mainframe accepts only 8 characters as program name All other entries are OPTIONAL and for documentation purpose only

ENVIRONMENT DIVISION § THIS FOLLOWS THE IDENTIFICATION DIVISION. § THIS IS THE MOST HARDWARE DEPENDENT DIVISION. § THE COMPUTER AND OTHER DEVICE REQUIREMENTS ARE DESCRIBED IN THIS SECTION. § IT HAS 2 SECTIONS

ENVIRONMENT DIVISION. CONFIGURATION SECTION. SOURCE COMPUTER. S 390. OBJECT COMPUTER. S 390. (where the program is compiled) (where the program is executed) INPUT-OUTPUT SECTION. FILE-CONTROL. SELECT employee-master ASSIGN TO DD 1. organization is sequential. I-O-CONTROL. SPECIAL-NAMES. CURRENCY SIGN IS literal-1. DECIMAL POINT IS COMMA.

DATA DIVISION - Has two sections FILE SECTION and WORKINGSTORAGE SECTION - Describes the characteristics of files used in the program like REC SIZE, BLOCK SIZE, FORMAT ETC… - It contains RECORD STRUCTURE and FIELD DESCRIPTION entries. - All the above are declared in FILE SECTION

DATA DIVISION - DATA DIVISION has working-storage section to declare temporary memory variables used in the program. - In essence, the contents of FILE SECTION variables are stored in the devices like hard disk/printer where as the contents of WORKING-STORAGE section are in RAM buffers and are lost once the program is terminated

DATA DIVISION HAS SIX SECTIONS AS PER STANDARDS BUT MAINFRAME COBOL HAS ONLY FOUR SECTIONS • • FILE SECTION. WORKING-STORAGE SECTION. LINKAGE SECTION. LOCAL-STORAGE SECTION. • • REPORT SECTION SCREEN SECTION. The last 2 sections are not supported in IBM MAINFRAME COBOL SCREEN SECTION REQUIREMENTS ARE TAKEN CARE BY THE OLTP SOFTWARE CICS (Customer Information Control System)

DATA DIVISION The DATA DIVISION has the following structure DATA DIVISION. FILE SECTION. ………. . FILE SECTION ENTRIES …………. . WORKING-STORAGE SECTION. ……TEMPORARY MEMEORY VARIABLES, GROUP AND ELEMENTARY ITEMS. . EXAMPLE: IDENTIFICATION DIVISION. PROGRAM-ID. SAMPLE 1. AUTHOR. SRG. DATA DIVISION. WORKING-STORAGE SECTION. 01 Num 1 PIC 9 VALUE ZEROS. 01 Num 2 PIC 9 VALUE ZEROS. 01 Result PIC 99 VALUE ZEROS.

VARIABLES Let us start with working- storage section VARIABLES can be declared in WS section Variables are also known as data items or identifiers A data item starts with a level number followed by name, PIC clause and optionally VALUE and other clauses

VARIABLES A variable name can be maximum of 30 characters consisting of A-Z, 0 -9, hiphen No other special characters, no blanks are allowed. Must have at least one Alphabet

DATA ITEMS / VARIABLES Data Items can be ELEMENTARY OR GROUP ITEMS ELEMENTARY ITEM is one that cannot be further subdivided A set of related ELEMENTARY ITEMS is called a GROUP ITEM 01 LEVEL is used for declaring group items

DATA ITEMS / VARIABLES ELEMENTARY ITEMS which are not part of any Group is called INDEPENDENT ELEMENTARY ITEMS Generally Independent elementary items are declared in Working Storage Section with Level Number 77 Independent elementary items can also be declared with level number 01 from COBOL-85 version

DATA ITEMS / VARIABLES - 01 AND 77 LEVEL ITEM NAMES MUST BE UNIQUE - 01 LEVEL is also used for declaring Group Items, in which case it may have sub level items with level numbers 02 -49 - 02 -49 Level data items may have duplicates ( Duplicate items can be qualified – later we will discuss) - 01 - 77 levels are used in MARGIN-A where as other levels in MARGIN-B

COBOL Data Types There are three basic data types in COBOL Numeric, Alphabetic, Alphanumeric Data type is important because it determines how the data has to be stored inside the variable and what operations are valid on such variables

COBOL Data Types The contents in ALPHANUMERIC items are left justified The contents in NUMERIC items are right justified n COBOL’s weakness is, it is poor in type checking and enforcing

How to declare Variables in WS? Variables are declared with a level number, user defined name and PIC clause WORKING-STORAGE SECTION. n n n 77 WS-NAME PIC X(15) VALUE “MAPLES”. MAPLES----- 77 WS-CTR 1 PIC 9(6) VALUE 1234. 001234 77 WS-CTR 2 PIC 9(4) VALUE ZERO. 0000 77 WS-LINE PIC X(10) VALUE ALL “-”. 77 WS-ADDR PIC X(10) VALUE SPACES. Blank Spaces 77 WS-AMT PIC 9(4)v 9(3) value 12. 56 0012560 ------ Level number 77 indicates the item is independent data item PIC means PICTURE Clause indicating the type and size of the item Type can be X A 9 indicating alphanumeric, alphabetic, Numeric items

How to declare Variables in WS? VALUE indicates the initial contents of the variable The constants are three types in COBOL STRING CONSTANT, NUMERIC CONSTANT, FIGURATIVE CONSTANT

How to declare Variables in WS? Though the initial values are called constants, the contents can be modified late in the program The max size of X and A is 32000 chars without value clause and 160 with VALUE clause For numeric data item max allowed is 18 digits but mainframe COBOL allows up to 31 digits ( you have to set the compiler option PARM. COBOL=ARITH(EXTEND)

More examples 77 WS-CTR 2 PIC 9(05) 77 EMP-NAME 01 EMP-ADDRESS PIC A(15) X(12) 01 WS-GRP. 02 WS-EC 02 WS-EN 02 WS-DOB. 03 WS-DD 03 WS-MM 03 WS-YY 01 WS-GRP. 05 WS-EC 05 WS-EN 05 WS-DOB. 10 WS-DD 10 WS-MM 10 WS-YY VALUE 123. ( STORED AS 00123 ) ‘MAPLES’. “ 12, III STREET”. PIC 9(03). PIC X(12). PIC 9(02). PIC 9(03). PIC X(12). PIC PIC 9(02).

Figurative Constants n COBOL provides its special type of constants called Figurative Constants. n SPACE or SPACES n ZERO or ZEROS or ZEROES equivalent to 0 n QUOTE or QUOTES = " n HIGH-VALUE or HIGH-VALUES = Max Value n LOW-VALUE or LOW-VALUES = Min Value n ALL literal = filling with character

PROCEDURE DIVISION n The PROCEDURE DIVISION is where all the data described in the DATA DIVISION is processed. n It contains the logic to process the data. n There must be at least one paragraph, sentence and statement in the PROCEDURE DIVISION.

PROCEDURE DIVISION n The PROCEDURE DIVISION is hierarchical in structure and consists of Sections, Paragraphs, Sentences and Statements. n The paragraph and section names in this division are chosen by the programmer meaningfully

PROCEDURE DIVISION This is the fourth division of a COBOL PROGRAM. It contains SECTIONS/ PARAGRAPHS defined by users PARAGRAPHS Contain ( Instructions ) Statements to be executed

PROCEDURE DIVISION n STATEMENTS are used to process the data items n A STATEMENT starts with a verb like OPEN, ADD, MOVE, DISPLAY etc. , n A set of COBOL STATEMENTS can be grouped using a PARAGRAPH NAME

PROCEDURE DIVISION o PARAGRAPH MUST BEGIN IN MARGIN A o MUST NOT HAVE MORE THAN 30 CHARACTERS. o MUST NOT BE A COBOL o MUST NOT HAVE ANY HYPHEN (-) o MUST BE generally RESERVED WORD. SPECIAL CHARACTERS UNIQUE. EXCEPT

PROCEDURE DIVISION. MAIN-PARA. ADD A TO B. MOVE B TO F. . . EXIT-PARA. STOP RUN. ------------------------------------------------------------- MINIMUM COBOL RPOGRAM IDENTIFICATION DIVISION. PROGRAM-ID. SAMPLE 2. PROCEDURE DIVISION. DISPLAY “HELLO WORLD". STOP RUN.

First PROCEDURE DIVISION STATEMENT Display statement will show the contents of a variable or literal in the output or SPOOL (SYSOUT ) IDENTIFICATION DIVISION. PROGRAM-ID. PRG 1. DATA DIVISION. WORKING-STORAGE SECTION. 77 WS-NAME PIC X(10) VALUE ‘MAPLES’. PROCEDURE DIVISION. DISPLAY “HALLO” WS-NAME. STOP RUN. Note: DISPLAY statement IN MAIN FRAME COBOL CANNOT control the LINE or COLUMN position of the SCREEN

You have to use multiple display statements to show the contents on separate lines IDENTIFICATION DIVISION. PROGRAM-ID. DEMOPRG 1. DATA DIVISION. WORKING-STORAGE SECTION. 77 WS-NAME PIC X(10) VALUE ‘MAPLES’. PROCEDURE DIVISION. DISPLAY “HALLO” DISPLAY WS-NAME. DISPLAY “HOW ARE YOU? ”. STOP RUN.

Program Preparation Allocate the following PDS in the following format <USERID>. DEV. JOB (To keep all your JCL’s) <USERID>. DEV. PROCLIB (To keep all your Procedure) <USERID>. DEV. SOURCE (To keep all your programs) <USERID>. DEV. LOADLIB (To keep all your Load Module)

Procedure to compile and execute a COBOL program using two JCLs COMPLINK and RUNJCL (You have to use two JCL’s one to compile and create load module and one to run your program (RUNJCL) COMPLIER JCL //MAPLE 06 R JOB NOTIFY=MAPLE 06, CLASS=M // JCLLIB ORDER=(ZOS. PROCLIB) //S 1 EXEC PROC=IGYWCL, MEM=PRG 1 //COBOL. SYSIN DD DSN=<USERID. DEV. SOURCE>(&MEM), DISP=SHR //LKED. SYSLMOD DD DSN=<USERID. DEV. LOADLIB>(&MEM), DISP=SHR // THIS JCL IS USED TO COMPILE YOUR COBOL PROGRAM PRG 1, CHECK FOR SYNTAX ERRORS AND SAVE THE LOAD MODULE IN <USERID>. DEV. LOADLIB IGYWCL IS A CATALOGED PROCEDURE GIVEN BY IBM

How to execute your load module? RUNJCL //MAPLE 06 R JOB NOTIFY=&SYSUID, CLASS=M //JOBLIB DD DSN=<USERID. DEV. LOADLIB>, DISP=SHR //S 1 EXEC PGM=PRG 1 //SYSPRINT DD SYSOUT=* //SYSIN DD * 1234 KRISHNA TNAGAR ADMIN …………. /* // ================================= NOTE: You can also use the procedure IGYWCLG which can compile, link and execute with a single JCL //GO. SYSIN DD * is to be used for giving the input

ACCEPT thru’ SYSIN DD ACCEPT statement reads a line from SYSIN DD * of RUNJCL and stores the data in the variable. Data should be given in INTERNAL FORMAT IDENTIFICATION DIVISION. PROGRAM-ID. DEMOPRG 1. DATA DIVISION. WORKING-STORAGE SECTION. 77 WS-NAME PIC X(10). 77 WS-NUM PIC 9(5). PROCEDURE DIVISION. ACCEPT WS-NAME. DISPLAY WS-NAME. ACCEPT WS-NUM. DISPLAY WS-NUM. STOP RUN. IMPORTANT: INPUT DATA IS TO BE GIVEN THRU SYSIN DD * IN RUNJCL Giving data thru SYSIN DD * is different from VALUE CLAUSE For numeric item input should be given as: 00123 NOT AS 123

EXAMPLE IDENTIFICATION DIVISION. PROGRAM-ID. DEMOEX 01. DATA DIVISION. WORKING-STORAGE SECTION. 77 NAME PIC X(15). 01 AGE PIC 9(2). PROCEDURE DIVISION. PARA-1. ACCEPT NAME. ACCEPT AGE. DISPLAY “HELLO " NAME. DISPLAY “YOUR AGE IS " AGE. STOP RUN.

ACCEPT variable FROM {DATE/TIME/DAY-OF-WEEK } DISPLAY VAR 1/LIT 1 VAR 2/LIT 2. . DATE etc… are COBOL reserved data-items ACCEPT can be followed by only one data-item [ YYYYMMDD ].

You have to use multiple display statements to show the contents on separate lines IDENTIFICATION DIVISION. PROGRAM-ID. DEMOPRG 1. DATA DIVISION. WORKING-STORAGE SECTION. 77 WS-NAME PIC X(10). PROCEDURE DIVISION. ACCEPT WS-NAME DISPLAY “HALLO” DISPLAY WS-NAME. DISPLAY “HOW ARE YOU? ”. STOP RUN.

You have to use multiple display statements to show the contents on separate lines IDENTIFICATION DIVISION. PROGRAM-ID. DEMOPRG 1. DATA DIVISION. WORKING-STORAGE SECTION. 77 NUMB PIC 9(3)V 9(3) VALUE 12. 34. PROCEDURE DIVISION. ACCEPT NUMB DISPLAY NUMB. STOP RUN. IMPORTANT: THRU SYSIN DD * YOU HAVE TO GIVE INPUT AS 012340 WITHOUT DOT

ACCEPT can be used to capture system registers like DATE, TIME etc. IDENTIFICATION DIVISION. PROGRAM-ID. DEMODAT 0. DATA DIVISION. WORKING-STORAGE SECTION. 77 WS-DATE PIC 9(8). PROCEDURE DIVISION. ACCEPT WS-DATE FROM DATE YYYYMMDD. DISPLAY WS-DATE. STOP RUN.

ACCEPT variations n ACCEPT WS-NAME n ACCEPT WS-DATE FROM DATE n ACCEPT WS-TIME FROM TIME n ACCEPT WS-DAY FROM DAY YYYYDDD n ACCEPT WS-DWK FROM DAY-OF-WEEK n YYYYMMDD

IDENTIFICATION DIVISION. PROGRAM-ID. DEMODAT 1. DATA DIVISION. WORKING-STORAGE SECTION. 77 WS-TIME PIC 9(8) VALUE 0. PROCEDURE DIVISION. ACCEPT WS-TIME FROM TIME. DISPLAY WS-TIME. STOP RUN.

IDENTIFICATION DIVISION. PROGRAM-ID. DEMOWK 01. DATA DIVISION. WORKING-STORAGE SECTION. 77 WS-DAY PIC 9(6) VALUE 0. PROCEDURE DIVISION. ACCEPT WS-DAY FROM DAY YYYYDDD. DISPLAY WS-DAY. ACCEPT WS-DWK FROM DAY-OF-WEEK DISPLAY WS-DWK. STOP RUN.
- Slides: 66