COBOL is a high-level programming language first developed by the CODASYL Committee (Conference on Data Systems Languages) in 1960

. Since then, responsibility for developing new COBOL standards has been assumed by the American National Standards Institute (ANSI). Three ANSI standards for COBOL have been produced: in 1968, 1974 and 1985. A new COBOL standard introducing object-oriented programming to COBOL, is due within the next few years. The word COBOL is an acronym that stands for COmmon Business Oriented Language. As the the expanded acronym indicates, COBOL is designed for developing business, typically file-oriented, applications. It is not designed for writing systems programs. For instance you would not develop an operating system or a compiler using COBOL. For over four decades COBOL has been the dominant programming language in the business computing domain. In that time it it has seen off the challenges of a number of other languages such as PL1, Algol68, Pascal, Modula, Ada, C, C++. All these languages have found a niche but none has yet displaced COBOL. Two recent challengers though, Java and Visual Basic, are proving to be serious contenders. COBOL's dominance in underlined by the reports from the Gartner group. In 1997 they estimated that there were about 300 billion lines of computer code in use in the world. Of that they estimated that about 80% (240 billion lines) were in COBOL and 20% (60 billion lines) were written in all the other computer languages combined [Brown]. In 1999 they reported that over 50% of all new mission-critical applications were still being done in COBOL and their recent estimates indicate that through 2004-2005 15% of all new applications (5 billion lines) will be developed in COBOL while 80% of all deployed applications will include extensions to existing legacy (usually COBOL) programs. Gartner estimates for 2002 are that there are about two million COBOL programmers world-wide compared to about about one million Java programmers and one million C++ programmers. COBOL is self-documenting One of the design goals for COBOL was to make it possible for non-programmers such as supervisors, managers and users, to read and understand COBOL code. As a result, COBOL contains such English-like structural elements as verbs, clauses, sentences, sections and divisions. As it happens, this design goal was not realized. Managers and users nowadays do not read COBOL programs. Computer programs are just too complex for most laymen to understand them, however familiar the syntactic elements. But the design goal and its effect on COBOL syntax has had one important side-effect. It has made COBOL the most readable, understandable and self-documenting programming language in use today. It has also made it the most verbose. It is easy for programmers unused to the business programming paradigm, where programming with a view to ease of maintenance is very important, to dismiss the advantage that COBOL's readability imparts. Not only does this

readability generally assist the maintenance process but the older a program gets the more valuable this readability becomes. When programs are new, both the in-program comments and the external documentation accurately reflect the program code. But over time, as more and more revisions are applied to the code, it gets out of step with the documentation until the documentation is actually a hindrance to maintenance rather than a help. The self-documenting nature of COBOL means that this problem is not as severe with COBOL programs as it is with other languages Readers who are familiar with C or C++ or Java might want to consider how difficult it becomes to maintain programs written in these languages. C programs that you have written yourself are difficult enough to understand when you come back to them six months later. Consider how much more difficult it would be to understand a program that had been written fifteen years previously, by someone else, and which had since been amended and added to by so many others that the documentation no longer accurately reflects the program code. This is a nightmare still awaiting maintenance programmers of the future COBOL is simple COBOL is a simple language (no pointers, no user defined functions, no user defined types) with a limited scope of function. It encourages a simple straightforward programming style. Curiously enough though, despite its limitations, COBOL has proven itself to be well suited to its targeted problem domain (business computing). Most COBOL programs operate in a domain where the program complexity lies in the business rules that have to be encoded rather than in the sophistication of the data structures or algorithms required. And in cases where sophisticated algorithms are required COBOL usually meets the need with an appropriate verb such as the SORT and the SEARCH. B.C.A. (Regular) SEMESTER - I Effective from 2007-08 onwards CORE-1 COBOL PROGRAMMING Subject Description: This subject deals the programming concepts using COBOL language. Goal: To learn about COBOL programming language. Objective: On successful completion of this subject the students should have skills for writing programs for business applications and file handling concepts. ----UNIT-I: Introduction to COBOL: COBOL words - Literals - Structure of COBOL Program COBOLCoding Sheet-IDENTIFICATION DIVISION- ENVIRONMENT DIVISION – DATA DIVISION –Editing and Non-editing Picture Clauses – Level Numbers – VALUE and FILLER Clause. UNIT-II: PROCEDURE DIVISION – Data Movement Verb – Arithmetic Verbs : Add, Subtract, Multiply, Divide, Compute – Input/Output Statement: Accept, Display Control Verbs: GOTO – GOTODepending on – Stop Run – CORRESPONDING Option - ROUNDED option - ON SIZE ERROR option - Simple Programs Using Above Verbs. UNIT-III: Conditional Statements: If Statement – Nested if statement – Sign Condition – Class Condition- Condition Name – Compound Condition- PERFORM Statements, More about DATA Division: RENAMES-REDEFINES – Simple Programs Using the above Verbs.

UNIT-IV: Files in COBOL: Sequential – Relative – Indexed Sequential - Random files – File description and Record description entries - Input/Output Verbs: Open, read, write, rewrite, Close, Delete – Sort Verb – Simple Programs using above Verbs. UNIT-V: Table Handling: Occurs Clause – Two and Multi-Dimensional Tables – Occurs. Indexed by Clause – SET Verb – START and SEARCH Verb – Random Files-Keys & Their Importance – INVALID KEY Clause – SCREEN SECTION - Simple Programs using above Verbs. TEXT BOOK: 1. COBOL PROGRAMMING, M.K. ROY & D.GHOSH DASTIDAR, 2nd ed, TMH. REFERENCE BOOKS: 1. COBOL programming – V. Rajaraman, PHI Pub. 2. Introduction To Cobol Programming – Dr. R. Krishnamoorthy, JJ Publications. 3. Structured COBOL – Welburn, TMH, 4th Edition.

PRACTICAL LIST 1. Write a COBOL Program to find the sum of individual digits of a 10-digit number until a single digit is produced. 2. Write a COBOL Program to accept the inputs Student Name, Marks for 5 subjects and declare the result as PASS, if the student gets minimum of 10 marks in each subject, otherwise declare as FAIL. 3. Write a COBOL Program to accept the given date (DDMMYY) and display the result in the following specified format: 030498 as 3rd APR 1998. 4. Write a COBOL Program to display the given three digit number into words using OCCURS clause. (Example: 342 as THREE HUNDRED AND FORTY TWO). 5. Write a COBOL Program to create a student data file STUDENT.DAT, using the following fields: ROLL-NO, NAME, AGE, YEAR-IN-COLLEGE, SEX, MARKS for 5 subjects. 6. Write a COBOL Program to create the following two files, using the student data file STUDENT.DAT (created by Program 5): FILE-1.DAT: List of male students who are studying third year FILE-2.DAT: List of female students who are studying first year (Use MOVE CORRESPONDING option). 7. Write a COBOL Program to sort the student data file STUDENT.DAT (created by Program 5) in the ascending order of the fields SEX, YEAR-IN-COLLEGE and ROLL-NO (use SORT verb) into SORT.DAT file. 8. Write a COBOL Program to create an Indexed Sequential File EMPLOYEE.DAT for the Employees of an Organization using the fields: ROLL-NO, NAME, DOB, SEX, BASIC-PAY and DESIGNATION.

11. 01 REC-1. PURCHASES. 05 MARK2 PIC 9(2) VALUE 2. DATA DIVISION. 02 SNO PIC 9(2) VALUE 11.DAT (created by Program 8). DATA DIVISION. P-1. 02 MARK1 PIC 9(2) VALUE 1. Write a COBOL Program to create an Inventory data file INVENT. ENVIRONMENT DIVISION. 12. PROGRAM-ID. DISPLAY(5 5) MARK2 OF REC-2.DAT (created by Program 11). Write a COBOL Program to prepare Re-Order Level Statement by using the inventory data file INVENT. IDENTIFICATION DIVISION.DAT by using the following fields: ITEM-CODE. STOP RUN. WORKING-STORAGE SECTION. PROCEDURE DIVISION. DISPLAY(3 5) MARK1 OF REC-1. . 2 PROGRAM FOR MOVE CORRESPONDING Write a simple program to demonstrate Move Corresponding. DISPLAY(1 1) ERASE. WORKING-STORAGE SECTION. 05 SNO PIC 9(2) VALUE 10. SALES. DESCRIPTION. ENVIRONMENT DIVISION. Write a COBOL Program to find the number of male employees whose BASIC-PAY > 4000 and number of female employees whose BASIC-PAY < 3000 using the employee data file EMPLOYEE. 10.9. 1 PROGRAM FOR ADD CORRESPONDING Write a simple program to demonstrate Add Corresponding. Write a COBOL Program to update the BASIC-PAY of each employee in the employee data file EMPLOYEE. if the CLOSE-STOCK is less than SAFETY-LEVEL. PROGRAM-ID.DAT (created by Program 8) by incrementing 25% of BASIC-PAY. SAFETYLEVEL and CLOSESTOCK. IDENTIFICATION DIVISION. OPEN-STOCK. ADD CORR REC-1 TO REC-2. 01 REC-2. DISPLAY (10 5) SNO OF REC-2.

IF M DISPLAY(10 5) "MARRIED". ACCEPT MS.. P-1.. EMP-OCCURS. WORKING-STORAGE SECTION. DISPLAY(1 1) ERASE. 02 SNO PIC 9(2) VALUE 11. P-1. SINGLE PERSON "... STOP RUN. IDENTIFICATION DIVISION. 05 NAME PIC X(4). 05 F PIC X(10) VALUE SPACES. DISPLAY(7 5) " 0 TO 9 . 01 MS PIC 9(2). DISPLAY(1 1) ERASE. MOVE CORR REC-1 TO REC-2. PROCEDURE DIVISION. IF S DISPLAY(10 5) "SINGLE". PROGRAM-ID. DATA DIVISION. 05 SNO PIC Z(2). PROCEDURE DIVISION. 88 S VALUE 0 THRU 9 . Get the employee details for 3 persons and calculate the total salary. PROGRAM-ID. ENVIRONMENT DIVISION. DISPLAY(5 5) "ENTER MARRIAGE STATUS : ". 01 HEAD-1 PIC X(80) VALUE ALL "-". 88 M VALUE 10 THRU 99. . 02 EMP-DET OCCURS 3 TIMES. DATA DIVISION.. 4 PROGRAM USING OCCURS CLAUSE Write a simple program to demonstrate Occurs Clause. 05 EMP-NAME PIC X(20). 02 NAME PIC X(4) VALUE "RAVI". IDENTIFICATION DIVISION. MARRIED PERSON". DISPLAY(8 5) " 10 TO 99 . DISPLAY REC-2.01 REC-1. 05 EMP-SAL PIC 9(5)V9(2). 05 F PIC X(10) VALUE SPACES. WORKING-STORAGE SECTION. ENVIRONMENT DIVISION. 01 HEAD-2. 01 EMP-REC. STOP RUN.. 3 PROGRAM FOR CONDITION NAMES Write a simple program to demonstrate Condition names usage. 01 REC-2.

02 F PIC X(15) VALUE SPACES. 02 E-SAL PIC Z(5). COMPUTE TOT-SAL = TOT-SAL + EMP-SAL(I). 01 TOT-SAL PIC 9(6)V9(2) VALUE 0.. 02 F PIC X(10) VALUE "SALARY". PERFORM DISP-PARA UNTIL I > 3. MOVE EMP-SAL(I) TO E-SAL.. ACCEPT EMP-NAME(I).Z(2). Write a program to maintain the stu-file for which a record has just 2 fields. DISPLAY HEAD-2. DISPLAY "TOTAL SALARY = " E-TOT-SAL. rno(Roll Number) and name(Student Name). DISPLAY HEAD-3. STOP RUN. IDENTIFICATION DIVISION. 02 F PIC X(20) VALUE "NAME". DISPLAY " ". DISPLAY(5 5) "NAME : ". PROCEDURE DIVISION. DISPLAY(10 5) "SALARY : ". GET-PARA. ADD 1 TO I. namely. MOVE 1 TO I. PERFORM GET-PARA 3 TIMES. MOVE EMP-NAME(I) TO E-NAME.". 02 F PIC X(15) VALUE SPACES. 01 HEAD-3. 01 I PIC 9 VALUE 1. . delete the records in the file. modify. DISPLAY HEAD-1. ADD 1 TO I. DISP-PARA. 01 E-TOT-SAL PIC Z(6). Give the provisions to add. PROGRAM-ID. ENVIRONMENT DIVISION. MOVE TOT-SAL TO E-TOT-SAL. DISPLAY(1 1) ERASE. 02 F PIC X(22) VALUE SPACES. DISPLAY HEAD-1. INPUT-OUTPUT SECTION. 5. 02 E-NAME PIC X(20). PARA-1. DISPLAY(1 1) ERASE. DISPLAY HEAD-1. ACCEPT EMP-SAL(I).02 F PIC X(15) VALUE SPACES. 02 F PIC X(20) VALUE SPACES.Z(2). 02 F PIC X(15) VALUE SPACES. DISPLAY(3 5) "ENTER DATA FOR 3 EMPLOYEES.

FILE-CONTROL. SELECT STU-FILE ASSIGN TO DISK ORGANIZATION IS INDEXED ACCESS MODE IS RANDOM RECORD KEY IS RNO FILE STATUS IS FS. DATA DIVISION. FILE SECTION. FD STU-FILE LABEL RECORDS ARE STANDARD VALUE OF FILE-ID IS "STU.DAT". 01 STU-REC. 02 RNO PIC 9(3). 02 NAME PIC X(20). WORKING-STORAGE SECTION. 01 FS PIC X(2) VALUE SPACES. 01 ANS PIC X VALUE SPACE. 01 CH PIC 9 VALUE 0. PROCEDURE DIVISION. OPEN-PARA. OPEN I-O STU-FILE. IF FS = "30" OPEN OUTPUT STU-FILE CLOSE STU-FILE OPEN I-O STU-FILE. P-1. DISPLAY(1 1) ERASE. DISPLAY(3 5) "MAIN MENU". DISPLAY(5 5) "1 TO ADD". DISPLAY(7 5) "2 TO MODIFY". DISPLAY(9 5) "3 TO DELETE". DISPLAY(11 5) "4 TO EXIT". DISPLAY(13 5) "YOUR CHOICE [1..4] : ". ACCEPT CH. IF CH < 0 OR CH > 4 GO TO P-1. GO TO ADD-PARA MODI-PARA DEL-PARA EXIT-PARA DEPENDING ON CH. ADD-PARA. DISPLAY(1 1) ERASE. DISPLAY(3 5) "RNO : ". ACCEPT RNO. DISPLAY(5 5) "NAME : ". ACCEPT NAME. WRITE STU-REC INVALID KEY DISPLAY(15 5) "ERROR!". DISPLAY(20 5) "CONTINUE ADD RECORDS [Y/N] : ". ACCEPT ANS. IF ANS = "Y" GO TO ADD-PARA ELSE GO TO P-1. MODI-PARA. DISPLAY(1 1) ERASE. DISPLAY(3 5) "ENTER ROLL NO TO MODIFY".

