You are on page 1of 11

COBOL CASE STUDY COBOL Programming Fundamentals - Ver. 1.

COBOL CASE STUDY


COBOL PROGRAMMING FUNDAMENTALS
DOCUMENT CONTROL

Version 1.0

Date 10 July 2009

Author TCS IBM COE

Comments Initial

For Internal Use Only

Page 2 of 11

COBOL CASE STUDY COBOL Programming Fundamentals - Ver. 1.0

COBOL PROGRAMMING FUNDAMENTALS CASE STUDY 1. The Requirement Statement:


This Case Study covers numerous small program statements which need to be coded and executed. This Case Study helps you acquire fundamental knowledge on COBOL programming and the different syntax and semantics involved in it. Make use of the COBOL class room presentation for syntax of the commonly used COBOL statements.

2. Problem Statements
Exercise 1 ACCEPT from SYSIN and DISPLAY statements:In the first exercise accept a number WS-A using the COBOL ACCEPT FROM SYSIN statement. Consider the WS-A to be a value which represents temperature in Degree Celsius, convert it into Fahrenheit scale and store it into WS-B. Display WS-B using the DISPLAY statement. Also use third variable WS-DATE to get the date from SYSTEM and display it using the DISPLAY statement. Refer to Appendix-A for the source code.

Exercise 2 Different NUMBER CLAUSES:In this exercise DECLARE the variables in the format specified and perform the different formatting on the numbers as specified below. The following are the various Input variables.

01 01 01 01 01 01 01 01 01 01 01 01 01

WS-COMP-3 PIC S9(5)V9(3) USAGE IS COMP-3 WS-COMP-1 PIC 9(5)V9(3) USAGE IS COMP-3. WS-COMP-2 PIC 9(5)V9(3) USAGE IS COMP-3. WS-RESULT PIC 9(10). WS-COMP PIC 9(3) COMP. WS-COMP-SYNC PIC 9(3) COMP SYNC. WS-JUST-RGT PIC X(5) JUST RIGHT. WS-N PIC 9(5). WS-A-REDEF REDEFINES WS-N PIC X(5). WS-SGN-TRL-SEP PIC S9(3) SIGN TRAILING SEPARATE VALUE -135. WS-SGN-LED-SEP PIC S9(3) SIGN LEADING SEPARATE VALUE -135. WS-SGN-TRL PIC S9(3) SIGN TRAILING VALUE -135. WS-SGN-LED PIC S9(3) SIGN LEADING VALUE -135.

For Internal Use Only

Page 3 of 11

COBOL CASE STUDY COBOL Programming Fundamentals - Ver. 1.0

1.) Move the value -5555.77 to WS-COMP-3 and DISPLAY the result. 2.) Move the value 100 to WS-COMP-1 and WS-COMP-2 and MULTIPLY the two values and store the result in WS-RESULT. DISPLAY WS-COMP-1, WS-COMP-2 and WS-RESULT. 3.) Move the value 865 to WS-COMP and DISPLAY that. 4.) Move the value 657 to WS-COMP-SYNC and DISPLAY that. 5.) Move the value SGN to WS-JUST-RGT and DISPLAY that. 6.) Move the value 46858 to WS-N and DISPLAY the variable WS-A-REDEF 7.) DISPLAY the variables WS-SGN-TRL-SEP, WS-SGN-TRL, WS-SGN-LED-SEP and WSSGN-LED. (Note the values for these variables are declared in the VALUE CLAUSE)

Exercise 3 Different PICTURE CLAUSE Editing:In this exercise DECLARE the variables in the format specified and perform the different formatting on the numbers as specified below.

01 01 01 01 01 01 01 01 01 01 01 01 01

WS-EDT-Z WS-EDT-AST WS-EDT-CUR WS-EDT-MIN WS-EDT-PLS WS-EDT-CR WS-EDT-DB WS-EDT-PER WS-EDT-COM WS-EDT-BLA WS-EDT-ZER WS-EDT-SLA WS-EDT-WHE