ACCEPT RNO. READ STU-FILE KEY IS RNO INVALID KEY DISPLAY(13 5) "NO RECORD FOUND" GO TO C-PARA. DISPLAY(5 5) "RNO = " RNO. DISPLAY(7 5) "NAME = " NAME. DISPLAY(9 5) "SURE TO MODIFY [Y/N] : ". ACCEPT ANS. IF ANS = 'Y' DISPLAY(1 1) ERASE DISPLAY(5 5) "NAME : " ACCEPT NAME REWRITE STU-REC. C-PARA. DISPLAY(15 5) "CONTINUE MODIFICATION [Y/N] : ". ACCEPT ANS. IF ANS = 'Y' GO TO MODI-PARA ELSE GO TO P-1. DEL-PARA. DISPLAY(1 1) ERASE. DISPLAY(3 5) "ENTER ROLL NO TO DELETE". ACCEPT RNO. READ STU-FILE KEY IS RNO INVALID KEY DISPLAY(13 5) "NO RECORD FOUND" GO TO C-PARA1. DISPLAY(5 5) "RNO = " R//NO. DISPLAY(7 5) "NAME = " NAME. DISPLAY(9 5) "SURE TO DELETE [Y/N] : ". ACCEPT ANS. IF ANS = 'Y' DELETE STU-FILE RECORD. C-PARA1. DISPLAY(15 5) "CONTINUE DELETION [Y/N] : ". ACCEPT ANS. IF ANS = 'Y' GO TO DEL-PARA ELSE GO TO P-1. EXIT-PARA. CLOSE STU-FILE. 6. Write a program to maintain the stu-file for which a record has just 2 fields, namely, rno(Roll Number) and name(Student Name). Give the provisions to add, modify, delete the records in the file. IDENTIFICATION DIVISION. PROGRAM-ID. ENVIRONMENT DIVISION. INPUT-OUTPUT SECTION. FILE-CONTROL. SELECT STU-FILE ASSIGN TO DISK ORGANIZATION IS INDEXED ACCESS MODE IS RANDOM RECORD KEY IS RNO FILE STATUS IS FS.

DATA DIVISION. FILE SECTION. FD STU-FILE LABEL RECORDS ARE STANDARD VALUE OF FILE-ID IS "STU.DAT". 01 STU-REC. 02 RNO PIC 9(3). 02 NAME PIC X(20). WORKING-STORAGE SECTION. 01 FS PIC X(2) VALUE SPACES. 01 ANS PIC X VALUE SPACE. 01 CH PIC 9 VALUE 0. PROCEDURE DIVISION. OPEN-PARA. OPEN I-O STU-FILE. IF FS = "30" OPEN OUTPUT STU-FILE CLOSE STU-FILE OPEN I-O STU-FILE. P-1. DISPLAY(1 1) ERASE. DISPLAY(3 5) "MAIN MENU". DISPLAY(5 5) "1 TO ADD". DISPLAY(7 5) "2 TO MODIFY". DISPLAY(9 5) "3 TO DELETE". DISPLAY(11 5) "4 TO EXIT". DISPLAY(13 5) "YOUR CHOICE [1..4] : ". ACCEPT CH. IF CH < 0 OR CH > 4 GO TO P-1. GO TO ADD-PARA MODI-PARA DEL-PARA EXIT-PARA DEPENDING ON CH. ADD-PARA. DISPLAY(1 1) ERASE. DISPLAY(3 5) "RNO : ". ACCEPT RNO. DISPLAY(5 5) "NAME : ". ACCEPT NAME. WRITE STU-REC INVALID KEY DISPLAY(15 5) "ERROR!". DISPLAY(20 5) "CONTINUE ADD RECORDS [Y/N] : ". ACCEPT ANS. IF ANS = "Y" GO TO ADD-PARA ELSE GO TO P-1. MODI-PARA. DISPLAY(1 1) ERASE. DISPLAY(3 5) "ENTER ROLL NO TO MODIFY". ACCEPT RNO. READ STU-FILE KEY IS RNO INVALID KEY DISPLAY(13 5) "NO RECORD FOUND" GO TO C-PARA. DISPLAY(5 5) "RNO = " RNO. DISPLAY(7 5) "NAME = " NAME.

CLOSE STU-FILE. IF ANS = 'Y' GO TO MODI-PARA ELSE GO TO P-1. ACCEPT ANS. INPUT-OUTPUT SECTION. IF ANS = 'Y' DISPLAY(1 1) ERASE DISPLAY(5 5) "NAME : " ACCEPT NAME REWRITE STU-REC. IF ANS = 'Y' DELETE STU-FILE RECORD. EXIT-PARA. ENVIRONMENT DIVISION. DISPLAY(1 1) ERASE. SELECT BANKFILE ASSIGN TO DISK ORGANIZATION IS LINE SEQUENTIAL. DEL-PARA. C-PARA. ACCEPT RNO. ACCEPT ANS. ACCEPT ANS. DISPLAY(3 5) "ENTER ROLL NO TO DELETE". FILE-CONTROL. 7 PROGRAM TO FIND INTEREST ON BANK DEPOSITS Write a program to find interest on deposits The criteria is given below : ______________________________________________ Principal Years of Deposit IntRate ______________________________________________ >=5000 >= 3 10% >=5000 < 3 8% <5000 Any 7% ______________________________________________ IDENTIFICATION DIVISION. ACCEPT ANS. C-PARA1. IF ANS = 'Y' GO TO DEL-PARA ELSE GO TO P-1. STOP RUN. READ STU-FILE KEY IS RNO INVALID KEY DISPLAY(13 5) "NO RECORD FOUND" GO TO C-PARA1. DISPLAY(15 5) "CONTINUE DELETION [Y/N] : ". FILE SECTION. DISPLAY(15 5) "CONTINUE MODIFICATION [Y/N] : ". DISPLAY(5 5) "RNO = " RNO. DATA DIVISION. DISPLAY(7 5) "NAME = " NAME.DISPLAY(9 5) "SURE TO MODIFY [Y/N] : ". FD BANKFILE . PROGRAM-ID. DISPLAY(9 5) "SURE TO DELETE [Y/N] : ".

02 F PIC X(5) VALUE SPACES. 02 E-N PIC Z(2). 02 DNAME PIC X(21). 02 F PIC X(8) VALUE "DEPS NO". 02 F PIC X(5) VALUE SPACES. READ-PARA. 02 F PIC X(5) VALUE SPACES. 02 F PIC X(3) VALUE SPACES. PERFORM G-W-PARA UNTIL ANS = 'N' OR 'N'. OPEN INPUT BANKFILE. DISPLAY HEAD-1.Z(2). WORKING-STORAGE SECTION. 01 INT PIC 9(6)V9(2) VALUE 0. 02 E-TOT PIC Z(6). 02 E-DNO PIC Z(5). 02 E-P PIC Z(4). DISPLAY HEAD-2. 02 F PIC X(3) VALUE SPACES. 02 F PIC X(5) VALUE SPACES. 02 F PIC X(8) VALUE "DEPOSIT".Z(2). P-1. 02 N PIC 9(2). DISPLAY HEAD-1. 02 F PIC X(10) VALUE " NETT". 02 F PIC X(5) VALUE SPACES. 02 F PIC X(8) VALUE "PERIOD".LABEL RECORDS ARE STANDARD VALUE OF FILE-ID IS "BANK. 01 ANS PIC X VALUE SPACE. 01 BANKREC. 02 F PIC X(8) VALUE "INTEREST". 01 HEAD-1 PIC X(80) VALUE ALL '-'. 01 HEAD-3. 02 F PIC X(5) VALUE "RATE". DISPLAY(1 1) ERASE. 02 F PIC X(3) VALUE SPACES. 01 KEY-IN PIC X VALUE SPACE.DAT". 01 R PIC 9(2) VALUE 0. 02 P PIC 9(4)V9(2). CLOSE BANKFILE.Z(2). 02 DNO PIC 9(5). 02 E-DNAME PIC X(26). 01 TOT PIC 9(6)V9(2) VALUE 0. 02 F PIC X(5) VALUE SPACES. 01 HEAD-2. . 02 E-R PIC Z(2). 02 E-INT PIC Z(4). OPEN OUTPUT BANKFILE. PROCEDURE DIVISION. 02 F PIC X(10) VALUE "DEPS NAME".

MOVE N TO E-N. DISPLAY(9 5) "YEARS : ". Tranfile has 3 fileds tpno. DISPLAY(5 5) "DEP NAME : ". G-W-PARA. INPUT-OUTPUT SECTION.trcode. MOVE INT TO E-INT. DISPLAY(1 1) ERASE. CLOSE-PARA. GO TO READ-PARA. CLOSE BANKFILE. IF ( P NOT < 5000 AND N < 3) MOVE 8 TO R COMPUTE TOT = P * (1 + R / 100) ** N. SELECT INVFILE ASSIGN TO DISK ORGANIZATION IS INDEXED . FILE-CONTROL. By reading tranfile records if trcode=1 then update the qty with qty+tqty in invfile. ACCEPT DNO.READ BANKFILE AT END GO TO CLOSE-PARA. COMPUTE INT = TOT .name. DISPLAY HEAD-3. IF ( P NOT < 5000 AND N NOT < 3 ) MOVE 10 TO R COMPUTE TOT = P * (1 + R / 100) ** N. MOVE DNAME TO E-DNAME. MOVE DNO TO E-DNO. MOVE P TO E-P. MOVE R TO E-R. IDENTIFICATION DIVISION. WRITE BANKREC.P. IF ( P < 5000) MOVE 7 TO R COMPUTE TOT = P * (1 + R / 100) ** N. DISPLAY(15 5) "ADD MORE [Y/N] : ". If trcode=2 then update the qty with qty-tqty in invfile.qty.namely. 8 PROGRAM FOR INVENTORY Write a program to update the inventory file. DISPLAY(3 5) "DEP NO : ". invfile and tranfile. ACCEPT N. PROGRAM-ID. Consider 2 files. ACCEPT DNAME. ACCEPT ANS. Invfile has 3 fields pno. MOVE TOT TO E-TOT. DISPLAY HEAD-1. ENVIRONMENT DIVISION. STOP RUN. ACCEPT P.tqty. DISPLAY(7 5) "AMOUNT : ".

CLOSE TRANFILE. 02 TPNO PIC 9(5). OPEN OUTPUT TRANFILE. OPEN I-O INVFILE. DATA DIVISION.DAT". SELECT TRANFILE ASSIGN TO DISK ORGANIZATION IS LINE SEQUENTIAL. 02 TQTY PIC 9(5). 01 FS PIC X(2) VALUE SPACES. PERFORM G-W-TRAN UNTIL ANS = 'N' OR 'N'. 01 TRANREC. IF TRCODE = 1 ADD TQTY TO QTY REWRITE INVREC. READ TRANFILE AT END GO TO CLOSE-PARA. 02 QTY PIC 9(5).ACCESS MODE IS DYNAMIC RECORD KEY IS PNO FILE STATUS IS FS. READ INVFILE KEY IS PNO INVALID KEY DISPLAY(5 5) "NO RECORD FOUND FOR" . 02 PNO PIC 9(5). P-1. 01 INVREC. READ-PARA. PERFORM G-W-INV UNTIL ANS = 'N' OR 'N'. FD TRANFILE LABEL RECORDS ARE STANDARD VALUE OF FILE-ID IS "TRAN. OPEN INPUT TRANFILE. FILE SECTION.DAT". 01 KEY-IN PIC X VALUE SPACE. IF FS = "30" OPEN OUTPUT INVFILE CLOSE INVFILE OPEN I-O INVFILE. FD INVFILE LABEL RECORDS ARE STANDARD VALUE OF FILE-ID IS "INV. 01 ANS PIC X VALUE SPACE. PROCEDURE DIVISION. 02 NAME PIC X(5). 02 TRCODE PIC 9. IF TRCODE = 2 SUBTRACT TQTY FROM QTY . WORKING-STORAGE SECTION. PNO ACCEPT KEY-IN GO TO READ-PARA. MOVE TPNO TO PNO. MOVE SPACE TO ANS.

ACCEPT KEY-IN. OPEN INPUT INVFILE. CLOSE INVFILE. DISPLAY(7 5) "QUANTITY : ". DISPLAY(12 5) "ADD MORE [Y/N] :". ACCEPT TQTY. ACCEPT QTY. WRITE TRANREC. DISPLAY(7 5) "QTY : " QTY. READ INVFILE NEXT RECORD AT END GO TO CL-PARA. ACCEPT TPNO. DISPLAY(3 5) "PART NO: " PNO. STOP RUN. WRITE INVREC INVALID KEY DISPLAY(10 5) "REC EXISTS!". ACCEPT TRCODE. DISPLAY(3 5) "PART NO: ".. DISPLAY(3 5) "PART NO: ". ACCEPT NAME. CL-PARA. DISPLAY(1 1) ERASE.". GO TO READ-PARA. DISPLAY(5 5) "TRANCODE : ". DISPLAY(5 5) "NAME : " NAME. ACCEPT ANS. DISPLAY(2 5) "INVEN DETAILS. CLOSE-PARA. DISPLAY(5 5) "NAME : ". CLOSE INVFILE TRANFILE... DISPLAY(7 5) "QUANTITY : ". ACCEPT PNO. ACCEPT ANS. G-W-INV. DISPLAY(1 1) ERASE.._ 9 PROGRAM TO REWRITE PAY RATE Write a program to modify the hourly-pay-rate based on the criteria given below: ______________________________________ Existing Hpr Increase in Hpr in Rs) ______________________________________ <=5 25% >5 but <= 20% >8 but <=12 15% >12 10% . OP-PARA.. GO TO R-PARA.REWRITE INVREC. DISPLAY(2 5) "TRAN DETAILS . DISPLAY(12 5) "ADD MORE [Y/N] :". DISPLAY(1 1) ERASE. G-W-TRAN.".. R-PARA.

IF ( HPR > 8 AND HPR NOT GREATER 12 ) . PROCEDURE DIVISION. WORKING-STORAGE SECTION. ENVIRONMENT DIVISION. 02 F PIC X(5) VALUE SPACES. PROGRAM-ID. 01 PAYREC. 02 F PIC X(5) VALUE "NAME". 02 F PIC X(12) VALUE SPACES. IF ( HPR > 5 AND HPR NOT GREATER 8 ) COMPUTE HPR = HPR + HPR * 0. 02 IDNO PIC X(3)9(6). SELECT PAYFILE ASSIGN TO DISK. FD PAYFILE LABEL RECORDS ARE STANDARD VALUE OF FILE-ID IS "PAY. 01 HEAD-2. FILE SECTION. INPUT-OUTPUT SECTION. CLOSE PAYFILE. 02 F PIC X(36) VALUE "MIS INFO". 02 F PIC X(5) VALUE "IDNO".25. 02 E-HPR PIC Z(2). 02 F PIC X(5) VALUE SPACES. 01 ANS PIC X VALUE SPACE. 02 E-IDNO PIC X(3)9(6). 02 F PIC X(5) VALUE SPACES. 02 F PIC X(7) VALUE "NEWHPR". 02 MIS PIC X(42). OPEN EXTEND PAYFILE. 02 HPR PIC 99V99.DAT". DISPLAY(3 5) "HPR = " HPR. 01 HEAD-1 PIC X(80) VALUE ALL '-'. 02 NAME PIC X(25). ACCEPT ANS. READ-PARA. DISPLAY(1 1) ERASE. 01 HEAD-3. P-1. 02 E-NAME PIC X(25). FILE-CONTROL.---------------------------------------------------------IDENTIFICATION DIVISION.20.Z(2). OPEN I-O PAYFILE. DATA DIVISION. READ PAYFILE AT END GO TO CLOSE-PARA. 02 E-MIS PIC X(41). PERFORM G-W-PARA UNTIL ANS = 'N' OR 'N'. IF ( HPR NOT GREATER 5 ) COMPUTE HPR = HPR + HPR * 0.

ACCEPT HPR. CLOSE-PARA. MOVE NAME TO E-NAME.COMPUTE HPR = HPR + HPR * 0. CLOSE PAYFILE.30 0.50 0. STOP RUN. DISPLAY(7 5) " HRLY PAY RATE : ". DISPLAY HEAD-1. IF ( HPR > 12 ) COMPUTE HPR = HPR + HPR * 0. DISPLAY(5 5) "EMP NAME :". ACCEPT NAME. DISPLAY HEAD-1.3 & 4. REWRITE PAYREC. MOVE MIS TO E-MIS.15.2. DISPLAY HEAD-1. GO TO READ-PARA. DISPLAY HEAD-2.50 0. GO TO READ-PARA-1. READ-PARA-1. READ PAYFILE AT END GO TO CLOSE-PARA-1. DISPLAY(1 1) ERASE. DISPLAY(3 5) "ID NO [ X(3) 9(6)] : ".40 0.10. WRITE PAYREC.The rate schedule for customers is shown in one table as -----------------------------------------------------------------------------Customer code 1 2 2 3 3 4 Consumption All Below 1000& Below 5000& All 1000 above 5000 above Rate/unit 0. OPEN INPUT PAYFILE. ACCEPT ANS. .30 ----------------------------------------------------------------------------The customer record is: col:1-5 customer no. CLOSE PAYFILE. DISPLAY HEAD-1. 10 PROGRAM FOR ELECTRICITY BILL An electricity company supplies electricity to 4 types of customers coded 1. ACCEPT MIS. DISPLAY HEAD-3.50 0. MOVE 0 TO HPR. DISPLAY(15 5) "ADD MORE (Y/N) :". CLOSE-PARA-1. MOVE IDNO TO E-IDNO. DISPLAY(9 5) "MISCELL INFO : ". MOVE HPR TO E-HPR. DISP-PARA. ACCEPT IDNO. G-W-PARA.

02 F PIC X(6) VALUE "CSCODE". 01 HEAD-3. INPUT-OUTPUT SECTION. 01 HEAD-1 PIC X(80) VALUE ALL '-'. PROGRAM-ID. SELECT EFILE ASSIGN TO DISK ORGANIZATION IS LINE SEQUENTIAL. PERFORM G-W-PARA UNTIL ANS = 'N' OR 'N'. 01 ANS PIC X VALUE SPACE. 02 F PIC X(11) VALUE SPACES. OPEN INPUT EFILE. 01 AMT PIC 9(5)V9(2) VALUE 0. DISPLAY HEAD-1.Z(2). P-1. 02 F PIC X(5) VALUE SPACES. 02 CCODE PIC 9. 02 UNITS PIC 9(7). . DATA DIVISION.DAT". ENVIRONMENT DIVISION. FILE SECTION. FILE-CONTROL. 02 F PIC X(5) VALUE "UNITS". CLOSE EFILE. 02 BLANK SCREEN. 02 F PIC X(11) VALUE SPACES. 02 E-UNITS PIC Z(7). DISPLAY HEAD-2. SCREEN SECTION. OPEN EXTEND EFILE. PROCEDURE DIVISION. 02 F PIC X(11) VALUE SPACES. 02 E-CCODE PIC Z. col:68 customer code. 01 HEAD-2. 01 EREC. 02 F PIC X(5) VALUE SPACES. WORKING-STORAGE SECTION. 02 F PIC X(10) VALUE "AMOUNT". IDENTIFICATION DIVISION. col:61-67 consumption in units. 02 F PIC X(10) VALUE SPACES. 02 F PIC X(11) VALUE SPACES. Read a customer record and print a bill using GOTO DEPENDING ON statement.col:6-60 name & address. FD EFILE LABEL RECORDS ARE STANDARD VALUE OF FILE-ID IS "E. 02 E-AMT PIC Z(5). DISPLAY HEAD-1. 01 B-SCREEN.