PIC ZZZV99. PIC **999. PIC $ZZ999. PIC -ZZZV99. PIC +ZZZV99. PIC ZZZCR. PIC ZZZ9V99DB. PIC $ZZZ9.99. PIC ZZ,Z9.99. PIC 99B99B99. PIC 09990. PIC 999/999/99. PIC 999.99 BLANK WHEN ZERO.

1. ZERO SUPPRESSION 2. ASTERIK 3. CURRENCY 4. MINUS SIGN 5. PLUS SIGN 6. CREDIT SIGN 7. DEBIT SIGN 8. PERIOD 9. COMMA 10. BLANK INSERTION 11. ZERO INSERTION 12. / INSERTION 13. BLANK WHEN ZERO

: Move 38.4 to WS-EDT-Z and DISPLAY it. : Move 04678 to WS-EDT-AST and DISPLAY it. : Move 32664 to WS-EDT-CUR and DISPLAY it. : Move -46.52 to WS-EDT-MIN and DISPLAY it. : Move -46.52 to WS-EDT-PLS and DISPLAY it. : Move -42 to WS-EDT-CR and DISPLAY it. : Move -152.25 to WS-EDT-DB and DISPLAY it. : Move 0285 to WS-EDT-PER and DISPLAY it. : Move 123.45 to WS-EDT-COM and DISPLAY it. : Move 150182 to WS-EDT-BLA and DISPLAY it. : Move 456 to WS-EDT-ZER and DISPLAY it. : Move 3245 to WS-EDT-SLA and DISPLAY it. : Move 0 to WS-EDT-WHE and DISPLAY it.

For Internal Use Only

Page 4 of 11

COBOL CASE STUDY COBOL Programming Fundamentals - Ver. 1.0

Exercise 4 ADD Clause:In this exercise we will see how to use the ADD clause in COBOL and to perform arithmetic operations on the numbers as specified below.

01 01 01 01 01

WS-A WS-B WS-C WS-D WS-E

PIC 99 PIC 99 PIC 999 PIC 999 PIC 999

VALUE 12. VALUE 10. VALUE 13. VALUE 15. VALUE 20.

1.) 2.) 3.) 4.) 5.)

Add WS-A and WS-B and store the result in WS-B also display it. Add WS-A, WS-B, WS-C and WS-D and store the result in WS-D also display it. Add 15, WS-A and WS-B and store the result in WS-B also display it. Add WS-A and WS-B and store the result in WS-C also display it (use GIVING). Add WS-A and WS-B and store the result in WS-C, WS-D and WS-E also display it (use GIVING). Note: The values of the variables will change after each Arithmetic operation.

Exercise 5 SUBTRACT Clause:In this exercise we will see how to use the ADD clause in COBOL and to perform arithmetic operations on the numbers as specified below.

01 01 01 01 01

WS-A WS-B WS-C WS-D WS-E

PIC 99. PIC 99. PIC 999. PIC 999. PIC 999.

1.) 2.) 3.) 4.) 5.) 6.)

Move 10 to WS-A and 5 to WS-B Subtract WS-A from WS-B and store the result in WS-B also display it. Move 10 to WS-A, 5 to WS-B and 6 to WS-C Subtract WS-A and WS-B from WS-C and store the result in WS-C also display it. Move 10 to WS-A, 5 to WS-B, 6 to WS-C and 1 to WS-D Subtract WS-A and WS-B from WS-C and store the result in WS-D also display it (use GIVING). 7.) Move 10 to WS-A and 5 to WS-B 8.) Subtract 15 from WS-A and WS-B and display the result.

For Internal Use Only

Page 5 of 11

COBOL CASE STUDY COBOL Programming Fundamentals - Ver. 1.0

Exercise 6 MOVE Clause:In this exercise we will see how to use the MOVE clause in COBOL works. This is a very important clause as values can be transferred between variables only using MOVE. The Input declarations are as specified below. Perform the various MOVE statements mentioned.

01 01 01 01

WS-A WS-B WS-C WS-D

PIC 99V9. PIC 99.9. PIC X(5). PIC S9(2)V9

USAGE COMP-3.

1.) 2.) 3.) 4.) 5.)

Move 99.9 to WS-A (Use this syntax MOVE 99.9 TO WS-A) and display it. Move 99.9 to WS-B (Use this syntax MOVE 99.9 TO WS-B) and display it. Move 99.9 to WS-A (Use this syntax MOVE 99.9 TO WS-A) and display it. Move 99.9 to WS-B (Use this syntax MOVE 99.9 TO WS-B) and display it. Perform an arithmetic operation of your choice using WS-B. (Ex: ADD WS-A TO WS-B) 6.) Move WS-B to WS-D 7.) Move WS-B to WS-C and Display it. 8.) Perform an arithmetic operation of your choice using WS-D and WS-A. (Ex: ADD WS-A TO WS-D)

Exercise 7 CONDITIONAL Operation:In this exercise we will see how to use the different CONDITIONAL operations in COBOL. This is a mostly used during Validations performed on variables. Refer to the source code available in Appendix-B it explains most of the CONDITIONAL Operations execute the code and get familiarized of the different executions.

Exercise 8 INSPECT Clause:In this exercise we will see how to use the INSPECT operations in COBOL. This is used when you want to do manipulations to text. INSPECT is the keyword which is used to do the manipulation. Use the variables mentioned below and do the manipulations given below.
01 01 WS-A WS-TALLYCOUNT PIC X(7) VALUE 'AEERIE.'. PIC 9(1) VALUE 0.

1.) Find the number of letter E between first occurrence of letter A and first occurrence of letter . (Use TALLYING, INSPECT, INITIAL, BEOFRE and AFTER keywords) 2.) Replace all the Leading letter E by A. (Use INSPECT and LEADING)

For Internal Use Only

Page 6 of 11

COBOL CASE STUDY COBOL Programming Fundamentals - Ver. 1.0

Exercise 9 STRING and UNSTRING Clause:In this exercise we will see how to use the STRING and UNSTRING operations in COBOL. This is used when you want to do manipulations to text. STRING and UNSTRING is the keyword which is used to do the manipulation. Use the variables mentioned below and do the manipulations given below.

01 01 01 01 01 01 01 01 01 01

WS-A WS-B WS-C WS-D WS-E WS-F WS-G WS-H WS-CHAR-POS WS-T

PIC X(4) VALUE 'TATA'. PIC X(6) VALUE 'BIR LA '. PIC X(11) VALUE 'CEO,CFO,CTO'. PIC X(19) VALUE SPACES. PIC X(38) VALUE SPACES. PIC X(38) VALUE SPACES. PIC X(38) VALUE SPACES. PIC X(38) VALUE 'TATA.CONSULTANCY.SERVICES.'. PIC 9(2) VALUE 1. PIC 9(2) VALUE 1.

1.) Display WS-A, WS-B, WS-C and WS-D. 2.) Filter out the word CEO from WS-C using DELIMITED keyword. Use POINTER keyword to find the position of the DELIMITER. 3.) Filter out the words TATA, CONSULTANCY and SERVICES in a single statement using UNSTRING verb.

Exercise 10 OCCURS and PERFORM Clause:In this exercise we will see how to use the OCCURS Clause and PERFORM operation in COBOL. OCCURS Clause is used to declare an array of 10 elements. Code a COBOL program to declare an array using the OCCURS clause and accept the values from SYSIN for the array using the ACCEPT verb. Also display the values that are present in the array using the DISPLAY verb. Usage of PERFORM operation will help in accepting and displaying the values for the array. Syntax for OCCURS and PERFORM:
01 WS-LIST-OF-NUM. 05 WS-NUMBER SUB-SCRIPT PIC 9(3) PIC 99. OCCURS 10 TIMES.