C4. G-W-PARA. DISPLAY HEAD-1.40 GO TO M-PARA.30 GO TO M-PARA ELSE COMPUTE AMT = (UNITS . Fields Columns -----------------Serial No. MOVE UNITS TO E-UNITS. DISPLAY HEAD-3. ACCEPT ANS. GO TO C1 C2 C3 C4 DEPENDING ON CCODE.50 GO TO M-PARA. STOP RUN. M-PARA.40 GO TO M-PARA. COMPUTE AMT = UNITS * 0. CLOSE EFILE. ACCEPT UNITS. DISPLAY(10 5) "CONTINUE [Y/N] : ".READ-PARA.4999) * 0.999) * 0.5 + 4999 * 0. 11 PROGRAM TO CREATE 2 FILES USING AN EXISTING FILE A file contains the following records about a class. IF (UNITS < 5000) COMPUTE AMT = UNITS * 0. C1. DISPLAY(3 5) "CUS CODE: ". DISPLAY (5 5) "UNITS : ". IF ( UNITS < 1000) COMPUTE AMT = UNITS * 0.5 + 999 * 0. IF (UNITS NOT < 1000 ) COMPUTE AMT = (UNITS .30. COMPUTE AMT = UNITS * 0. CLOSE-PARA. GO TO READ-PARA. 5-10 Name 11-30 Age 31-32 Sex 33 . ACCEPT CCODE. 1-4 Roll No. DISPLAY B-SCREEN. READ EFILE AT END GO TO CLOSE-PARA. C3. MOVE CCODE TO E-CCODE. MOVE AMT TO E-AMT. C2.30 GO TO M-PARA. WRITE EREC.

ENVIRONMENT DIVISION. OPEN OUTPUT COLLFILE. 02 NAME PIC X(20). . 01 MALEREC PIC X(80). 88 MALE VALUE 1.Year in college 34 Select records with the following characteristics and write them in 2 files. FD FEMALEFILE LABEL RECORDS ARE STANDARD VALUE OF FILE-ID IS "FEMALE. 02 SEX PIC 9. SELECT FEMALEFILE ASSIGN TO DISK ORGANIZATION IS LINE SEQUENTIAL. READ-PARA. OPEN INPUT COLLFILE OUTPUT MALEFILE FEMALEFILE. 88 FOURTH VALUE IS 4. FILE SECTION.DAT". WORKING-STORAGE SECTION. 88 FEMALE VALUE IS 2. SELECT COLLFILE ASSIGN TO DISK ORGANIZATION IS LINE SEQUENTIAL. Use condition names for sex and year in college. CLOSE COLLFILE. DATA DIVISION. PERFORM G-W-PARA UNTIL ANS = 'N' OR 'N'.DAT". 02 SNO PIC 9(4). 01 FEMALEREC PIC X(80). 01 ANS PIC X VALUE SPACE. 02 RNO PIC 9(6). FD MALEFILE LABEL RECORDS ARE STANDARD VALUE OF FILE-ID IS "MALE. PROCEDURE DIVISION. 02 YR PIC 9. DISPLAY(1 1) ERASE. INPUT-OUTPUT SECTION. FILE-CONTROL. 01 COLLREC.DAT". File-2: Records of all females under 19 years of age in the fourth year in the college. P-1. File-1: Records of all males over 18 years of age who are in the third year in the college. IDENTIFICATION DIVISION. PROGRAM-ID. FD COLLFILE LABEL RECORDS ARE STANDARD VALUE OF FILE-ID IS "COLL. SELECT MALEFILE ASSIGN TO DISK ORGANIZATION IS LINE SEQUENTIAL. 88 THIRD VALUE 3. 02 AGE PIC 9(2).

Information Management: The File System . ACCEPT SNO.READ COLLFILE AT END GO TO CLOSE-PARA. G-W-PARA. DISPLAY(11 5) "SEX (1 . GO TO READ-PARA. ACCEPT RNO. DISPLAY(9 5) "AGE : ". CLOSE COLLFILE MALEFILE FEMALEFILE. ACCEPT NAME. DISPLAY(5 5) "RNO: ". ACCEPT SEX. ACCEPT ANS. DISPLAY(7 5) "NAME: ".THIRD. DISPLAY(3 5) "SNO : ". IF (MALE AND AGE > 18 AND THIRD) MOVE COLLREC TO MALEREC WRITE MALEREC. CLOSE-PARA.Device Driver. OPERATING SYSTEMS UNIT-I: Operating System Overview: Operating System Objectives and Functions – The Evolution of Operating Systems – Major Achievements – Developments Leading to Modern Operating Systems – Microsoft Windows Overview – Traditional UNIX Systems – Modern UNIX Systems – Linux 95. IF(FEMALE AND AGE < 19 AND FOURTH) MOVE COLLREC TO FEMALEREC WRITE FEMALEREC.MALE . STOP RUN. UNIT-II: OS-Functions and Structure: Different Services of Operating System – Operating System Structure – Booting. 2 -FEMALE) : ". UNIT-IV: Memory Management: Introduction – Single Contiguous Memory Management – Fixed . UNIT-III: Process Management: What Is A Process? – Evolution of Multiprogramming – Context Switching – Process States – Process State Transitions – Operations on a Process. 4 -FOURTH : ". DISPLAY(13 5) "YEAR (3 . ACCEPT YR. DISPLAY(20 5) "ADD MORE(Y/N) :". DISPLAY(1 1) ERASE. ACCEPT AGE. WRITE COLLREC.

Two recent challengers though. Modula.1-14. 3. 2nd edition. Greg Gagne.9 UNIT-IV: 8. TMH. For instance you would not develop an operating system or a compiler using COBOL.2. 7th Edition. COBOL is designed for developing business. 4. All these languages have found a niche but none has yet displaced COBOL.1-8. PHI. Ada.Partition Memory Management – Variable Partitions – Non-contiguous Allocation – Paging – Segmentation – Combined Systems – Virtual Memory Management Systems. OPERATING SYSTEM PRINCIPLES – Abraham Silberschatz. 2. OPERATING SYSTEMS Internals and Design Principles – William Stallings. responsibility for developing new COBOL standards has been assumed by the American National Standards Institute (ANSI). Gartner estimates for 2002 are that there are about two million COBOL programmers world-wide compared to about about one million Java programmers and one million C++ programmers.8 UNIT-V: 14.6. MODERN OPERATIING SYSTEMS – Andrew S. applications. so just think how old it is in computer years.9) REFERENCE BOOKS: 1. (UNIT-I: 2. In that time it has seen off the challenges of a number of other languages such as PL1. In 1999 they reported that over 50% of all new mission-critical applications were still being done in COBOL and their recent estimates indicate that through 2004-2005 15% of all new applications (5 billion lines) will be developed in COBOL while 80% of all deployed applications will include extensions to existing legacy (usually COBOL) programs. As the expanded acronym indicates. 4. are proving to be serious contenders.Remote Procedure Calls – Clusters –Windows Cluster – Sun Cluster – Beowulf And Linux Clusters. 5th edition. A new COBOL standard introducing object-oriented programming to COBOL. Of that they estimated that about 80% (240 billion lines) were in COBOL and 20% (60 billion lines) were written in all the other computer languages combined [Brown]. Tanenbaum.3 UNIT-III: 5. Client/Server and Clusters: Client/Server Computing – Distributed Message Passing. TEXTBOOKS: 1. It is not designed for writing systems programs. UNIT-V: Distributed Processing. Algol68. Wiley India. For over four decades COBOL has been the dominant programming language in the business computing domain. OPERATING SYSTEMS – Achyut Godbole . Introduction COBOL is about as old as your parents. 2nd edition. COBOL COBOL is a high-level programming language first developed by the CODASYL Committee (Conference on Data Systems Languages) in 1960. typically file-oriented. OPERATING SYSTEMS Concepts and Design – Milan Milankovic. Three ANSI standards for COBOL have been produced: in 1968. C. mainly because there are thousands and thousands of applications out there .7. Java and Visual Basic. TMH. It is still an important language though. 3.2-5. is due within the next few years. Peter Baer Galvin. COBOL's dominance in underlined by the reports from the Gartner group.1-2. (UNIT II: 3.2.9. In 1997 they estimated that there were about 300 billion lines of computer code in use in the world. 3. Since then. The word COBOL is an acronym that stands for COmmon Business Oriented Language.7) 2. 2nd edition. 1974 and 1985. C++. 5. Pascal.PHI.

You can also enter when and by whom it was written. There aren't as many jobs for COBOL as for C++ or JAVA. ENVIRONMENT DIVISION : This is where you define the files the program needs. STOP RUN This is quite a bit more complex than a similar program an Pascal or C The first thing to notice are the four divisions : IDENTIFICATION DIVISION : This is where you must enter the program name.written in COBOL. Each statement begins with a word like DISPLAY that describes the action to be performed by the statement. It is similar to the top of a Pascal program. A paragraph is a series of statements named by a label (FIRST-PARA in this case). not a lot of people are learning COBOL anymore. but then again. PROCEDURE DIVISION : This is where you write the program. There aren't many data-structures (actually. records. . There are a couple of hundred reserved words in COBOL so I find it useful to put hyphens in user defined names like HELLO-WORLD. DISPLAY "Hello World". so here it is: IDENTIFICATION DIVISION PROGRAM-ID HELLO-WORLD ENVIRONMENT DIVISION DATA DIVISION PROCEDURE DIVISION FIRST-PARA. You also used to put hardware information here. COBOL really isn't that difficult either. everything is really a record). DATA DIVISION : This is where you declare variables. You can easily learn enough to sound intelligent in a day. The Basics A "hello world" program is as good a way as any to understand the structure of a COBOL program. There is no specific mark to the end of a paragraph. and there are no notions of scope or parameter passing. but that isn't really necessary anymore. In the sample above this is all in one paragraph. files etc. and someone has got to fix them and add to them. especially if you know other structured languages like Pascal and C.

Screen Output Screen output is always one of the first things you want to master with a new language. Data Declaration The HELLO-WORLD program can be altered to demonstrate basic data declaration. To find out more about using floating-point and signed numbers. . In this program two string variables and one number variable are created and given initial values. These are then printed out. IDENTIFICATION DIVISION PROGRAM-ID HELLO-WORLD ENVIRONMENT DIVISION DATA DIVISION WORKING-STORAGE SECTION. You can also use it to write digits: DISPLAY 300. The problem with number is that this technique only works for unsigned integers. statement. see the Arithmetic Operations section. You have seen an example of screen output with the DISPLAY "Hello World". Or a combination: DISPLAY "The number is " 300.32". 01 WS_STRING-1 PIC X(20) VALUE "Hello World". See the Assigning Values to Variables section. a way around this is as follows: DISPLAY "-766. 01 WS-STRING-2 PIC X(30) VALUE "This is program number ". Also note that you can't make statements like : DISPLAY 2 + 3 You have to store the result of 2 + 3 in a variable first.

We can use the variable WE-PROGRAM-NUMBER from above to store a new program number: DISPLAY "Enter the program number".the need for these becomes apparent when we deal with records. There is a more advanced type of assignment that MOVE can't help us with. We can give WS-STRING-1 a new value like this : MOVE "Hello Everyone" TO WS-STRING-1. and then a PIC clause. DISPLAY WS-PROGRAM-NUMBER. DISPLAY WS-STRING-2.01 WS-PROGRAM-NUMBER PIC 9(2) VALUE 1. The number following in brackets tells the size (for X(4) you could also write XXXX). This is followed by the data name. The 01 before each variable is its level number . PROCEDURE DIVISION FIRST-PARA. '9' represents a number. Keyboard Input Keyboard input is simple. STOP RUN The WORKING-STORAGE SECTION is used for declaring variables other than files and associated records. . ACCEPT WS-PROGRAM-NUMBER. Continuing with our variable WS-PROGRAM-NUMBER from the Data Declaration section. The PIC clause establishes the type and size of the variable. we can assign a new value for it with the following structure in the PROCEDURE DIVISION section : MOVE 5 TO WS-PROGRAM-NUMBER. This can only be used to obtain a single variable at a time. Assigning Values to Variables The two main keywords used for this are MOVE and COMPUTE. we can write the following in Pascal : numA := numB * numC. DISPLAY WS-STRING-1. 'X' represents a character. Given correctly declared variables.

MULTIPLY WS-NUMBER-1 BY WS-NUMBER-2 GIVING WS-NUMBER-3. These have been given values. With division. so be careful. COBOL won't check to see if the new value is appropriate for the variable in either size or type. COBOL has very little built in error checking. There are also a collection of keywords that deal with arithmetic. Remember to be careful when assigning values to variables. numbers can be rounded up like this : COMPUTE WS-NUMBER-1 ROUNDED = 37/10. it is worth pointing out here. Floating point numbers are declared like this in DATA DIVISION : WS-NUMBER-10 PIC 99V99. Bare in mind that there is no mod operator available.The value of numA becomes the value of the expression numB * numC. To do this in Pascal we need to use the COMPUTE keyword. We could now give it the value -32. Given the right declarations statements like these sometimes occur : ADD WS-NUMBER-1 TO WS-NUMBER-2. We can do this with the following code in the PROCEDURE DIVISION section : COMPUTE WS-PROGRAM-NUMBER = WS-NUMBER-1 * WS-NUMBER-2. COBOL can also handle floating point numbers. Simply put an S at the front : WS-NUMBER-10 PIC S99V99. A confusing aspect of this is that to enter a number like this with ASSIGN the user would have to enter 2398. Suppose we have created two number variables in the DATA DIVISION section called WS-NUMBER-1 and WS-NUMBER-2. This number can hold values with two leading and two trailing spaces like 23. Arithmetic Operations The COMPUTE keyword above introduced arithmetic. . and division is done with /. and we want to multiply them together and store the result in WS-PROGRAM-NUMBER. You could also just use numbers : COMPUTE WS-PROGRAM-NUMBER = 10 * 5. In fact.76. COBOL can also deal with signed numbers.98.

.DIVIDE WS-NUMBER-1 INTO WS-NUMBER-2 GIVING WS-NUMBER-3 REMAINDER WSNUMBER-4.... SUBTRACT WS-NUMBER-1 FROM WS-NUMBER-2 GIVING WS-NUMBER-3.... PARA-TWO. This is all very silly....... PERFORM PARA-THREE.... DISPLAY "Paragraph two has been executed"... but COBOL is an old language...... DISPLAY "Paragraph three has been executed" ...... PARA-ONE.... ..... There is an additional clause you may add to the end : ADD WS-NUMBER-1 TO WS-NUMBER-2 ON SIZE ERROR DISPLAY "Size error" NOT SIZE ERROR DISPLAY WS-NUMBER-1 END-ADD Procedural Abstraction Just like Pascal and C........ This is what the PROCEDURE DIVISION portion of a program will resemble when the PERFORM keyword is used to split the code into separate procedures. In this example the procedures (with names like PARA-ONE) call each other in turn : CONTROL-PARA... ... STOP RUN... the GIVING clause is optional... PERFORM PARA-TWO. COBOL allows you to split your code up into small procedures..... ...... As you can see..

GO TO SEC-EXIT... Control Structures : Selection COBOL has fewer structures for controlling program flow than other languages like C.. Make sure the last statement in the first paragraph is STOP RUN or you will find yourself in a lot of trouble.. but it does the job. This is the equivalent of STOP RUN.... these aren't really procedures in the Pascal sense. so in effect a section is like a sub-program.. Also. the code to be executed when the program begins is before the other procedures.. .. Sections A whole group of paragraphs can be combined in a larger structure called a SECTION... and doesn't return to the rest of the program until GO TO is executed.. A section can then be called with PERFORM... SEC-CONTROL... They can be called with PERFORM followed by their name.............. Control Structures rely on conditionals to evaluate between alternatives... and nothing explicitly marks the end of them. SEC-EXIT..... Note that the paragraph names are arbitrary.PARA-THREE... PERFORM SEC-ONE-ONE... . In case you were wondering... ... SEC-ONE SECTION. Control is then passed to the first paragraph in SECTION-ONE..everything is global in COBOL. COBOL includes the following : .. You cannot pass them parameters.. EXIT.... unlike in Pascal.. DISPLAY "This is the last paragraph" . This could then be called by : PERFORM SECTION-ONE.. and they can't create their own independent variables ....... SEC-ONE-ONE........

. GREATER. There is also the equivalent of the Pascal Case statement... ELSE DISPLAY "Not greater than 100" END IF. just remember to put the right number of END IF statements in.. We can also add an ELSE clause : IF WS-NUMBER-1 > 100 THEN DISPLAY "Greater than 100". You can also nest selection structures inside other selection structures.<. LESS OR EQUAL. NOT EQUAL There are also the three main logical operators : AND. END IF. =.. This one evaluates WS-NUMBER-1. NOT= These are self explanatory. and calls a different procedure for each possible value : ACCEPT WS-NUMBER-1.. >=. OR Selection statements can now be made as follows : IF WS-NUMBER-1 > 100 THEN DISPLAY "Greater than 100"... GREATER OR EQUAL. <=. EQUAL.. NOT.. >. EVALUATE WS-NUMBER-1 WHEN "1" PERFORM PARA-ONE WHEN "2" PERFORM PARA-TWO .. and they also have word form equivalents: LESS.

. Control Structures : Iteration The Pascal While statement can be performed like this : ACCEPT WS-NUMBER-1 PERFORM UNTIL WS-NUMBER-1 > 100 AND WS-NUMBER-1 < 1000 DISPLAY "Still between 100 and 1000" ACCEPT WS-NUMBER-1 END-PERFORM The Pascal For statement is implemented like this : PERFORM TEST AFTER VARYING WS-INDEX FROM 1 BY 1 UNTIL WS-INDEX = 10 DISPLAY "WS-INDEX is now equal to " WS-INDEX END-PERFORM This is more complex than Pascal.. Records A record is a set of related information grouped together. and goes until 10. They are extremely common in COBOL applications. but it is still pretty obvious what is going on . NUMERIC So we can have statements like IF WS-NUMBER-1 NUMERIC ..it starts at 1. ALPHABETIC-LOWER.WHEN "3" PERFORM PARA-THREE WHEN OTHER PERFORM PARA-FOUR END-EVALUATE You can also test to see if a piece of data is one of four class types : ALPHABETIC. ALPHABETIC-UPPER.. Here is how one might be declared in COBOL to hold information about a person : DATA DIVISION .. increments by 1.

though. Firstly. we can initialize it as empty like this : MOVE SPACES TO WS-PERSON-RECORD We can now put some values into the elements : MOVE 22 TO WS-AGE MOVE "Dane" TO WS-NAME MOVE "New Zealand" TO WS-ADDRESS MOVE WS-ADDRESS TO WS-STRING DISPLAY WS-STRING. The others are the elementary items.this is the group item.WORKING STORAGE SECTION 01 WS-PERSON-RECORD 03 WS-NAME PIC X(20) 03 WS-ADDRESS PIC X(40) 03 WS-AGE 01 WS-STRING PIC 9(2) PIC X(80) Note that the top line has no PIC clause . arithmetic arguments can't be applied to it. This is also possible : MOVE WS-PERSON-RECORD TO WS-STRING DISPLAY WS-STRING The data in a record is alphanumeric. You can also nest records inside records : 01 WS-PERSON-RECORD 03 WS-NAME 03 WS-ADDRESS 05 WS-STREET 05 WS-CITY PIC X(20) PIC X(20) PIC X(20) . Even if it is a number. We can now use this is various ways.

03. you would also have to change the value of 03. 05 is simple. Each file must also be defined in the DATA DIVISION section: DATA DIVISION. 03. Notice that you can directly reference a field. Each file that is going to be used must have its own SELECT division. FILE-CONTROL. SELECT DATA-FILE-1 ASSIGN TO DEVICE-NAME ORGANIZATION IS SEQUENTIAL ACCESS MODE IS SEQUENTIAL Actually. FD DATA-FILE-1. the following code is placed in the ENVIRONMENT DIVISION : ENVIRONMENT DIVISION INPUT-OUTPUT SECTION.Note now that WS-ADDRESS doesn't have a PIC clause. but usually DISK and PRINTER will be two of the options available. but then if you wanted to insert a new level between 01 and 02. To use a sequential file in COBOL you must make declarations in three of the four divisions.FieldName. this can vary. and stored on a disk or some other permanent storage device. . the last two lines aren't needed since they are the default conditions for a sequential file. They could be ordered 01. if a field name is used elsewhere for another purpose you need to give the record name : MOVE SPACES TO WS-AGE IN WS-PERSON-RECORD Sequential Files A sequential file consists of a sequence of data items terminated by an end marker. Firstly. FILE SECTION. 01 PERSON-REC. 02. With regards to DEVICE-NAME. unlike in Pascal where you need to use A dot operator like RecordName. The reason the levels have been ordered 01. However.

This code opens the file and transfers the first record to the record area defiled in FILE SECTION : PROCEDURE DIVISON. . In this example a status variable is assigned to WS-END-OF-DATA-FILE. OPEN INPUT DATA-FILE-1. The AT END clause is necessary : it tells the file what to do when it hits the end of the file. To open a file for writing use the structure : OPEN OUTPUT DATA-FILE-1.03 NAME 03 ADDRESS PIC X(20) PIC X(30) These records are defined just the same as those in the WORKING-STORAGE section. this needs to be declared and initialized earlier like this : WORKING-STORAGE SECTION. READ DATA-FILE-1. 01 WS-END-OF-DATA-FILE VALUE "N". READ DATA-FILE-1 AT END MOVE "E" TO WS-END-OF-DATA-FILE. Naturally. PARA-1. but first it must be opened in this section. OPEN INPUT DATA-FILE-1. Now we can use READ and WRITE to work with these files in the PROCEDURE DIVISION section. It is also useful to add a condition name like this on the next line : 88 END-OF-DATA-FILE VALUE "E" PIC X We can now test for the end of the file in a loop : PROCEDURE DIVISION PARA-1.

WRITE also allows you to append records to an existing file.. that is DATA-FILE-1 already had data in it.. .. This code searches the file for a record with a certain address...AT END MOVE "E" TO WS-END-OF-DATA-FILE NOT AT END ADD 1 TO RECORD-COUNT END READ . it would count the number of records in the file... WRITE accepts a record name. and then open it with the following code : OPEN EXTEND DATA-FILE-1.. Also note that unlike READ which accepted a file name as its parameter.. CLOSE DATA-FILE-1... This code opens a file and writes a record : PROCEDURE DIVISION..... OPEN OUTPUT DATA-FILE-1. . PARA-1.. MOVE "Dane" TO NAME MOVE "New Zealand" TO ADDRESS. Simply declare the file as in the previous examples. a new file would be created... and the old data would be written over. Given that RECORD-COUNT had been declared... PARA-1.. We use the keyword WRITE to transfer data to a file. Take into account though. It is also possible to edit existing records in place by opening the file for input and output. and then changes that address (note : RECORDFOUND must first be declared in a similar fashion to WS-END-OF-DATA-FILE) : PROCEDURE DIVISION.. WRITE PERSON-REC. Also remember to CLOSE each file you open or bad things will happen.

READ DATA-FILE-1 AT END MOVE "E" TO WS-END-OF-DATA-FILE DISPLAY "DATA NOT FOUND" END-READ PERFORM UNTIL END-OF-DATA-FILE OR RECORD-FOUND IF ADDRESS = "New Zealand" THEN MOVE "Australia" TO ADDRESS REWRITE PERSON-REC MOVE "Y" TO WS-RECORD-FOUND ELSE READ DATA-FILE-1 AT END MOVE "E" TO WS-END-OF-DATA-FILE DISPLAY "DATA NOT FOUND" END-READ END-IF END-PERFORM CLOSE DATA-FILE-1 STOP RUN. COBOL also allows you to create indexed files. Data Editing .OPEN I-O DATA-FILE-1.

50 $30. Remember that these special data types are only used for output.$$9. so it has many built in facilities for data editing and report making.50 $45.99 PIC ----9.800.900. Arrays Arrays aren't quite as natural to COBOL as they are to most other languages.00 This would be very difficult in many languages.450.99.00 $20. but COBOL can easily handle it.COBOL is mainly a language for creating data storage applications.9 This allows you to get rid of irrelevant zeros when you are outputting data. This is how the following numbers would be outputted : 2050 3045000 50 $20. but that is a sign of COBOL's age.$$$. A common situation in report writing is the need to write right aligned output like this $200.600.00 $0. COBOL can handle these topics far better than other structured languages like Pascal and C. a number signaled by a "$" is only included if it's needed.50 As you can see. Here is how an array might be created to store the salaries of ten employees : 01 WS-EMPLOYEE-REC 03 WS-EMPLOYEE-SALARY OCCURS 10 TIMES PIC 99999 . We simply create a data type like this : 01 WS-CURRENCY-OUTPUT PIC $$. and the output is right aligned. you can't do arithmetic with them. You can also do the same thing with "+" and "-" signs : PIC ++++++9.00 $1.

You can then use parenthesis to refer to a particular element in the array : DISPLAY WS-EMPLOYEE-SALARY(3) This will print out the fifth element (as opposed to C. where it would print out the sixth element).You may think it would make sense to declare all this at level 01. 03 WS-MONTH OCCURS 12 TIMES 05 WS-FIVE-HIGHEST-TEMPERATURES OCCURS 5 TIMES PIC 999 . PIC X(20) 05 WS-ADDRESS PIC X(40) 05 WS-SALARY PIC 99999 We can now refer to an entire record at a time with : WS-EMPLOYEE(10) Or to a single field of a record : WS-ADDRESS(30) We can also create multi-dimensional arrays : 01 WS-TABLE VALUE ZEROS. This code creates 20 records with three fields each : 01 EMPLOYEE-REC-TABLE 03 WS-EMPLOYEES 05 WS-NAME OCCURS 50 TIMES. We can create a variable like WS-INDEX. and use it in place of the number : DISPLAY WS-EMPLOYEE-SALARY(WS-INDEX) But we can't use expressions like : DISPLAY WS-EMPLOYEE-SALARY(WS-INDEX + 1) The value of WS-INDEX would need to be incremented before hand. but the OCCURS clause cannot be used at level 01. It is also simple to declare an array of records.

We can now access a specific element like this (which shows the 2nd highest temperature in May): WS-TABLE(5. and is used mainly for error handling. clause : this initializes all the elements to zero (including strings). INPUT-OUTPUT SECTION. but remember to use the same key value as in ENVIRONMENT DIVISION : DATA DIVISION. but with an indexed file we can identify and retrieve records according to their unique key values. To find a record in a sequential file we need to look through all the records in order. . Bare in mind that this cannot be changed once the record has been created. This is how a file might be declared in the ENVIRONMENT DIVISION section : ENVIRONMENT DIVISION. SELECT EMPLOYEE-FILE ASSIGN TO DISK ORGANIZATION IS INDEXED ACCESS MODE IS RANDOM RECORD KEY IS EMPLOYEE-NUMBER FILE STATUS IS EMPLOYEE-STATUS The RECORD KEY is equivalent to a primary key in a relational database. It is must be declared as a two byte character in WORKINGSTORAGE. FILE SECTION. The declaration in DATA DIVISION is the same as with sequential files. FD EMPLOYEE-FILE.Note here the VALUE ZEROS. FILE-CONTROL. The FILE STATUS clause is optional. 2) Or we could access all the temperatures for November like this : WS-MONTH(11) Indexed Files Indexed Files have an advantage over sequential files in the sense that they can be accessed randomly.

and if it exists it will be transferred into the file's record area : MOVE "AR8763" TO EMPLOYEE-NUMBER. supposing the variables mentioned had been created in the WORKING-STORAGE section : MOVE "RW3425" TO WS-EMPLOYEE-NUMBER MOVE "JERRY SEINFELD" TO WS-EMPLOYEE-NAME. MOVE 56000 TO WS-EMPLOYEE-SALARY WRITE EMPLOYEE-REC FROM WS-EMPLOYEE-REC. PIC X(20). To modify a record we would usually do a READ followed by a REWRITE : . To work with the file we use the keywords READ. If we want to read a file." This is how we write a record to file. 01 EMPLOYEE-STATUS PIC XX.01 EMPLOYEE-REC. and then written the record to file in one go. PIC 9(5). we give the key value of the record we want. READ EMPLOYEE-FILE INVALID KEY DISPLAY "No such file exists. Also. WRITE. We have first put the values into an intermediary storage area. remember to close it when you have finished : CLOSE EMPLOYEE-FILE. DELETE and REWRITE. PIC X(6). Firstly we must open the file for input and output : OPEN I-O EMPLOYEE-FILE. 03 EMPLOYEE-NUMBER 03 EMPLOYEE-NAME 03 EMPLOYEE-SALARY WORKING-STORAGE SECTION. We can now move onto the PROCEDURE DIVISION section.

* The YYYYMMDD in "ACCEPT CurrentDate FROM DATE YYYYMMDD. STOP RUN. PROCEDURE DIVISION. Michael Coughlan. This example program is almost the shortest COBOL program we can have. If not used. * Uses the ACCEPT and DISPLAY verbs to accept a student record * from the user and display some of the fields. 2. Also shows how the ACCEPT may be used to get and DISPLAY the system time and date.MOVE "RW3425" TO WS-EMPLOYEE-NUMBER MOVE "KRAMER" TO WS-EMPLOYEE-NAME MOVE 65000 TO WS-EMPLOYEE-SALARY REWRITE EMPLOYEE-REC FROM WS-EMPLOYEE-REC INVALID KEY DISPLAY "Record doesn't exist. PROGRAM-ID." 1. The program accepts a simple student record from the user and displays the individual fields. Also shows how * the ACCEPT may be used to get the system date and time." * is a format command that ensures that the date contains a * 4 digit year. DISPLAY "I did it". IDENTIFICATION DIVISION. PROGRAM-ID. We could make it shorter still by leaving out the STOP RUN. the year supplied by the system will * only contain two digits which may cause a problem in the year 2000. ShortestProgram. IDENTIFICATION DIVISION." This is how we could delete the record we just created (you would normally do a READ first to make sure you have the right record : MOVE "RW3425" TO EMPLOYEE-NUMBER DELETE EMPLOYEE-FILE INVALID KEY DISPLAY "That record doesn't exist. AcceptAndDisplay. . DisplayPrompt. AUTHOR.

* YYMMDD 01 CurrentDate. 02 CurrentMonth PIC 99. 03 Initials PIC XX. 02 CourseCode PIC X(4). DISPLAY "Date is " CurrentDay SPACE CurrentMonth SPACE CurrentYear. * HHMMSSss s = S/100 01 CurrentTime. ACCEPT StudentDetails.Gender" DISPLAY "SSSSSSSNNNNNNNNIICCCCG". Begin. Accepts two single digit numbers from the user. ACCEPT CurrentDate FROM DATE YYYYMMDD. * Example program using ACCEPT.Initials. * YYDDD 01 DayOfYear.ID. Michael Coughlan. AUTHOR. DISPLAY "Today is day " YearDay " of the year". IDENTIFICATION DIVISION. PIC 9(3). 02 StudentName. 02 CurrentMinute PIC 99. PROCEDURE DIVISION. 02 CurrentHour PIC 99. DISPLAY "Enter student details using template below". multiplies them together and displays the result. 01 StudentDetails. 02 FILLER 02 YearDay PIC 9(4). DISPLAY "Name is ". . 02 CurrentDay PIC 99. WORKING-STORAGE SECTION. DISPLAY and MULTIPLY to * get two single digit numbers from the user and multiply them together DATA DIVISION. DISPLAY "Enter . 02 CurrentYear PIC 9(4). STOP RUN.CourseCode.DATA DIVISION. DISPLAY "The time is " CurrentHour ":" CurrentMinute. 02 Gender PIC X. Initials SPACE Surname. 02 StudentId PIC 9(7).Surname. ACCEPT DayOfYear FROM DAY YYYYDDD. 02 FILLER PIC 9(4). 3. PROGRAM-ID. 03 Surname PIC X(8). Multiplier. ACCEPT CurrentTime FROM TIME.

* An example program demonstrating the use of * condition names (level 88's). An example program that implements a primitive calculator. SELECTION AND ITERATION 4. Result. 01 Result PIC 99 VALUE ZEROS. PROCEDURE DIVISION. WORKING-STORAGE SECTION. AUTHOR. DISPLAY "Result is = ". 01 Num1 PIC 9 VALUE ZEROS. An example program demonstrating the use of Condition Names (level 88's). PROGRAM-ID. 5. MULTIPLY Num1 BY Num2 GIVING Result. Iteration-If. Michael Coughlan. The calculator only does additions and multiplications. Result END-PERFORM. 01 Num1 PIC 9 VALUE ZEROS. IDENTIFICATION DIVISION. Conditions. STOP RUN. DATA DIVISION. . STOP RUN. PERFORM 3 TIMES DISPLAY "Enter First Number : " WITH NO ADVANCING ACCEPT Num1 DISPLAY "Enter Second Number : " WITH NO ADVANCING ACCEPT Num2 DISPLAY "Enter operator (+ or *) : " WITH NO ADVANCING ACCEPT Operator IF Operator = "+" THEN ADD Num1. AUTHOR.WORKING-STORAGE SECTION. PROCEDURE DIVISION. ACCEPT Num1. ACCEPT Num2. Num2 GIVING Result END-IF IF Operator = "*" THEN MULTIPLY Num1 BY Num2 GIVING Result END-IF DISPLAY "Result is = ". Calculator. DISPLAY "Enter second number (1 digit) : " WITH NO ADVANCING. PROGRAM-ID. IDENTIFICATION DIVISION. 01 Num2 PIC 9 VALUE ZEROS. DISPLAY "Enter first number (1 digit) : " WITH NO ADVANCING. 01 Result PIC 99 VALUE ZEROS. 01 Num2 PIC 9 VALUE ZEROS. 01 Operator PIC X VALUE SPACE. Michael Coughlan.

IDENTIFICATION DIVISION. . 01 Char PIC X. 88 Consonant VALUE "b". DATA DIVISION. PROCEDURE DIVISION. Michael Coughlan. * Use the output of this program to get an understanding of how * this format of the PERFORM works.".* The EVALUATE and PERFORM verbs are also used. PerformFormat1. Starting to run program" PERFORM OneLevelDown DISPLAY "Back in TopLevel. "i". "c". DISPLAY "Enter lower case character or digit. An example program demonstrating how the first format of the PERFORM may be used to change the flow of control through a program." WHEN Consonant DISPLAY "The letter " Char " is a consonant. DISPLAY ">>>> Now in OneLevelDown" PERFORM TwoLevelsDown DISPLAY ">>>> Back in OneLevelDown". TopLevel. TwoLevelsDown. No data ends.". Begin. DISPLAY ">>>>>>>> Now in TwoLevelsDown. PROGRAM-ID." WHEN Digit DISPLAY Char " is a digit. "0" THRU "9". 6. "e"." PERFORM ThreeLevelsDown. 88 Vowel VALUE "a". "v" THRU "z". 88 Digit VALUE "0" THRU "9". ACCEPT Char. DISPLAY ">>>>>>>> Back in TwoLevelsDown. "p" THRU "t"." WHEN OTHER DISPLAY "problems found" END-EVALUATE END-PERFORM STOP RUN.". AUTHOR. "u". DISPLAY ">>>>>>>>>>>> Now in ThreeLevelsDown". OneLevelDown. "h" "j" THRU "n". "f". 88 ValidCharacter VALUE "a" THRU "z". * Illustrates how the first format of the PERFORM may * be used to change the flow of control through a program. DISPLAY "In TopLevel. PROCEDURE DIVISION. PERFORM UNTIL NOT ValidCharacter EVALUATE TRUE WHEN Vowel DISPLAY "The letter " Char " is a vowel. "d". ThreeLevelsDown. "g". STOP RUN. "o". WORKING-STORAGE SECTION.

IDENTIFICATION DIVISION. PROGRAM-ID. 88 MaxCountReached VALUE 99. * The PERFORM. STOP RUN. 01 UserInput PIC 99 VALUE ZEROS. IDENTIFICATION DIVISION. WORKING-STORAGE SECTION. WORKING-STORAGE SECTION. PROCEDURE DIVISION. * The INITIALIZE verb sets a data-item to its initial or * starting value.TIMES format executes a block of code x * number of times. DISPLAY "Starting to run program" PERFORM 3 TIMES DISPLAY ">>>>This is an in line Perform" END-PERFORM DISPLAY "Finished in line Perform" PERFORM OutOfLineEG NumOfTimes TIMES DISPLAY "Back in Begin.. 8. . * The PERFORM.UNTIL. PerformFormat3..UNTIL (third format) may be used to process a stream of values where the length of the stream cannot be determined in advance (although in this case there is a set maximum number of values in the stream). Begin. AUTHOR. DATA DIVISION. * Pay particular attention to the way the number stream is * processed in this program. About to Stop".7. The PERFORM. 01 IterCount PIC 99 VALUE ZEROS.. * Note how the ON SIZE ERROR can be used to detect when the * result of a computation is tot big for the data-item intended * to hold it. Demonstrates how the PERFORM. * Demonstrates the use of the PERFORM. * Demonstrates the second format of the PERFORM.UNTIL is most often used to process a * stream of data where the length of the stream can not * be determined in advance. Demonstrates the second format of the PERFORM.. AUTHOR.. Michael Coughlan. Michael Coughlan. PROGRAM-ID.TIMES may be used to execute a block of code x number of times. PerformFormat2. DISPLAY ">>>> This is an out of line Perform". 01 NumofTimes PIC 9 VALUE 5. DATA DIVISION. OutOfLineEG.

" RunningTotal DISPLAY "The final count is ." AverageValue STOP RUN.VARYING. Michael Coughlan. * Note that the PERFORM within a PERFORM produces the same * results as the PERFORM. 9. PerformFormat4." IterCount DISPLAY "Enter number :. 01 RunningTotal PIC 999 VALUE ZEROS. GetUserInput. PROGRAM-ID.AFTER DATA DIVISION. Begin..VARYING and the PERFORM. ADD UserInput TO RunningTotal ON SIZE ERROR DISPLAY "Error . .new total too large for data-item. 01 AverageValue PIC 99 VALUES ZEROS. AUTHOR." IterCount COMPUTE AverageValue = RunningTotal / IterCount DISPLAY "The average value entered is . 01 LoopCount PIC 9 VALUE ZEROS. Also introduces the WITH TEST BEFORE and WITH TEST AFTER phrases...VARYING." WITH NO ADVANCING ACCEPT UserInput PERFORM GetUserInput UNTIL EndOfUserInput OR MaxCountReached DISPLAY "The final total is . PERFORM UNTIL IterCount = 5 DISPLAY "IterCount = " IterCount ADD 1 TO IterCount END-PERFORM DISPLAY "Finished in line Perform. IDENTIFICATION DIVISION. PROCEDURE DIVISION..VARYING format." WITH NO ADVANCING ACCEPT UserInput.." INITIALIZE Itercount DISPLAY "Enter a stream of up to 99 numbers. Demonstrates how the PERFORM. WORKING-STORAGE SECTION." NOT ON SIZE ERROR ADD 1 TO IterCount END-ADD END-ADD DISPLAY "Total so far is .88 EndOfUserInput VALUE ZEROS." RunningTotal DISPLAY "Count so far is ." DISPLAY "Each number must be in the range 1-99. Enter 0 to stop.AFTER (fourth format) may be used for counting iteration. PROCEDURE DIVISION. 01 LoopCount2 PIC S9 VALUE ZEROS. * An example program using the PERFORM." DISPLAY "Enter number :. * Pay particular attention to the values produced by the * WITH TEST BEFORE and WITH TEST AFTER loops..

DISPLAY "Start WHILE Iteration of LoopBody" PERFORM LoopBody WITH TEST BEFORE VARYING LoopCount FROM 1 BY 2 UNTIL LoopCount GREATER THAN 5. DISPLAY "Start inline loops" PERFORM VARYING LoopCount FROM 1 BY 1 UNTIL LoopCount GREATER THAN 4 PERFORM VARYING LoopCount2 FROM 5 BY -2 UNTIL LoopCount2 LESS THAN ZERO DISPLAY "InLine loop " WITH NO ADVANCING DISPLAY "LoopCount = " LoopCount " LoopCount2 = " LoopCount2 END-PERFORM END-PERFORM. PERFORM LoopBody VARYING LoopCount FROM 1 BY 1 UNTIL LoopCount GREATER THAN 4 AFTER LoopCount2 FROM 5 BY -2 UNTIL LoopCount2 LESS THAN ZERO. 02 PrnTens PIC 9.. 02 HundredsCount PIC 99 VALUE ZEROS. 10. DISPLAY "Finished REPEAT iteration. PROGRAM-ID.. 01 DisplayItems. . 02 UnitsCount PIC 99 VALUE ZEROS. 01 Counters. DISPLAY "Start PERFORM VARYING. Demonstrates how the PERFORM.. * Simulates a mileage counter DATA DIVISION. LoopBody.VARYING and the PERFORM. DISPLAY "LoopBody " WITH NO ADVANCING DISPLAY "LoopCount = " LoopCount " LoopCount2 = " LoopCount2. DISPLAY "Finished PERFORM VARYING.AFTER".AFTER (fourth format) may be used to simulate a mileage counter. Michael Coughlan. MileageCounter. DISPLAY "Finished inline loops". LoopCount = " LoopCount. DISPLAY "Finished WHILE iteration.. DISPLAY "Start REPEAT Iteration of LoopBody" PERFORM LoopBody WITH TEST AFTER VARYING LoopCount FROM 1 BY 2 UNTIL LoopCount GREATER THAN 5.AFTER". STOP RUN..VARYING.Begin. IDENTIFICATION DIVISION. 02 TensCount PIC 99 VALUE ZEROS. 02 PrnHunds PIC 9. WORKING-STORAGE SECTION. AUTHOR. LoopCount = " LoopCount.

IDENTIFICATION DIVISION." STOP RUN. CountMilage. Michael Coughlan. DISPLAY "Using an out-of-line Perform". * Example program showing how to create a sequential file * using the ACCEPT and the WRITE verbs. PROGRAM-ID. PROCEDURE DIVISION. FILE SECTION. MOVE HundredsCount TO PrnHunds MOVE TensCount TO PrnTens MOVE UnitsCount TO PrnUnits DISPLAY PrnHunds "-" PrnTens "-" PrnUnits." DISPLAY "Now using in-line Performs" DISPLAY "About to start mileage counter simulation". Example program demonstrating how to create a sequential file from data input by the user. DISPLAY "About to start mileage counter simulation". AUTHOR. SEQUENTIAL FILE 11. Begin. SeqWrite. DISPLAY "End of mileage counter simulation.02 PrnUnits PIC 9. FILE-CONTROL. . PERFORM VARYING HundredsCount FROM 0 BY 1 UNTIL HundredsCount > 9 PERFORM VARYING TensCount FROM 0 BY 1 UNTIL TensCount > 9 PERFORM VARYING UnitsCount FROM 0 BY 1 UNTIL UnitsCount > 9 MOVE HundredsCount TO PrnHunds MOVE TensCount TO PrnTens MOVE UnitsCount TO PrnUnits DISPLAY PrnHunds "-" PrnTens "-" PrnUnits END-PERFORM END-PERFORM END-PERFORM. PERFORM CountMilage VARYING HundredsCount FROM 0 BY 1 UNTIL HundredsCount > 9 AFTER TensCount FROM 0 BY 1 UNTIL TensCount > 9 AFTER UnitsCount FROM 0 BY 1 UNTIL UnitsCount > 9 DISPLAY "End of mileage counter simulation. * Note: In this version of COBOL pressing the Carriage Return (CR) * without entering any data results in StudentDetails being filled * with spaces. INPUT-OUTPUT SECTION. SELECT StudentFile ASSIGN TO "STUDENTS. ENVIRONMENT DIVISION.DAT" ORGANIZATION IS LINE SEQUENTIAL. DATA DIVISION.

02 StudentName. Initials. MOB. PROCEDURE DIVISION.CBL for the definitive example. * See SeqRead. Course. To test the program download thistestdata file. 02 StudentId PIC 9(7). PROGRAM-ID. This version does not use level 88's to signal when the end of the file has been reached.StudId. 03 DOBirth PIC 9(2). Begin. Surname. 02 StudentName. 02 DateOfBirth. 02 CourseCode PIC X(4). FD StudentFile." PERFORM GetStudentDetails PERFORM UNTIL StudentDetails = SPACES WRITE StudentDetails PERFORM GetStudentDetails END-PERFORM CLOSE StudentFile STOP RUN. DATA DIVISION. FILE SECTION. IDENTIFICATION DIVISION. FILE-CONTROL.FD StudentFile. 03 MOBirth PIC 9(2). GetStudentDetails. DOB. 01 StudentDetails. Enter no data to end. AUTHOR. SELECT StudentFile ASSIGN TO "STUDENTS. 01 StudentDetails. 03 Initials PIC XX. An example program that reads a sequential file and displays the records. YOB. * An example showing how to read a sequential file without * using condition names. 02 Gender PIC X. SeqReadNo88. 02 StudentId PIC 9(7). INPUT-OUTPUT SECTION.DAT" ORGANIZATION IS LINE SEQUENTIAL. ENVIRONMENT DIVISION. Gender" DISPLAY "NNNNNNNSSSSSSSSIIYYYYMMDDCCCCG" ACCEPT StudentDetails. DISPLAY "Enter . 03 YOBirth PIC 9(4). Michael Coughlan. . 03 Surname PIC X(8). OPEN OUTPUT StudentFile DISPLAY "Enter student details using template below. 12.

DATA DIVISION. FILE-CONTROL. 02 StudentName. 03 YOBirth PIC 9(4). 02 DateOfBirth. 02 Gender PIC X. 03 DOBirth PIC 9(2). OPEN INPUT StudentFile READ StudentFile AT END MOVE HIGH-VALUES TO StudentDetails END-READ PERFORM UNTIL StudentDetails = HIGH-VALUES DISPLAY StudentId SPACE StudentName SPACE CourseCode SPACE YOBirth READ StudentFile AT END MOVE HIGH-VALUES TO StudentDetails END-READ END-PERFORM CLOSE StudentFile STOP RUN. * This is the definitive example ENVIRONMENT DIVISION. 02 StudentId PIC 9(7). An example program that reads a sequential file and displays the records. Michael Coughlan. AUTHOR. 88 EndOfStudentFile VALUE HIGH-VALUES. IDENTIFICATION DIVISION. 03 Initials PIC XX. 01 StudentDetails. 03 MOBirth PIC 9(2). FILE SECTION. 13. FD StudentFile.DAT" ORGANIZATION IS LINE SEQUENTIAL. 03 MOBirth PIC 9(2). To test the program download this testdata file. PROCEDURE DIVISION. 02 DateOfBirth. SeqRead. PROGRAM-ID. 02 CourseCode PIC X(4). 03 YOBirth PIC 9(4). This is the correct version which uses the Condition Name (level 88) "EndOfFile"to signal when the end of the file has been reached. . INPUT-OUTPUT SECTION. * An example program showing how to read a sequential file. 03 Initials PIC XX. Begin. 03 Surname PIC X(8). SELECT StudentFile ASSIGN TO "STUDENTS.03 Surname PIC X(8).

88 EndOfStudentFile VALUE HIGH-VALUES. OPEN INPUT StudentFile READ StudentFile AT END SET EndOfStudentFile TO TRUE END-READ PERFORM UNTIL EndOfStudentFile DISPLAY StudentId SPACE StudentName SPACE CourseCode SPACE YOBirth READ StudentFile AT END SET EndOfStudentFile TO TRUE END-READ END-PERFORM CLOSE StudentFile STOP RUN. To test this program you will need to download Student Masterfile . 02 CourseCode PIC X(4). 14.DAT" ORGANIZATION IS LINE SEQUENTIAL ACCESS MODE IS SEQUENTIAL. Demonstrates how to insert records into a sequential file from a file of transaction records.NEW" ORGANIZATION IS LINE SEQUENTIAL ACCESS MODE IS SEQUENTIAL. Begin.which contains the inserted records.Students. DATA DIVISION.Students. SELECT NewStudentRecords ASSIGN "STUDENTS. AUTHOR.DAT" ORGANIZATION IS LINE SEQUENTIAL ACCESS MODE IS SEQUENTIAL. FILE-CONTROL. IDENTIFICATION DIVISION. SELECT StudentRecords ASSIGN "STUDENTS. SELECT TransRecords ASSIGN "TRANSINS. PROCEDURE DIVISION. 01 StudentRecord.Dat file with insertions * taken from the Transins. .03 DOBirth PIC 9(2). FILE SECTION. InsertRecords. Michael Coughlan.Dat and the Transaction file Transins. * This program updates the Students. 02 Gender PIC X. FD StudentRecords. ENVIRONMENT DIVISION.dat. A new file is created which contains the inserted records. INPUT-OUTPUT SECTION.Dat file to create a new file * . PROGRAM-ID. 02 StudentID PIC X(7).New .

Michael Coughlan. Prints the results in a very short report. To test the program download Students. This program reads records from the student file. FD NewStudentRecords. 88 EndOfTransFile VALUE HIGH-VALUES. PROCEDURE DIVISION. PROGRAM-ID. counts the total number of student records in the file and the number records for females and males. 15. 02 TransStudentID PIC X(7).02 FILLER PIC X(23)." TransStudentId " already exists in file" READ TransRecords AT END SET EndOfTransFile TO TRUE END-READ END-EVALUATE END-PERFORM CLOSE StudentRecords CLOSE TransRecords CLOSE NewStudentRecords STOP RUN. 01 TransRecord. . AUTHOR. FD TransRecords. 01 NewStudentRecord PIC X(30).Dat IDENTIFICATION DIVISION. StudentNumbersReport . OPEN INPUT StudentRecords OPEN INPUT TransRecords OPEN OUTPUT NewStudentRecords READ StudentRecords AT END SET EndOfStudentFile TO TRUE END-READ READ TransRecords AT END SET EndOfTransFile TO TRUE END-READ PERFORM UNTIL (EndOfStudentFile) AND (EndOfTransFile) EVALUATE TRUE WHEN (StudentID < TransStudentID) WRITE NewStudentRecord FROM StudentRecord READ StudentRecords AT END SET EndOfStudentFile TO TRUE END-READ WHEN (StudentID > TransStudentID) WRITE NewStudentRecord FROM TransRecord READ TransRecords AT END SET EndOfTransFile TO TRUE END-READ WHEN (StudentID = TransStudentID) DISPLAY "Error . 02 FILLER PIC X(23). BEGIN.

WORKING-STORAGE SECTION.ZZ9.RPT" ORGANIZATION IS LINE SEQUENTIAL. 02 CourseCode PIC X(4). 01 StudentDetails. INPUT-OUTPUT SECTION. 01 PrintLine PIC X(40). 88 EndOfStudentFile VALUE HIGH-VALUES. * Adds one to the TotalStudents count * IF the Gender is Male adds one to TotalMales * IF the Gender is Female adds one to TotalFemales * At end of file writes the results to the report file. 02 FemaleCount PIC 9(4) VALUE ZERO. *OUTPUT Shows the number of student records in the file and the * number of records for males and females. 02 StudentCount PIC 9(4) VALUE ZERO. 03 DOBirth PIC 9(2). . 02 StudentId PIC 9(7). 02 FILLER PIC X(17) VALUE "Total Males = ". "m". 03 Surname PIC X(8). 01 FemaleTotalLine. 02 FILLER PIC X(17) VALUE "Total Students = ". 01 WorkTotals.*INPUT The student record file Students. 01 MaleTotalLine.DAT" ORGANIZATION IS LINE SEQUENTIAL. FILE-CONTROL. 02 MaleCount PIC 9(4) VALUE ZERO. 02 DateOfBirth. FILE SECTION. FD StudentFile. 02 StudentName.ZZ9. *PROCESSING For each record read.ZZ9. 02 PrnStudentCount PIC Z. 02 Gender PIC X. 01 HeadingLine PIC X(21) VALUE " Record Count Report". 03 YOBirth PIC 9(4). 02 PrnFemaleCount PIC Z. 03 Initials PIC XX. 02 FILLER PIC X(17) VALUE "Total Females = ". 01 StudentTotalLine. SELECT StudentFile ASSIGN TO "STUDENTS. DATA DIVISION. 88 Male VALUE "M". SELECT ReportFile ASSIGN TO "STUDENTS. ENVIRONMENT DIVISION. 03 MOBirth PIC 9(2).Dat Records in this file * are sequenced on ascending Student Number. 02 PrnMaleCount PIC Z. FD ReportFile.

AUTHOR. Uses the SORT and an INPUT PROCEDURE to accept records from the user and sort them on ascending StudentId. FILE-CONTROL. Michael Coughlan. MOVE StudentCount TO PrnStudentCount MOVE MaleCount TO PrnMaleCount MOVE FemaleCount TO PrnFemaleCount WRITE PrintLine FROM HeadingLine AFTER ADVANCING PAGE WRITE PrintLine FROM StudentTotalLine AFTER ADVANCING 2 LINES WRITE PrintLine FROM MaleTotalLine AFTER ADVANCING 2 LINES WRITE PrintLine FROM FemaleTotalLine AFTER ADVANCING 2 LINES. ENVIRONMENT DIVISION. PrintReportLines. . Sorting & Merging 16. INPUT-OUTPUT SECTION. ReportFile STOP RUN. Begin.DAT" ORGANIZATION IS LINE SEQUENTIAL. This program * allows student records to be entered in any order but * produces a file sequenced on ascending StudentId.PROCEDURE DIVISION. * An example program using the SORT and an * INPUT PROCEDURE. InputSort. The program accepts records * from the user and RELEASEs them to the work file * where they are sorted. SELECT StudentFile ASSIGN TO "SORTSTUD. OPEN INPUT StudentFile OPEN OUTPUT ReportFile READ StudentFile AT END SET EndOfStudentFile TO TRUE END-READ PERFORM UNTIL EndOfStudentFile ADD 1 TO StudentCount IF Male ADD 1 TO MaleCount ELSE ADD 1 TO FemaleCount END-IF READ StudentFile AT END SET EndOfStudentFile TO TRUE END-READ END-PERFORM PERFORM PrintReportLines CLOSE StudentFile. IDENTIFICATION DIVISION. PROGRAM-ID.

". * 03 MOBirth PIC 9(2). * 02 CourseCode PIC X(4). Begin. STOP RUN.Dat IDENTIFICATION DIVISION. Sorts the student masterfile (sorted on ascending Student Id) and produces a new file. DISPLAY "Enter . SORT WorkFile ON ASCENDING KEY WStudentId INPUT PROCEDURE IS GetStudentDetails GIVING StudentFile. To test the program download Students. 02 FILLER PIC X(23).TMP". Gender" DISPLAY "NNNNNNNSSSSSSSSIIYYYYMMDDCCCCG" ACCEPT WorkRec. sorted on ascending student name. 01 WorkRec. DOB. containing only the records of male students. Surname. Michael Coughlan.StudId. 17. 02 WStudentId PIC 9(7). PERFORM UNTIL WorkRec = SPACES RELEASE WorkRec ACCEPT WorkRec END-PERFORM. Course. * 03 YOBirth PIC 9(4). MaleSort. * 02 DateOfBirth. SD WorkFile. MOB." DISPLAY "Enter no data to end. DISPLAY "Enter student details using template below.SELECT WorkFile ASSIGN TO "WORK. Initials. * 02 StudentName. * Uses the the SORT and an INPUT PROCEDURE to read * the student masterfile (sorted on ascending Student Id) * and from it to produce a file containing only the records of * male students sorted on ascending student name. PROCEDURE DIVISION. GetStudentDetails. AUTHOR. YOB. FD StudentFile. PROGRAM-ID. * 03 Initials PIC XX. FILE SECTION. * 02 Gender PIC X. * 03 Surname PIC X(8). DATA DIVISION. * 03 DOBirth PIC 9(2). * The StudentDetails record has the description shown below. 01 StudentDetails PIC X(30). * But in this program we don't need to refer to any of the items in * the record and so we have described it as PIC X(32) * 01 StudentDetails * 02 StudentId PIC 9(7). .

FD MaleStudentFile. 02 FILLER PIC 9(7).TMP". Uses the MERGE to insert records from a transaction file into a sequential master file. 18. STOP RUN. OPEN INPUT StudentFile READ StudentFile AT END SET EndOfFile TO TRUE END-READ PERFORM UNTIL EndOfFile MOVE StudentRec TO WorkRec IF MaleStudent RELEASE WorkRec END-IF READ StudentFile AT END SET EndOfFile TO TRUE END-READ END-PERFORM CLOSE StudentFile.DAT" ORGANIZATION IS LINE SEQUENTIAL.dat.Dat and the Transaction file Transins. INPUT-OUTPUT SECTION. 88 EndOfFile VALUE HIGH-VALUES. PROCEDURE DIVISION. FD StudentFile. 01 MaleStudentRec PIC X(30).DAT" ORGANIZATION IS LINE SEQUENTIAL. To test this program you will need to download Student Masterfile . GetMaleStudents. . SELECT MaleStudentFile ASSIGN TO "MALESTUDS. DATA DIVISION. 02 WGender PIC X. 01 StudentRec PIC X(30). FILE-CONTROL. 88 MaleStudent VALUE "M". SORT WorkFile ON ASCENDING KEY WStudentName INPUT PROCEDURE IS GetMaleStudents GIVING MaleStudentFile. FILE SECTION.Students. 02 FILLER PIC X(12). SD WorkFile.ENVIRONMENT DIVISION. 02 WStudentName PIC X(10). SELECT StudentFile ASSIGN TO "STUDENTS. SELECT WorkFile ASSIGN TO "WORK. 01 WorkRec. Begin.

Solves a number of string handling tasks such as : . Removing leading blanks from a string. MERGE WorkFile ON ASCENDING KEY WStudentId USING InsertionsFile.SeqInsert. 02 FILLER PIC X(23). * The program merges the file Students. PROGRAM-ID. Extracting the last x number of chars from a string.Dat to create a new file Students. AUTHOR. String handling 19. DATA DIVISION. SELECT InsertionsFile ASSIGN TO "TRANSINS. FD NewStudentFile. RefModification. INPUT-OUTPUT SECTION.Dat and * Transins. Removing trailing blanks from a string.TMP". 01 StudentRec PIC X(30). ENVIRONMENT DIVISION. PROCEDURE DIVISION. MergeFiles. Michael Coughlan. STOP RUN.cbl . SD WorkFile. .IDENTIFICATION DIVISION. Begin.DAT" ORGANIZATION IS LINE SEQUENTIAL. * See the example program . * Example program demonstrating the use of the MERGE. 01 NewStudentRec PIC X(30). FD InsertionsFile. FILE SECTION. Extracting the first x number of chars from a string. FD StudentFile. PROGRAM-ID. SELECT NewStudentFile ASSIGN TO "STUDENTS. FILE-CONTROL.DAT" ORGANIZATION IS LINE SEQUENTIAL.New. 01 InsertionRec PIC X(30). StudentFile GIVING NewStudentFile. SELECT WorkFile ASSIGN TO "WORK. 02 WStudentId PIC 9(7).for a more traditional * approach to inserting records in a Sequential File. * A problem with using the MERGE for inserting records is that * duplicate records are not detected.Extracting a substring from a string given the start position and length of the substring. SELECT StudentFile ASSIGN TO "STUDENTS. AUTHOR. MICHAEL COUGHLAN. 01 WorkRec. Finding the location of the first occurrence of any of a substring's chars in a string IDENTIFICATION DIVISION.NEW" ORGANIZATION IS LINE SEQUENTIAL.

DATA DIVISION. * Solution . * Use reference modification of get the substring. * The program solves a number of tasks suggested by Judy Yaeger in her * article "Character Manipulation Using COBOL" where she attempted to show * the limitations of COBOL string handling. * In this example we get 3 characters starting at position 9 DISPLAY "Task1 = " xStr(9:3) * Task2 prefix(xStr. 01 CharCount PIC 99 VALUE ZEROS. 01 StrLength PIC 99 VALUE ZEROS. DISPLAY "Task4 Before = " xStr "<<<<<<" MOVE 0 TO CharCount INSPECT FUNCTION REVERSE(xStr) TALLYING CharCount FOR LEADING SPACES DISPLAY "Task4 After = "xStr(1:50 . 01 EndCount PIC 99 VALUE ZEROS. Length) * Extract a substring from a string given the StartPos and Length. The substring length is then the * FullSringLength .use reference modification with start of substring * defined as the FullStringLength .SubStringLength + 1 * In this example we get the last 13 characters. * Task1 substring(xStr. WORKING-STORAGE SECTION. Begin. PROCEDURE DIVISION.CharCount) "<<<<<<<".CharCount. MOVE 13 TO StrLength DISPLAY "Task3 = " xStr2((StrSize . 01 CharPos PIC 99 VALUE ZEROS.* An example program using Reference Modification. 01 StrSize PIC 99 VALUE 32.. * In this example we get the first 7 characters DISPLAY "Task2 = " xStr(1:7) * Task3 suffix(xStr. 01 xStr2 PIC X(32) VALUE "This is the second source string".use reference modification starting at position 1. Intrinsic Functions * and the INSPECT.VARYING to * keep reducing the size of the substring until a non space character .use reference modification to get the substring. * Solution 2 * Use reference modification and the PERFORM.Length) * Extract the first Length number of chars from a string * Solution . StartPos. 01 xStr PIC X(50) VALUE " This is the first source string".Length) * Extract the last Length number of chars from a string * Solution .StrLength) + 1:StrLength) * Task4: trimblank(xStr) * Remove trailing blanks from a string * Solution 1 * Use the REVERSE intrinsic function to reverse the string * then use the INSPECT tallying to count the number of spaces at the * begining of the reversed string. 01 yStr PIC X(4) VALUE SPACES.

PROGRAM-ID. Michael Coughlan. * In this example we have only implemented unpacking .. To test this program use the data file VarLen.VARYING and reference modification to * locate each of the characters in the yString.Use the PERFORM. PERFORM VARYING CharCount FROM 50 BY -1 UNTIL xStr(CharCount:1) NOT = SPACE END-PERFORM DISPLAY "Task4 After = "xStr(1:CharCount) "<<<<<<<". Return whichever is the least. 20. UnstringFileEg. INSPECT xStr TALLYING CharCount FOR LEADING SPACES DISPLAY "Task5 =" xStr(CharCount: 50 . * Solution .. CharCount has the location.* is encountered.Use the INSPECT. AUTHOR. An example showing the unpacking of comma separated records and the size validation of the unpacked fields. Then use reference modification to get the substring.TALLYING to count the characters before * the first occurrence of the substring. * Solution .yStr) * Find the location of the first occurrence of substring yStr in xStr. IDENTIFICATION DIVISION. * In this example we get the position of the substring "source".dat. MOVE 1 TO CharCount. in string yStr * Solution . MOVE 1 TO CharCount INSPECT xStr TALLYING CharCount for CHARACTERS BEFORE INITIAL "source".CharCount characters.CharCount) * Task6 index(xStr. MOVE "fred" TO yStr MOVE 51 TO EndCount PERFORM VARYING CharPos FROM 1 BY 1 UNTIL CharPos > 4 MOVE 1 TO CharCount INSPECT xStr TALLYING CharCount FOR CHARACTERS BEFORE INITIAL yStr(CharPos:1) IF CharCount < EndCount MOVE CharCount TO EndCount END-IF END-PERFORM DISPLAY "Task7 First occurrence is in char position " EndCount DISPLAY "The character is " xStr(EndCount:1) STOP RUN. * Task5 Left_trimblank(xStr) * Remove leading blanks from a string.yStr) * Find the location of the first occurrence of any of the characters * in substring xStr.Use the inspect to count the leading blanks and reference * modification to get the substring from the point indicated by CharCount * and for FullStrLength . Then use the INSPECT to * find the position of each in the xString. * Example showing the unpacking of comma separated records * and the size validation of the unpacked fields. DISPLAY "Task6 First occurrence is in char position " CharCount * Task7 cindex(xStr.

88 DeleteVideo VALUE "2. 02 TransDate PIC X(8). 02 DateCount PIC 99.DAT" ORGANIZATION IS LINE SEQUENTIAL. 02 SuppNameCount PIC 99.".". FILE SECTION.". FILE-CONTROL. 02 TransType PIC 9. ENVIRONMENT DIVISION. 88 ValidSuppAdr VALUE 1 THRU 50. 02 Supplier-Code PIC XX. 02 TypeCode PIC XX. 88 ValidDate VALUE 8. 88 ValidSuppCode VALUE 1 THRU 2. OPEN INPUT VarLengthRecFile READ VarLengthRecFile AT END SET EndOfFile TO TRUE END-READ PERFORM UNTIL EndOfFile MOVE ZEROS TO InsertSupplierCounts * First find the actual length of the record PERFORM VARYING StringEnd FROM 78 BY -1 UNTIL RemainingRec(StringEnd:1) NOT = SPACE . Begin. 88 EndOfFile VALUE HIGH-VALUES. SELECT VarLengthRecFile ASSIGN TO "VarLen.". 02 SuppAdrCount PIC 99. 88 InsertSupplier VALUE "4. "2. FD VarLengthRecFile. * These counts allow us to detect if there are too many chars * in a particular field. 88 ValidSuppName VALUE 1 THRU 20. 02 Supplier-Address PIC X(50). 02 SuppCodeCount PIC 99. 02 RemainingRec PIC X(78). 88 ValidTypeCode VALUE "1. 88 DeleteSupplier VALUE "1.".". 01 InsertSupplierCounts.* an InsertSupplier record. 01 VarLenRec. 01 InsertSupplierRec. 88 InsertVideo VALUE "3. For instance the date field should be * 8 characters in size.". WORKING-STORAGE SECTION. "3. PROCEDURE DIVISION.". INPUT-OUTPUT SECTION. 01 StringEnd PIC 99. DATA DIVISION."4. 02 Supplier-Name PIC X(20).

02 CityCode PIC 9. FILE-CONTROL. SELECT SalesFile ASSIGN TO "GBSALES. A simplified version of RepWriteFull.only salesperson totals ENVIRONMENT DIVISION.LPT"." INTO TransDate COUNT IN DateCount Supplier-Code COUNT IN SuppCodeCount Supplier-Name COUNT IN SuppNameCount Supplier-Address COUNT IN SuppAdrCount END-UNSTRING PERFORM CheckForErrors ELSE IF NOT ValidTypeCode DISPLAY SPACE DISPLAY "Record = " VarLenRec(1:70) DISPLAY "Type code is not valid" END-IF END-IF READ VarLengthRecFile AT END SET EndOfFile TO TRUE END-READ END-PERFORM CLOSE VarLengthRecFile STOP RUN. FILE SECTION. DATA DIVISION. SELECT PrintFile ASSIGN TO "SALESREPORTA. DISPLAY SPACE DISPLAY "Record = " VarLenRec(1:70) IF NOT ValidDate DISPLAY "Date Size Error" END-IF IF NOT ValidSuppCode DISPLAY "Supplier Code Error" END-IF IF NOT ValidSuppName DISPLAY "Supplier name Error" END-IF IF NOT ValidSuppAdr DISPLAY "Supplier address Error" END-IF. INPUT-OUTPUT SECTION. Report Generator 21.cbl using only one control break. IDENTIFICATION DIVISION. 88 EndOfFile VALUE HIGH-VALUES.dat data file.DAT" ORGANIZATION IS LINE SEQUENTIAL. All four of these Report Writer programs use the GBsales. FD SalesFile. 01 SalesRecord. PROGRAM-ID. . CheckForErrors. AUTHOR. Michael Coughlan.END-PERFORM IF InsertSupplier UNSTRING RemainingRec(1:StringEnd) DELIMITED BY ". ReportExampleA. * Very simple version .

$$$. 03 COLUMN 12 PIC X(32) VALUE "An example COBOL Report Program". 03 COLUMN 1 PIC X(9) SOURCE CityName(CityCode). 02 LINE IS PLUS 1. 03 COLUMN 12 PIC X(11) VALUE "Salesperson". 03 COLUMN 2 PIC X(4) VALUE "City". REPORT SECTION. 03 CityName PIC X(9) OCCURS 7 TIMES. . 01 SalesPersonGrp TYPE IS CONTROL FOOTING SalesPersonNum NEXT GROUP PLUS 2. 03 COLUMN 6 PIC X(17) VALUE "Bible Salesperson". 03 COLUMN 23 PIC X(26) VALUE " . 03 COLUMN 28 PIC X(4) VALUE "Sale". 03 COLUMN 28 PIC X(5) VALUE "Value". 03 COLUMN 13 PIC X(6) VALUE "Number". 01 DetailLine TYPE IS DETAIL. 03 COLUMN 25 PIC $$. WORKING-STORAGE SECTION. 02 LINE 5.02 SalesPersonNum PIC 9. 03 FILLER PIC X(18) VALUE "Dublin Belfast ".99 SOURCE ValueOfSale. 02 FILLER REDEFINES TableValues. 03 FILLER PIC X(18) VALUE "Sligo Waterford". 02 LINE 4.Sales and Salary Report". 01 TYPE IS PAGE HEADING. 03 FILLER PIC X(9) VALUE "Limerick". 02 TableValues. 02 LINE IS PLUS 1. FD PrintFile REPORT IS SalesReport. 03 COLUMN 2 PIC X(4) VALUE "Name". 03 COLUMN 15 PIC X(21) VALUE "Sales for salesperson". 02 LINE 1. 01 NameTable. 03 FILLER PIC X(18) VALUE "Cork Galway ". 02 ValueOfSale PIC 9(4)V99. 02 LINE 2. RD SalesReport CONTROLS ARE SalesPersonNum PAGE LIMIT IS 66 HEADING 1 FIRST DETAIL 6 LAST DETAIL 42 FOOTING 52. 03 COLUMN 15 PIC 9 SOURCE SalesPersonNum.

READ SalesFile AT END SET EndOfFile TO TRUE END-READ.$$$. This program is the solution to one of the programming exercises. 22. 03 Surname PIC X(8). Begin. FILE-CONTROL. 01 TYPE IS PAGE FOOTING. READ SalesFile AT END SET EndOfFile TO TRUE END-READ. PERFORM PrintSalaryReport UNTIL EndOfFile.03 COLUMN 37 PIC 9 SOURCE SalesPersonNum. 88 EndOfStudentFile VALUE HIGH-VALUES. 02 StudentName.99 SUM ValueOfSale. OPEN OUTPUT PrintFile. DATA DIVISION.Michael Coughlan". 03 SMS COLUMN 45 PIC $$$$$. INPUT-OUTPUT SECTION. PROCEDURE DIVISION. 01 StudentDetails. This program counts the number of students born in each month and displays the result. 03 COLUMN 45 PIC X(6) VALUE "Page :". The program uses a pre-filled table of month names. FD StudentFile. 03 COLUMN 52 PIC Z9 SOURCE PAGE-COUNTER. PrintSalaryReport. Michael Coughlan. 03 COLUMN 1 PIC X(29) VALUE "Programmer . Read the program specification first. CLOSE SalesFile. 02 StudentId PIC 9(7). 03 Initials PIC XX. PrintFile. ENVIRONMENT DIVISION. MonthTable. IDENTIFICATION DIVISION. GENERATE DetailLine. 03 COLUMN 43 PIC X VALUE "=". SELECT StudentFile ASSIGN TO "STUDENTS.DAT" ORGANIZATION IS LINE SEQUENTIAL. . STOP RUN. PROGRAM-ID. INITIATE SalesReport. OPEN INPUT SalesFile. FILE SECTION. 02 LINE IS 53. * This program counts the number of students born in each month and * displays the result. AUTHOR. TERMINATE SalesReport.

03 MOBirth PIC 9(2). PROCEDURE DIVISION. 03 FILLER PIC X(18) VALUE "March April". A program is required which will process the Students File (Students. 03 FILLER PIC X(18) VALUE "SeptemberOctober". 01 DisplayLine. 01 HeadingLine PIC X(19) VALUE " Month StudCount". . WORKING-STORAGE SECTION.02 DateOfBirth. 02 PrnStudentCount PIC ZZ9. 02 PrnMonth PIC X(9). 03 FILLER PIC X(18) VALUE "January February". 03 FILLER PIC X(18) VALUE "May June". 03 YOBirth PIC 9(4). 02 TableValues. Program Specification. 01 MonthTable. CLOSE StudentFile STOP RUN. Begin. 03 FILLER PIC X(18) VALUE "July August". 03 FILLER PIC X(18) VALUE "November December". 02 FILLER PIC X(4) VALUE SPACES. 02 FILLERh REDEFINES TableValues. 03 Month OCCURS 12 TIMES PIC X(9). 01 MonthIdx PIC 999. 02 CourseCode PIC X(4). 03 DOBirth PIC 9(2). OPEN INPUT StudentFile READ StudentFile AT END SET EndOfStudentFile TO TRUE END-READ PERFORM UNTIL EndOfStudentFile ADD 1 TO MonthCount(MOBirth) READ StudentFile AT END SET EndOfStudentFile TO TRUE END-READ END-PERFORM DISPLAY HeadingLine PERFORM VARYING MonthIdx FROM 1 BY 1 UNTIL MonthIdx > 12 MOVE Month(MonthIdx) TO PrnMonth MOVE MonthCount(MonthIdx) TO PrnStudentCount DISPLAY DisplayLine END-PERFORM. 01 MonthCount OCCURS 12 TIMES PIC 999 VALUE ZEROS.Dat) and will count the number students born on each month of the year and will display the results to January to December order. 02 Gender PIC X.

The main problem that we face here is that the results must be displayed in MonthName order but the file is held in StudentId order. Each record of the students file contains the following items.Download Students. A tables based solution is a good idea here. Sample Solution When you have written your program and have compiled it and have it working correctly you may wish to compare it with this sample solution. Suggested Approaches. Field Type Length Value Student Id 9 7 0-9999999 Student Name Group Surname X 8 Initials X 2 DateOfBirth Group Year 9 4 0000-9999 Month 9 2 01-12 Day 9 2 01-31 Course Code X 4 Gender X 1 M/F Example Run Month StudCount January 2 February 2 March 3 April 4 May 1 June 0 July 2 August 1 September 4 October 5 November 3 December 5 Use Edited picture clauses and the zero suppression symbol to produce the output as shown.Dat and save it to the WorkArea directory on the drive D: Student File Description The Students File is a sequential file held in ascending StudentId order. SCREEN SECTION . We can use the month number as a direct index into the month table.

001900 05 LINE 7 COLUMN 11 PIC 99999 FROM CYYDDD HIGHLIGHT. Uses 000600* modular. 002500 05 LINE 11 COLUMN 17 PIC ZZ. 002800 05 LINE 24 COLUMN 2 VALUE 002900 'PRESS <Enter> TO CONTINUE OR "Q" TO END'. 000200 PROGRAM-ID. ID DIVISION. 03 BYY PIC 99. 002200 05 LINE 9 COLUMN 34 PIC 99999 TO BYYDDD AUTO. 001600 01 FORM-SCREEN BACKGROUND IS BLUE FOREGROUND IS WHITE. 03 CYY PIC 99. 01 BYYDDD. 004200 STOP RUN.ZZZ FROM DAYSLIVED HIGHLIGHT. 002300 01 OUTPUT-SCREEN. PROGRAM-ID.24. Adapted to RM/COBOL-85 Version 5.Program 1. DATA DIVISION. 001500 SCREEN SECTION. 003300 PERFORM UNTIL FLAG = 'Q' OR 'q' 003400 ACCEPT CYYDDD FROM DAY 003500 DISPLAY FORM-SCREEN 003600 ACCEPT INPUT-SCREEN 003700 COMPUTE DAYSLIVED = (CYY . 002700 01 END-SCREEN. 01 CYYDDD. ID DIVISION.' LINE 11 COLUMN 24. PRICE.BDDD 003800 DISPLAY OUTPUT-SCREEN 003900 DISPLAY END-SCREEN 004000 ACCEPT FLAG LINE 24 COLUMN 47 PROMPT ' ' REVERSED 004100 END-PERFORM. 002400 05 'YOU HAVE LIVED' LINE 11 COLUMN 2. 002100 01 INPUT-SCREEN. CHANJIN CHUNG. 003200 MAIN-PARA. 003000 003100 PROCEDURE DIVISION. 001700 05 BLANK SCREEN.BYY) * 365 + CDDD . 03 CDDD PIC 999. 002600 05 'DAYS. WORKING-STORAGE SECTION. AUTHOR. 001400 01 FLAG PIC X VALUE 'C'. 03 BDDD PIC 999. structured logic and functionality can readily . 001800 05 'TODAY IS ' LINE 7 COLUMN 2 HIGHLIGHT. SCREEN1. Program 2. 000300*============================================================== 000400* Demonstrates use of SCREEN SECTION adapting CALC3 to this 000500* technique. 01 DAYSLIVED PIC 9(5). 002000 05 'ENTER YOUR DOB IN YYDDD FORMAT: ' LINE 9 COLUMN 2.

ZZ9 FROM WS-QUANTITY 005600 REVERSED. 003700 05 LINE 7 COLUMN 21 VALUE ' ' REVERSED.99. 001600 01 WS-GRAND-TOTAL PIC S9(8)V99. 001200 01 WS-QUANTITY PIC 9(5). 004100 05 LINE 14 COLUMN 43 VALUE ' ' REVERSED. 003500*-------------------------------------------------------------003600 01 CALC5-CLEAR-OFF-SCREEN. 001700 01 WS-RESPONSE PIC X VALUE 'C'. 003900 05 LINE 7 COLUMN 43 VALUE ' ' REVERSED. 002400 05 'Quantity Price Total' 002500 LINE 5 COLUMN 20 HIGHLIGHT.TO WS-PRICE 005100 REVERSED NO HIGHLIGHT REQUIRED. 004600 004700 05 LINE 7 COLUMN 21 PIC ZZ. 001400 01 WS-TOTAL PIC S9(7)V99. 002000*-------------------------------------------------------------002100 01 CALC5-LABEL-SCREEN BACKGROUND IS BLUE 002200 FOREGROUND IS WHITE. .000700* be expanded. 005400 005500 05 LINE 7 COLUMN 21 PIC ZZ. 001100 WORKING-STORAGE SECTION. 004200 05 LINE 24 COLUMN 1 VALUE 004300 'Enter data and press <Tab> or <Enter>' BLANK LINE. 001500 01 WS-TAX-AMOUNT PIC S9(7)V99. 003200 003300 05 'Grand Total' 003400 LINE 14 COLUMN 30 HIGHLIGHT. 003800 05 LINE 7 COLUMN 33 VALUE ' ' REVERSED. 004400*-------------------------------------------------------------004500 01 CALC5-INPUT-SCREEN. 000800*============================================================== 000900 001000 DATA DIVISION. 005200*-------------------------------------------------------------005300 01 CALC5-OUTPUT-SCREEN. 004000 05 LINE 10 COLUMN 43 VALUE ' ' REVERSED. 002900 003000 05 '=============' 003100 LINE 12 COLUMN 43 HIGHLIGHT. 001800* 001900 SCREEN SECTION.ZZ9 TO WS-QUANTITY 004800 REVERSED NO HIGHLIGHT REQUIRED AUTO. 002600 002700 05 '6% Tax' 002800 LINE 10 COLUMN 35 HIGHLIGHT. 002300 05 BLANK SCREEN. 001300 01 WS-PRICE PIC S99V99. 004900 005000 05 LINE 7 COLUMN 33 PIC Z9.

99FROM WS-PRICE 005900 REVERSED. .ZZZ. 007200*-------------------------------------------------------------007300 01 CALC5-ENDING-SCREEN BACKGROUND IS BLACK 007400 FOREGROUND IS BLUE.ZZZ. 009500 009600*============================================================== 009700 009800 1000-BOJ. 009300 PERFORM 3000-EOJ. 006900 007000 05 LINE 24 COLUMN 1 VALUE 007100 'Press <Enter> to continue or "Q" to end' BLANK LINE. 008400 05 LINE 11 COLUMN 10 008500 VALUE '********************************'.ZZ9.99.ZZZ. 006600 006700 05 LINE 14 COLUMN 43 PIC ZZ. 007800 05 LINE 8 COLUMN 10 007900 VALUE '* *'.FROM WS-TAX-AMOUNT 006500 REVERSED. 008200 05 LINE 10 COLUMN 10 008300 VALUE '* *'. 009000 PERFORM 1000-BOJ. 007600 05 LINE 7 COLUMN 10 007700 VALUE '********************************'. 009400 STOP RUN. 008600*============================================================== 008700* 008800 PROCEDURE DIVISION.005700 005800 05 LINE 7 COLUMN 33 PIC ZZ. 007500 05 BLANK SCREEN.ZZ9. 010300 010400*============================================================== 010500 010600 2000-PROCESS. 008900 0000-MAINLINE. 006300 006400 05 LINE 10 COLUMN 43 PIC ZZ. 009900*--------------------------------------------010000* Clear screen and display labels highlighted 010100*--------------------------------------------010200 DISPLAY CALC5-LABEL-SCREEN.99006800 FROM WS-GRAND-TOTAL REVERSED. 008000 05 LINE 9 COLUMN 10 008100 VALUE '* *'. 009100 PERFORM 2000-PROCESS 009200 UNTIL WS-RESPONSE = 'Q' OR 'q'. 006000 006100 05 LINE 7 COLUMN 43 PIC ZZ.FROM WS-TOTAL 006200 REVERSED.ZZ9.99.

011700 COMPUTE WS-TAX-AMOUNT = WS-TOTAL * . . 011100 011200*--------------------------------------------------011300* Accept the data. 011600 COMPUTE WS-TOTAL = WS-QUANTITY * WS-PRICE. 013600 DISPLAY 'CALC5 ENDED AS REQUESTED' 013700 LINE 9 COLUMN 14 013800 BLINK 013900 CONTROL "FCOLOR=RED". 011800 COMPUTE WS-GRAND-TOTAL = WS-TOTAL + WS-TAX-AMOUNT. display sign-off message. do computations. present results 011400*--------------------------------------------------011500 ACCEPT CALC5-INPUT-SCREEN. 011900 DISPLAY CALC5-OUTPUT-SCREEN. and 013300* leave the system set to normal screen colors 013400*------------------------------------------------013500 DISPLAY CALC5-ENDING-SCREEN.010700*-----------------------------------------------------010800* Blank out the data fields from any prior transaction 010900*-----------------------------------------------------011000 DISPLAY CALC5-CLEAR-OFF-SCREEN. 014000 DISPLAY ' ' 014100 LINE 1 COLUMN 1 014200 CONTROL "FCOLOR=WHITE". 012000 012100*--------------------------------------012200* Ask if user wants to continue or quit 012300*--------------------------------------012400 ACCEPT WS-RESPONSE 012500 LINE 24 COLUMN 60 012600 PROMPT '_' REVERSED NO BEEP LOW. 013100*------------------------------------------------013200* Clear the screen.06. 012700 012800*============================================================== 012900 013000 3000-EOJ.

Sign up to vote on this title
UsefulNot useful