77

PERFORM VARYING SUB-SCRIPT FROM 1 BY 1 UNTIL SUB-SCRIPT > 10 ACCEPT WS-NUMBER(SUB-SCRIPT) FROM SYSIN END-PERFORM.

For Internal Use Only

Page 7 of 11

COBOL CASE STUDY COBOL Programming Fundamentals - Ver. 1.0

B. Process Steps for Executing COBOL Program


Datasets Allocation Execute the following steps to allocate the given datasets (All the datasets except the Load Library should be created with RECFM = FB, LRECL=80, BLKSIZE=800, DIRBLOCKS = 3. Allocate a space of primary being 3 cylinders for source and Load library and 20 tracks for JCL and Copy library) a. Create the following JCL Library which should hold 2 members. (Compile and Run JCL). <userid>.COBOL.JCL b. Create the following Source Library to hold the 4 programs in two different members inside the PDS. <userid>.COBOL.SRCE c. Create the following COPY Library to hold copy books. <userid>.COBOL.COPYLIB d. Create the Load Library Dataset to hold the Load Members. The attributes of the Load Library should be RECFM=U, RECLEN=0, BLKSIZE=32760, DIRBLK=2, PRIMARYALLOC=3CYL. <userid>.COBOL.LOADLIB Steps for Execution a. Compile the written program with the Compile JCL available in TRG1T.ILP.COBOL (COBCOMP). Ensure that the format of the JOB-CARD, name of PGM-ID, path to LOADLIB and DD card for any files used in the programs are changed. The Valid Return Code from Compilation is 0 or 4. b. Create a Run JCL for your program, from the Run JCL available in TRG1T.ILP.COBOL (RUNJCL). Ensure that the format of the JOB-CARD, name of PGM-ID, path to LOADLIB and DD card for any files used in the programs are changed. The Valid Return Code from Run JCL is 0. Execute the program and check the Output displays in the Spool.

For Internal Use Only

Page 8 of 11

COBOL CASE STUDY COBOL Programming Fundamentals - Ver. 1.0

APPENDIX-A
ACCEPT FROM SYSIN AND DISPLAY STATEMENTS
IDENTIFICATION DIVISION. PROGRAM-ID. xxxxxxxx. ****************************************************************** * PROGRAM DESCRIPTION * ****************************************************************** * ENTER THE PROGRAM NAME & PROGRAM DESCRIPTION * ****************************************************************** ENVIRONMENT DIVISION. ****************************************************************** * DATA DIVISION * ****************************************************************** DATA DIVISION. ****************************************************************** * WORKING STORAGE SECTION * ****************************************************************** WORKING-STORAGE SECTION. * 01 WS-A PIC 99. 01 WS-B PIC 999. 01 WS-DATE PIC X(10) VALUE SPACES. *----------------------------------------------------------------* * DEFINE THE WORKING STORAGE VARIABLES NEEDED FOR THE PROGRAM * *----------------------------------------------------------------* ****************************************************************** * PROCEDURE SECTION * ****************************************************************** *ACCEPT STATEMENT DISPLAY 'SUPPORTS ACCEPT STATEMENT' DISPLAY 'COBOL - ACCEPT WS-A FROM SYSIN' ACCEPT WS-A FROM SYSIN DISPLAY 'WS-A:' WS-A ACCEPT WS-DATE FROM DATE DISPLAY WS-DATE. * *COMPUTE STATEMENT DISPLAY ' ' DISPLAY 'SUPPORTS COMPUTE STATEMENT' DISPLAY 'COBOL - COMPUTE WS-B = 1.8 * WS-A + 32' COMPUTE WS-B = 1.8 * WS-A + 32 DISPLAY 'WS-B:'WS-B STOP RUN. *----------------------------------------------------------------*

For Internal Use Only

Page 9 of 11

COBOL CASE STUDY COBOL Programming Fundamentals - Ver. 1.0

APPENDIX-B
CONDITIONAL OPERATIONS IN COBOL
IDENTIFICATION DIVISION. PROGRAM-ID.TCS. * PROGRAM IS TO CHECK SIGN, CLASS, CONDITION NAME, NEGATED, * COMPOUND CONDITION ENVIRONMENT DIVISION. DATA DIVISION. WORKING-STORAGE SECTION. * 77 WS-BALANCE PIC 9(2). 77 WS-ALPHA PIC A(2). 77 WS-ALPHANUM PIC X(2). 77 WS-NUM PIC 9(2). 77 MARITAL-STATUS PIC 9. 88 SINGLE VALUE 0. 88 MARRIED VALUE 1. 88 WIDOWED VALUE 2. 77 WS-AGE PIC 9(2). PROCEDURE DIVISION. * * SIGN CONDITION DISPLAY 'SUPPORTS SIGN CONDITION' MOVE 90 TO WS-BALANCE IF WS-BALANCE IS POSITIVE DISPLAY 'WS-BALANCE=' WS-BALANCE ' IS POSITIVE' END-IF MOVE -1 TO WS-BALANCE IF WS-BALANCE IS NEGATIVE DISPLAY 'WS-BALANCE=' WS-BALANCE ' IS NEGATIVE' END-IF MOVE 0 TO WS-BALANCE IF WS-BALANCE IS ZERO DISPLAY 'WS-BALANCE=' WS-BALANCE ' IS ZERO' END-IF * * CLASS CONDITION DISPLAY ' ' DISPLAY 'CLASS CONDITION SUPPORETD' MOVE 01 TO WS-NUM IF WS-NUM IS NUMERIC DISPLAY 'WS-NUM=' WS-NUM ' IS NUMERIC' END-IF MOVE 'XX' TO WS-ALPHANUM IF WS-ALPHANUM IS NUMERIC DISPLAY 'WS-ALPHANUM=' WS-ALPHANUM ' IS NUMERIC' ELSE DISPLAY 'WS-ALPHANUM=' WS-ALPHANUM ' IS ALPHABETIC' END-IF MOVE '01' TO WS-ALPHANUM IF WS-ALPHANUM IS NUMERIC DISPLAY 'WS-ALPHANUM=' WS-ALPHANUM ' IS NUMERIC' ELSE DISPLAY 'WS-ALPHANUM=' WS-ALPHANUM ' IS ALPHABETIC' END-IF MOVE 'AA' TO WS-ALPHA IF WS-ALPHA IS ALPHABETIC DISPLAY 'WS-APLHA=' WS-ALPHA ' IS ALPHABETIC' END-IF

For Internal Use Only

Page 10 of 11

COBOL CASE STUDY COBOL Programming Fundamentals - Ver. 1.0

* CONDITION NAME CONDITION DISPLAY ' ' DISPLAY 'CONDITION NAME CONDITION SUPPORETD' MOVE 1 TO MARITAL-STATUS IF MARRIED DISPLAY 'IF MARRIED, MARITAL-STATUS IS ' MARITAL-STATUS END-IF * * NEGATED SIMPLE CONDITION DISPLAY ' ' DISPLAY 'NEGATED SIMPLE CONDITION SUPPORETD' MOVE 0 TO MARITAL-STATUS IF NOT MARRIED DISPLAY 'IF SINGLE, MARITAL-STATUS IS ' MARITAL-STATUS END-IF * * COMPOUND CONDITION DISPLAY ' ' DISPLAY 'COMPOUND CONDITION SUPPORETD' MOVE 20 TO WS-AGE IF WS-AGE < 30 AND WS-AGE > 15 DISPLAY 'WS-AGE > 15 AND < 30:'WS-AGE END-IF STOP RUN.

For Internal Use Only

Page 11 of 11

You might also like