Академический Документы
Профессиональный Документы
Культура Документы
Training Material
www.mainframe230.blogspot.com
COBOL Training
Table of Contents
5 6
6 7 7 7 8
3. IDENTIFICATION DIVISION
3.1. SYNTAX OF IDENTIFICATION DIVISION
9
9
4. ENVIRONMENT DIVISION
4.1. CONFIGURATION SECTION 4.1.1. SOURCE-COMPUTER 4.1.2. OBJECT COMPUTER 4.1.3. SPECIAL-NAMES 4.2. INPUT-OUTPUT SECTION 4.2.1. FILE-CONTROL 4.2.2. I-O CONTROL
10
10 10 10 10 11 11 11
5. DATA DIVISION
5.1. LITERALS AND FIGURATIVE CONSTANTS 5.1.1. NUMERIC LITERAL 5.1.2. NON-NUMERIC LITERAL 5.1.3. LIST OF FIGURATIVE CONSTANTS 5.2. Data Description 5.2.1. LEVEL NUMBERS 5.2.2. Data Names 5.2.3. CLAUSE 5.2.4. CONDITION NAMES
13
14 14 15 15 15 15 16 18 23
6. PROCEDURE DIVISION
6.1. STATEMENTS IN PROCEDURE DIVISION 6.2. Data Movement 6.2.1. MOVE 6.3. ARITHMETIC VERBS 6.3.1. ADD 6.3.2. SUBTRACT 6.3.3. MULTIPLY 6.3.4. DIVIDE 6.3.5. ROUNDED OPTION 6.3.6. ON SIZE ERROR 6.3.7. COMPUTE 6.3.8. ARITHMETIC OPERATORS 6.4. Miscellaneous 6.4.1. ACCEPT
24
25 26 26 27 27 28 30 31 33 33 33 33 34 34
www.mainframe230.blogspot.com
Page 2
date:16.05.2010
COBOL Training 6.4.2. DISPLAY 6.4.3. CONTINUE 6.4.4. STOP RUN 6.4.5. EXIT 6.4.6. INITIALIZE 6.5. CONDITIONAL AND UNCONDITIONAL TRANSFER 6.5.1. SIMPLE IF STATEMENT 6.5.2. NESTED IF 6.5.3. EVALUATE STATEMENT 6.5.4. GO TO STATEMENT 6.5.5. GO TO WITH DEPENDING 6.5.6. ALTER 6.6. PERFORM STATEMENTS 6.6.1. Basic Perform 6.6.2. PERFORM with TIMES phrase 6.6.3. PERFORM with UNTIL phrase 6.6.4. PERFORM with VARYING phrase 6.7. Conditional expressions 6.7.1. RELATIONAL CONDITION 6.7.2. SIGN CONDITION 6.7.3. CLASS CONDITION 6.7.4. CONDITION-NAME CONDITION 6.7.5. NEGATED SIMPLE CONDITION 6.7.6. COMPOUND CONDITION 34 35 35 35 35 35 35 36 36 37 37 38 38 38 39 39 40 40 41 41 41 41 42 42
7. FILE HANDLING
7.1. Sequential Files 7.2. Indexed Files 7.3. Direct Access Files 7.4.STEPS INVOLVED IN FILE HANDLING 7.4.1. EXAMPLE 7.5. SELECT Statement 7.5.1. ORGANIZATION CLAUSE 7.5.2. ACCESS MODE CLAUSE 7.5.3. RECORD KEY PHRASE 7.5.4. ALTERNATE KEY PHRASE 7.6. FD entry 7.6.1. BLOCK CONTAINS / RECORD CONTAINS PHRASE 7.6.2. RECORDING MODE CLAUSE 7.7. INPUT / OUTPUT STATEMENTS 7.8. VSAM DATA SET 7.8.1. VSAM DATASETS AND ACCESS MODES 7.8.2. ENVIRONMENT DIVISION ENTRIES FOR VSAM FILES 7.8.3. COBOL STATEMENTS USABLE WITH SEQUENTIAL FILES 7.8.4. COBOL STATEMENTS WITH INDEXED FILES (KSDS) AND RELATIVE (RRDS) 7.8.5. PROCEDURE DIVISION STATEMENTS TO UPDATE VSAM FILES 7.8.6. ERROR PROCESSING FOR VSAM 7.8.7. APPLICATIONS BEST SUITED TO VSAM DATASETS
43
43 43 43 43 44 46 47 47 48 48 48 49 50 50 54 55 55 56 56 59 60 60
8. SUBPROGRAMS
8.1. LINKAGE SECTION 8.1.1. STATIC / DYNAMIC CALLS 8.1.2. PASSING RETURN CODE INFORMATION
61
62 63 63
www.mainframe230.blogspot.com
Page 3
date:16.05.2010
COBOL Training
65
65 66 69 71
72 78
78 78 78 79
80 82
82 83 83 84 84 84 85
www.mainframe230.blogspot.com
Page 4
date:16.05.2010
COBOL Training
1. OBJECTIVES
The main objective after the completion of this course, is that the students should be able to: Understand the standard format of the COBOL program. Write COBOL programs which uses VSAM or Non VSAM files. Submit jobs to Compile and Link-edit COBOL source codes. Write programs using modular design techniques.
www.mainframe230.blogspot.com
Page 5
date:16.05.2010
COBOL Training
2. INTRODUCTION TO COBOL
COBOL => COMMON BUSINESS ORIENTED LANGUAGE Cobol can be referred to as: High-Level Programming language oriented towards business applications. A procedure oriented language. A Procedure is a module having its own functionality. A Problem may be segmented into multiple sub-functionalities. Each sub-functionality is written as a Paragraph in Procedure Division and executed in a logical sequence as mentioned. Problem Oriented and Machine independent
History of COBOL
Developed in 1959 as standard language to meet needs of business Committee to develop language convened by Department of Defense Included representatives from academia, business, computer manufacturers
www.mainframe230.blogspot.com
Page 6
date:16.05.2010
COBOL Training
Sentence is a sequence of one or more statements terminated by period Paragraph consists of one or more sentences Section consists of one or more paragraphs Division consists of one or more paragraphs or sections Program is made up of divisions
www.mainframe230.blogspot.com
Page 7
date:16.05.2010
COBOL Training
www.mainframe230.blogspot.com
Page 8
date:16.05.2010
COBOL Training
3. IDENTIFICATION DIVISION
The Identification Division is: Paragraph PROGRAM-ID followed by a user-defined name is compulsory. Other paragraphs are optional but essential for documentation. Should begin in Area A. Length of PROGRAM-ID differs from compiler to compiler. For example, in the case of IBM COBOL, it is 8 characters. First division of a Cobol program. Identifies program name to operating system. Provides documentation about program.
www.mainframe230.blogspot.com
Page 9
date:16.05.2010
COBOL Training
4. ENVIRONMENT DIVISION
It is a machine dependent division. The Division itself is optional but for file handling it is essential. Should begin in Area A. Division is terminated by a period. Describes the computer used, peripheral devices, etc. Contains two sections namely, CONFIGURATION and INPUT-OUTPUT.
4.1.1. SOURCE-COMPUTER
Used to specify the name of the Source Computer compiling the COBOL program. The With Debugging Mode option can be mentioned to enable the Compiler to include all the lines with D in the 7th column as syntax (Otherwise it is taken as comment). It can also include the debug Declaratives in PROCEDURE DIVISION.
4.1.3. SPECIAL-NAMES
Specifies collating sequence. Maps IBM specified environment names to user defined mnemonic names. Substitutes character for currency sign. Indicates that , and . are to be interchanged in the PIC clause. We can also specify symbolic characters, such as, PERCENTAGE-SIGN IS 38. Regardless of the number of entries in this paragraph, there should be only one period at the end.
www.mainframe230.blogspot.com
Page 10
date:16.05.2010
COBOL Training
4.2.1. FILE-CONTROL
Associates each file used in a COBOL program with an external ddname. Physical dataset is assigned in DD statement of JCL at execution time. Three formats for the FILE-CONTROL paragraph are: Sequential file entries Indexed file entries Relative file entries
Coding rules for FILE-CONTROL paragraph. SELECT clause must appear first Other clauses may appear in any order Each clause must start in Area B
Example:
IDENTIFICATION DIVISION. PROGRAM-ID. EDS001. AUTHOR. A343. ENVIRONMENT DIVISION. CONFIGURATION SECTION. SOURCE-COMPUTER. IBM-390 WITH DEBUGGING MODE. OBJECT-COMPUTER. IBM-390 PROGRAM COLLATING SEQUENCE IS A01. SPECIAL-NAMES. ALPHABET A01 IS "A" ALSO "a" "B" ALSO "b" ALPHABET A IS STANDARD-1 SYSIN IS PROGIN CURRENCY IS "@" DECIMAL-POINT IS COMMA. INPUT-OUTPUT SECTION. FILE-CONTROL SELECT OUTFILE ASSIGN TO DD1 ORGANIZATION IS SEQUENTIAL ACCESS MODE SEQUENTIAL
www.mainframe230.blogspot.com
Page 11
date:16.05.2010
COBOL Training
www.mainframe230.blogspot.com
Page 12
date:16.05.2010
COBOL Training
5. DATA DIVISION
Data division is used to define data needed to be accessed by the program. Describes input and output format of data in files. Defines any constants and work areas.
The three sections of this division are: FILE SECTION Describes Record Structure of Files Succeeds file section Used for intermediate storage Common practice to group related items together. Describes data made available from another program. WORKING-STORAGE SECTION
LINKAGE SECTION FILE SECTION File Description Entries Each file described with an FD (File Descriptor) sentence One FD for each SELECT statement in ENVIRONMENT DIVISION
RECORD CONTAINS integer-1 CHARACTERS BLOCK CONTAINS integer-2 RECORDS. File Description clauses LABEL RECORDS clause - Used if labels stored in file
www.mainframe230.blogspot.com
Page 13
date:16.05.2010
COBOL Training BLOCKS CONTAINS clause - Indicates blocking factor for disk files Both labels and blocking usually handled by operating systems so clauses will be omitted RECORD CONTAINS clause - Indicates size of each record (Optional but recommended since it provides check on record size) Defining a Record Each FD followed by record description entries for the file Record-name defined at the 01 level -Considered highest level of data Fields within a record defined at subordinate level with level numbers from 02 to 49
Example: 01 Employee-Rec-In. 05 Name-In 05 Annual-Salary-In 05 Job-Description-In WORKING-STORAGE SECTION Follows FILE SECTION Starts in Area A, ends with period All items must be defined at 01 level or in entries subordinate to 01 level entry Rules for user-defined data-names apply Elementary items: Must include PICTURE clause May be assigned initial value with VALUE clause
www.mainframe230.blogspot.com
Page 14
date:16.05.2010
COBOL Training
05
+ or - Sign, if used must be the left-most character Not more than one decimal point Decimal may not be the right most character Maximum of 18 digits.
TOTAL-MARK PIC 9(3) VALUE 100.
May consist of any character in the character set Must be enclosed in quotes A numeric literal in quotes is non-numeric Maximum length of 120 chars
COMPANY-NAME PIC X(3) VALUE EDS.
www.mainframe230.blogspot.com
Page 15
date:16.05.2010
COBOL Training 01 This level - number specifies the record itself. A level - 01 entry may be either a group item or an elementary item. It must begin in Area A. They may begin in Area A or B Group Level Data Names Will not have Picture Clause.
PIC PIC XXXX. A(30). PIC A. PIC A. PIC A.
02-49 These level numbers specify group and elementary items within a record.
66
RECORDI. 05 DN1... . 05 DN2... . 05 DN3... . 05 DN4... . DN6 RENAMES DN1 THROUGH DN3.
www.mainframe230.blogspot.com
Page 16
date:16.05.2010
COBOL Training
If Name is duplicated across Records, it has to be qualified as Identifier-name of Record Name. DISPLAY BALANCE OF STOCK-RECORD
Here balance is an identifier declared in Stock-record. Identify the valid data-names 1.Date-Of-Birth 2. Amount$Out 3. Section 4. My-Section 5. -First-Name 6. Last--Name 7. 98-6 8. Time out
www.mainframe230.blogspot.com
Page 17
date:16.05.2010
COBOL Training
5.2.2.1. FILLER
Whenever there is no need for specific reference to fields in a record structure, we name it as FILLER or F. This Data name is used wherever there is fixed information like SPACES Or Headings Are stored. Example
01 Detail-record. 05 05 05 05 05 05 05 05 FILLER NAME FILLER BASIC FILLER DA FILLER GROSS PIC PIC PIC PIC PIC PIC PIC PIC X(10) X(30) . X(10) VALUE SPACES. 9(05) . X(10) VALUE SPACES. 9(04) . X(10) 9(6). VALUE SPACES. VALUE SPACES.
5.2.3. CLAUSE
A Clause specifies certain characteristics of the data item being described. The detail of each Clause is explained below.
www.mainframe230.blogspot.com
Page 18
date:16.05.2010
COBOL Training
PERIOD( . ) COMMA( , ) BLANK( B ) ZERO( 0 ) SLASH( / ) BLANK WHEN ZERO (Inserts blanks when data value is zero)
www.mainframe230.blogspot.com
Page 19
date:16.05.2010
COBOL Training
bbbbbb
www.mainframe230.blogspot.com
Page 20
date:16.05.2010
COBOL Training
Depending on the size of the data item, it can be stored either in half word or full word Should be numeric only
01
WS - NUM
PIC
S9(004)
USAGE
COMP.
COMP-1 USAGE . One word in floating point form The number is represented in hexadecimal The picture clause cannot be specified Suitable for arithmetic operations
WS-NUM USAGE COMP-1.
COMP-2 USAGE. Same as COMP 1 except that data is represented internally in two words Increases the precision of the data Internal Representation In Packed Decimal Form Each digit and sign occupy 1/2 a byte The Hexadecimal number C or F denotes a positive sign The Hexadecimal number D denotes a negative sign Maximum precision is 18
COMP-3 USAGE.
www.mainframe230.blogspot.com
Page 21
date:16.05.2010
COBOL Training
Only for numeric elementary items Picture string should contain S Usage should be DISPLAY Default is TRAILING without separate character EXAMPLE PIC S9(3) S9(3) VALUE -243 -243 SIGN LEADING TRAILING REPRESENTATION K 4 3 2 4 L
WS - REC (1) = 13 BYTES (i.e. X(1) = 5 bytes, Y(1) = 8 bytes) Hence, 10 such group items (WS-REC) are allocated memory space contiguously.
www.mainframe230.blogspot.com
Page 22
date:16.05.2010
COBOL Training
10 ADD-LN-2 PIC X(60).
5.2.3.7. RENAMES
Regrouping of elementary data items in a Record. 01 W-RESPONSE 05 W-CHAR-123 05 W-CHAR-4 05 W-CHAR-56 ADD-RESPONSE VIEW-RESPONSES DELETE-RESPONSE PIC XXX. PIC X. PIC XX. RENAMES RENAMES RENAMES
66 66 66
Justified clause does not alter initial settings as determined by the value clause Justified clause must not be specified with level 66(RENAMES) and level 88(condition-names) clauses. The question of justification comes only when you move a smaller data item to a larger data item. Example would be a PIC X(5) field to a PIC X(20)
www.mainframe230.blogspot.com
Page 23
date:16.05.2010
COBOL Training
6. PROCEDURE DIVISION
Last division in a COBOL program Contains statements which specify the operations to be performed Contains the following structure
Division: A division is a block of code, usually containing one or more sections, that starts where the division name is encountered and ends with the beginning of the next division or with the end of the program text. Section: A section is a block of code usually containing one or more paragraphs. A section begins with the section name and ends where the next section name is encountered or where the program text ends. Example: SelectUnpaidBills SECTION. FILE SECTION. Paragraphs: A paragraph is a block of code made up of one or more sentences. A paragraph begins with the paragraph name and ends with the next paragraph or section name or the end of the program text. Example: PrintFinalTotals. PROGRAM-ID. Sentences and statements: A sentence consists of one or more statements and is terminated by a period. Example: MOVE .21 TO VatRate
MOVE 1235.76 TO ProductCost COMPUTE VatAmount = ProductCost * VatRate.
A statement consists of a COBOL verb and an operand or operands. Example: SUBTRACT Tax FROM GrossPay GIVING NetPay
www.mainframe230.blogspot.com
Page 24
date:16.05.2010
COBOL Training PROCEDURE DIVISION GUIDELINES: Paragraph names and section names should start in Area A Statements and sentences should start in Area B Section names and paragraph names are user-defined Section names must be unique and must be different from paragraph names Section names should be followed by a space and the word SECTION with a period at the end Paragraph names must be unique within a section If paragraph name is duplicated across sections, it has to be qualified as paragraph-name { of / in } section name
COMPUTE...ON SIZE ERROR, COMPUTE...NOT ON SIZE ERROR Compiler Directives A Compiler-Directing Statement is a statement, beginning with a compiler directing verb, that causes the compiler to take a specific action during compilation. Example : Use, Copy Explicit Scope Terminator An EXPLICIT SCOPE TERMINATOR terminates the scope of conditional statements. This includes statements which contain conditional expression (like IF statement) and imperative statements (like ADD) Examples END-ADD, END-COMPUTE, END-SUBTRACT, END-MULTIPLY , END-DELETE, END-READ, END-IF, END-EVALUATE, END-CALL, Example with ADD ADD 1 TO TWODIGITYEAR ON SIZE ERROR PERFORM CENTURYWINDOWOVERFLOW END-ADD Implicit Scope Terminator At the end of any sentence, an IMPLICIT SCOPE TERMINATOR is a separator period that terminates the scope of all previous statements not yet terminated.
www.mainframe230.blogspot.com
Page 25
date:16.05.2010
COBOL Training
www.mainframe230.blogspot.com
Page 26
date:16.05.2010
01
www.mainframe230.blogspot.com
Page 27
date:16.05.2010
COBOL Training
< >>ADDidentifier-1identifier-2> literal-1 TO literal-2 < >GIVINGidentifier-3> ROUNDED >> SIZE ERRORimperative-statement-1 ON >> NOTSIZE ERRORimperative-statement-2 ON >>< END-ADD The values of the operands preceding the word GIVING are added together, and the sum is stored as the new value of each data item referenced by identifier-3. >>ADDCORRESPONDINGidentifier-1TOidentifier-2> CORR >> ROUNDED SIZE ERRORimperative-statement-1 ON >> NOTSIZE ERRORimperative-statement-2 ON >>< END-ADD
Elementary data items within identifier-1 are added to and stored in the corresponding elementary items within identifier-2. Example:
ADD A TO B ADD A B GIVING C. ADD CORR OLD-REC TO NEW-REC.
In the case of TO option, the previous value of the last named operand takes part in the summation and then this value is replaced by the result. Whereas in the case of GIVING option, the value of the last named operand does not take part in the summation and only the result is stored there. In the group add, numeric elementary items in the group referred to by first identifier are added to and stored in the corresponding elementary items of the second group. Data items in identifier-1 and identifier-2 take part in the summation, only if they have the Same data name and same qualifiers Corresponding items can have different locations within the group and the field sizes Can also be different.
6.3.2. SUBTRACT
<
www.mainframe230.blogspot.com
Page 28
date:16.05.2010
COBOL Training
>>SUBTRACTidentifier-1FROM> literal-1 < >identifier-2> ROUNDED >> SIZE ERRORimperative-statement-1 ON >> NOTSIZE ERRORimperative-statement-2 ON >>< END-SUBTRACT All identifiers or literals preceding the key word FROM are added together and this sum is subtracted from and stored immediately in identifier-2. This process is repeated for each successive occurrence of identifier-2, in the left-to-right order in which identifier-2 is specified. < >>SUBTRACTidentifier-1FROMidentifier-2> literal-1 literal-2 < >GIVINGidentifier-3> ROUNDED >> SIZE ERRORimperative-statement-1 ON >> NOTSIZE ERRORimperative-statement-2 ON >>< END-SUBTRACT All identifiers or literals preceding the key word FROM are added together and this sum is subtracted from identifier-2 or literal-2. The result of the subtraction is stored as the new value of each data item referenced by identifier-3. >>SUBTRACTCORRESPONDINGidentifier-1FROM> CORR >identifier-2> ROUNDED >> SIZE ERRORimperative-statement-1 ON >> NOTSIZE ERRORimperative-statement-2 ON >><
www.mainframe230.blogspot.com
Page 29
date:16.05.2010
COBOL Training
END-SUBTRACT Elementary data items within identifier-1 are subtracted from, and the results are stored in, the corresponding elementary data items within identifier-2.
Example:
SUBTRACT A FROM B. SUBTRACT A B FROM C. SUBTRACT A B FROM C D. SUBTRACT 10 FROM A.
6.3.3. MULTIPLY
< >>MULTIPLYidentifier-1BYidentifier-2> literal-1 ROUNDED >> SIZE ERRORimperative-statement-1 ON >> NOTSIZE ERRORimperative-statement-2 ON >>< END-MULTIPLY The value of identifier-1 or literal-1 is multiplied by the value of identifier-2; the product is then placed in identifier-2. For each successive occurrence of identifier-2, the multiplication takes place in the left-to-right order in which identifier-2 is specified. >>MULTIPLYidentifier-1BYidentifier-2> literal-1 literal-2 < >GIVINGidentifier-3> ROUNDED >> SIZE ERRORimperative-statement-1 ON >> NOTSIZE ERRORimperative-statement-2 ON >>< END-MULTIPLY
The value of identifier-1 or literal-1 is multiplied by the value of identifier-2 or literal-2. The product is then stored in the data item(s) referenced by identifier-3. Example:
www.mainframe230.blogspot.com
Page 30
date:16.05.2010
COBOL Training
MULTIPLY A MULTIPLY A MULTIPLY A MULTIPLY 0.5 BY B. BY B C D. BY B GIVING C. BY B.
6.3.4. DIVIDE
< >>DIVIDEidentifier-1INTOidentifier-2> literal-1 ROUNDED >> SIZE ERRORimperative-statement-1 ON >> NOTSIZE ERRORimperative-statement-2 ON >>< END-DIVIDE In Format 1, the value of identifier-1 or literal-1 is divided into the value of identifier-2, and the quotient is then stored in identifier-2. For each successive occurrence of identifier-2, the division takes place in the left-to-right order in which identifier-2 is specified. >>DIVIDEidentifier-1INTOidentifier-2> literal-1 literal-2 < >GIVINGidentifier-3> ROUNDED >> SIZE ERRORimperative-statement-1 ON >> NOTSIZE ERRORimperative-statement-2 ON >>< END-DIVIDE In Format 2, the value of identifier-1 or literal-1 is divided into the value of identifier-2 or literal-2. The value of the quotient is stored in each data item referenced by identifier-3. >>DIVIDEidentifier-1BYidentifier-2> literal-1 literal-2 < >GIVINGidentifier-3> ROUNDED >>
www.mainframe230.blogspot.com
Page 31
date:16.05.2010
COBOL Training
SIZE ERRORimperative-statement-1 ON >> NOTSIZE ERRORimperative-statement-2 ON >>< END-DIVIDE In Format 3, the value of identifier-1 or literal-1 is divided by the value of identifier-2 or literal-2. The value of the quotient is stored in each data item referenced by identifier-3. >>DIVIDEidentifier-1INTOidentifier-2> literal-1 literal-2 >GIVINGidentifier-3REMAINDERidentifier-4> ROUNDED >> SIZE ERRORimperative-statement-1 ON >> NOTSIZE ERRORimperative-statement-2 ON >>< END-DIVIDE In Format 4, the value of identifier-1 or literal-1 is divided into identifier-2 or literal-2. The value of the quotient is stored in identifier-3, and the value of the remainder is stored in identifier-4. >>DIVIDEidentifier-1BYidentifier-2> literal-1 literal-2 >GIVINGidentifier-3REMAINDERidentifier-4> ROUNDED >> SIZE ERRORimperative-statement-1 ON >> NOTSIZE ERRORimperative-statement-2 ON >>< END-DIVIDE In Format 5, the value of identifier-1 or literal-1 is divided by identifier-2 or literal-2. The value of the quotient is stored in identifier-3, and the value of the remainder is stored in identifier-4.
Example:
DIVIDE DIVIDE 5 A INTO INTO A .. B GIVING C.
www.mainframe230.blogspot.com
Page 32
date:16.05.2010
COBOL Training
DIVIDE DIVIDE A A BY BY B B GIVING C D. GIVING C REMAINDER D.
This phrase cannot be specified for the identifier that receives the remainder in a divide operation.
6.3.7. COMPUTE
< >>COMPUTEidentifier-1=> ROUNDED EQUAL >arithmetic-expression> >> SIZE ERRORimperative-statement-1 ON >> NOTSIZE ERRORimperative-statement-2 ON >>< END-COMPUTE
This verb is very powerful and is used as a substitute for any of the other arithmetic verbs like add, subtract, multiply, divide.
www.mainframe230.blogspot.com
Page 33
date:16.05.2010
COBOL Training Arithmetic operators permitted in COBOL are: ** / * + => => => => => EXPONENTIATION. DIVISION. MULTIPLICATION.. SUBTRACTION. ADDITION.
6.4. MISCELLANEOUS
6.4.1. ACCEPT
Accept statement can transfer data from an input/output device, or system information contained in the specified conceptual data items DATE, DAY, DAY-OF-WEEK, or TIME Format: ACCEPT IDENTIFIER [ FROM mnemonic name DATE DAY TIME DAY-OF-WEEK}] When FROM is omitted, the data is read from the operators terminal. At the time of execution, program is suspended until the operator enters the data. Mnemonic name option is implementation dependent and has to be defined in SPECIAL-NAMES paragraph. The date option returns six digit [9(6)] current date in YYMMDD format. The day option returns five digit [9(5)] current date in YYDDD format. The time option returns eight digit [9(8)] time in HHMMSSTT format The day-of-week option returns a single digit [9(1)] value as follows: 1 - Monday, 2 - Tuesday, 3 - Wednesday and so on.
6.4.2. DISPLAY
Display statement is used to display data on the terminal. Format DISPLAY {identifier-1 literal-1} Example
DISPLAY RESULT IS :,THE-RESULT
identifier-2
, literal - 2
[upon mnemonic-name]
www.mainframe230.blogspot.com
Page 34
date:16.05.2010
COBOL Training
6.4.3. CONTINUE
The CONTINUE statement allows you to specify a no operation statement. CONTINUE indicates that no executable instruction is present.
6.4.5. EXIT
The EXIT statement provides a common end point for a series of paragraphs. This statement indicates a no operation and when executed, no action takes place
6.4.6. INITIALIZE
This statement is used to initialize value of either an elementary or group item.
< >>INITIALIZEidentifier-1> >>< < REPLACINGALPHABETICBYidentifier-2 ALPHANUMERIC DATA literal-1 NUMERIC ALPHANUMERIC-EDITED NUMERIC-EDITED
Example: INITIALIZE WS - REC. 01 WS - REC. 05 A PIC 9(4). 05 B PIC A(4). 05 C PIC X(4). DATAITEM PIC BEFORE AFTER EXECUTION EXECUTION A 9(4) 1000 0000 B A(4) LIFE bbbb C X(4) A2BC bbbb INITIALIZE B REPLACING NUMERIC DATA BY 105 REPLACING ALPHANUMERIC DATA BY LOVE REPLACING NUMERIC-EDITED DATA BY 1500.23.
www.mainframe230.blogspot.com
Page 35
date:16.05.2010
COBOL Training ELSE statement-2 END-IF. The condition can be any one of the conditions mentioned before. Statement-1 represents one or more COBOL statements If the condition is found to be true, the statements represented in the THEN part will be executed. Statement-1 and Statement-2 represents one or more COBOL statements. If the condition is found to be FALSE, the statements represented in the ELSE part will be executed After execution of this statement control will be implicitly transferred to the next sentence following the IF statement. Period (.) is placed only at the end of the IF structure. i.e. after the connective END-IF. Example:
IF ELSE ADD AMOUNT TO TOTAL-2 END-IF. AMOUNT > 5000 THEN ADD AMOUNT TO TOTAL-1
NEXT SENTENCE will be used to make either the THEN or ELSE part dummy.
6.5.2. NESTED IF
Inclusion of one or more IF statements within its scope is called as Nesting. The most inclusive IF statement must have terminating period. IF condition-1 [THEN] IF condition-2 [THEN ] statement-1 ELSE statement-2 END-IF END-IF. Indent nested if s properly to improve readability and maintainability.
www.mainframe230.blogspot.com
Page 36
date:16.05.2010
COBOL Training WHEN OTHER phrase is selected only if none of the previous WHEN phrases are selected. Objects can be Numeric value , Non-numeric value, non-numeric values, Conditional Values, keywords Example1:
EVALUATE TRUE WHEN MONTH = 4 MOVE 30 WHEN MONTH = 2 MOVE 28 WHEN OTHER MOVE 31 END-EVALUATE. OR 6 OR 9 OR 11 TO NO - OF - DAYS TO NO - OF - DAYS TO NO - OF - DAYS
Example:
EVALUATE PRODUCT-TYPE ALSO CUSTOMER-TYPE WHEN 1 ALSO ANY MOVE 0 TO COMMISSION WHEN 2 ALSO 1 THRU 5 MOVE 10 TO COMMISSION WHEN OTHER MOVE 20 TO COMMISSION END-EVALUATE.
6.5.4. GO TO STATEMENT
>>GOprocedure-name-1>< TO
It is an unconditional transferring control to the specified paragraph . There is no any comparison being made by the system before branching off . Example GO TO
DISPLAY-PARA.
Note: It is advised to avoid GO TO Statements as the structured programming technique does not support unconditional transfer statements.
www.mainframe230.blogspot.com
Page 37
date:16.05.2010
COBOL Training The identifier specified in the statement must be a numeric, integral, elementary item. Example:GO TO RECEIPT-PARA, ISSUE-PARA, ADJUSTMENT-PARA DEPENDING ON TRANSACTION-TYPE.
6.5.6. ALTER
The ALTER statement changes the transfer point specified in a GO TO statement. The ALTER statement encourages the use of unstructured programming practices; the EVALUATE statement provides the same function as the ALTER statement and helps to ensure that your program will be well-structured.
>>ALTER> < >procedure-name-1TOprocedure-name-2>< PROCEED TO
The ALTER statement modifies the GO TO statement in the paragraph named by procedurename-1. Subsequent executions of the modified GO TO statement(s) transfer control to procedure-name-2. procedure-name-1 Must name a Procedure Division paragraph that contains only one sentence: a GO TO statement without the DEPENDING ON phrase. procedure-name-2 Must name a Procedure Division section or paragraph.
Before the ALTER statement is executed, when control reaches the paragraph specified in procedure-name-1, the GO TO statement transfers control to the paragraph specified in the GO TO statement. After execution of the ALTER statement, however, the next time control reaches the paragraph specified in procedure-name-1, the GO TO statement transfers control to the paragraph specified in procedure-name-2. The ALTER statement acts as a program switch, allowing, for example, one sequence of execution during initialization and another sequence during the bulk of file processing.
www.mainframe230.blogspot.com
Page 38
date:16.05.2010
PERFORM para-name-1 thru para-name-2. This statement executes all the statements beginning in the para-name-1 till the last statement in the para-name-2 If there are any other paragraphs placed in between these two paragraphs, those are also executed After executing the said range, the control is implicitly, transferred to the next line after the PERFORM statement Example:
PERFORM BEGIN-PARA THRU END-PARA.
This statement executes all the stmts. in READ-RTN 10 times without testing any condition.
www.mainframe230.blogspot.com
Page 39
date:16.05.2010
COBOL Training
This is also a looping statement which executes imperative statement for all possible values of identifier-1 and identifier-3 until Condition-1 and Condition-2 are TRUE. Example:
PERFORM VARYING I FROM 1 BY 1 UNTIL I > 3 AFTER J FROM 1 BY 1 UNTIL J > 4 Display SALE ( I , J ) END-PERFORM.
The above example will take I and j vales as 1,1 1,2 1,3 1,4 2,1 2,2 2,3 2,4 3,1 3,2 3,3 3,4
www.mainframe230.blogspot.com
Page 40
date:16.05.2010
COBOL Training
www.mainframe230.blogspot.com
Page 41
date:16.05.2010
COBOL Training
88 MARRIED VALUE IS 1. IF SINGLE ADD 1 TO SINGLE-COUNT.
OR
NOT .
www.mainframe230.blogspot.com
Page 42
date:16.05.2010
COBOL Training
7. FILE HANDLING
A data file is a collection of relevant records of an application. A record is a collection of relevant fields pertaining to an item/case/ account/transaction . File is organized in storage media in one of the following ways SEQUENTIAL INDEXED RELATIVE
www.mainframe230.blogspot.com
Page 43
date:16.05.2010
Describe the File with detail like record name ,record structure in the File Section of DATA
DIVISION .Also specify as how many records are grouped as a BLOCK for Input Output Operation. Step3. All files described as explained above should be first opened to do read/write operations. Open statement is used to connect dataset to the Cobol program. These statements are coded in procedure division. Files can be opened for reading, writing, appending. If we have to read a file READ statement is used in procedure Division. Read File -name.
Step4. SYNTAX :
If we have to write in to a file WRITE command is used in procedure Division. The record attached to the concerned file alone can be used to write into the file. SYNTAX : Step5. After read / write operations are over the file must be closed to disconnect the file from the Cobol program. SYNTAX : CLOSE File-name. WRITE Record -name.
7.4.1. EXAMPLE
IDENTIFICATION DIVISION. PROGRAM-ID. F1. ENVIRONMENT DIVISION. INPUT-OUTPUT SECTION. FILE-CONTROL. SELECT CUSTOMER-FILE ASSIGN TO ORGANIZATION IS INDEXED ACCESS MODE IS SEQUENTIAL RECORD KEY IS CUST-NO FILE STATUS IS WS-FST1. DATA DIVISION. FILE SECTION. FD CUSTOMER-FILE RECORD CONTAINS 80 CHARACTERS DATA RECORD IS CUSTOMER-RECORD. 01 CUSTOMER-RECORD. 05 CUST-NO PIC X(5). 05 CUST-DEPT PIC X(5). 05 CUST-DTLS PIC X(70). WORKING-STORAGE SECTION. 01 WS-FST1 PIC X(2). PROCEDURE DIVISION. 000-MAIN-PARA. OPEN OUTPUT CUSTOMER-FILE PERFORM 100-WRITE-PARA PERFORM 200-CLOSE-PARA. 100-WRITE-PARA. ACCEPT CUST-NO ACCEPT CUST-DEPT
DD1
www.mainframe230.blogspot.com
Page 44
date:16.05.2010
COBOL Training ACCEPT CUST-DTLS WRITE CUSTOMER-RECORD DISPLAY 'WRITE FILE STATUS:-' WS-FST1. 200-CLOSE-PARA. CLOSE CUSTOMER-FILE DISPLAY 'CLOSE FILE STATUS:-' WS-FST1 STOP RUN.
www.mainframe230.blogspot.com
Page 45
date:16.05.2010
COBOL Training
Indexed File
>>SELECTfile-name-1ASSIGNassignment-name-1> OPTIONAL TO > INDEXED> ORGANIZATION IS >> ACCESSSEQUENTIAL MODE IS RANDOM DYNAMIC >RECORDdata-name-2> KEY IS PASSWORD data-name-6 IS < >>< entry 1 STATUSdata-name-1 FILE IS entry 1: (3) ALTERNATE RECORDdata-name-3> KEY IS DUPLICATES WITH
www.mainframe230.blogspot.com
Page 46
date:16.05.2010
COBOL Training
Relative files
>>SELECTfile-name-1ASSIGNassignment-name-1> OPTIONAL TO >RELATIVE> ORGANIZATION IS >> ACCESSSEQUENTIAL MODE IS RELATIVEdata-name-4 KEY IS RANDOMRELATIVEdata-name-4 DYNAMIC KEY IS >>< STATUSdata-name-1 FILE IS
Assignment Name ties the select statement to the DDNAME in the JCL QSAM file >>name>< S- VSAM Sequential File >>AS- name>< VSAM Indexed or Relative File >>name><
www.mainframe230.blogspot.com
Page 47
date:16.05.2010
COBOL Training We can choose access mode depending on the order in which we like it to be accessed. Sequential Access Mode will facilitate to access any file records in sequential order. Random Access Mode will facilitate to access a particular record of an Indexed File / Relative File by supplying the Record key before read / write operation. Dynamic Access Mode will facilitate to access a particular record or in sequential order from an Indexed File / Relative File We give below all possible access modes on organization. Seq. access Organization Sequential Relative Indexed Order of write Ascending rel rec. no Ascending key value Invalid Value of rel key Value of rel key Invalid Seq. or random Seq. or random Random access Dynamic access
7.6. FD ENTRY
Syntax Sequential Files
>>FDfile-name-1> >> BLOCKinteger-2CHARACTERS CONTAINS integer-1TO RECORDS >> RECORDinteger-3 CONTAINS CHARACTERS integer-4TOinteger-5 CONTAINS CHARACTERS clause 1 DEPENDINGdata-name-1 ON >> LABELRECORDSTANDARD IS OMITTED >> < DATARECORDdata-name-4 IS RECORDS
www.mainframe230.blogspot.com
Page 48
date:16.05.2010
COBOL Training
ARE >> RECORDING mode MODE IS clause 1: VARYING> IS IN SIZE integer-6 FROM > TOinteger-7 CHARACTERS
www.mainframe230.blogspot.com
Page 49
date:16.05.2010
COBOL Training
F => Fixed which Indicates that every record of same length Example:
FD file-name BLOCK CONTAINS 500 CHARACTERS. RECORD CONTAINS 50 CHARACTERS. DATA-RECORD IS EMP-RECORD. EMP-RECORD 01 EMPNO PIC X(6). 02 EMP-NAME PIC X(30). 02 BASIC PIC 9(5). 02 HRA PIC 9(5). 02 DA PIC 9(4).
The above record is of length 50 characters. V => Variable which Indicates that record length may vary from I1 to I2. Example:
FD BLOCK CONTAINS 320 THRU 640 CHARACTERS. RECORD CONTAINS 32 THRU 64 CHARACTERS. DATA RECORD IS MARK-RECORD. 01 MARK-RECORD 02 P1 PIC 99. 02 MARKS OCCURS 10 THRU 20 Depending P1 03 SCORE PIC 999.
www.mainframe230.blogspot.com
Page 50
date:16.05.2010
COBOL Training
WRITE >>WRITErecord-name-1> FROMidentifier-1 INVALID imperative-statement-1 KEY NOT INVALID imperative-statement-2 KEY Add records to a file or load a file . When this command is used in an Indexed file key must be newer one always. START - Establish the current location in the cluster for a READ NEXT statement
>>STARTfile-name-1> >> KEYEQUALdata-name-1 IS TO = LESS THAN < GREATER THAN > NOT LESS THAN NOT < NOT GREATER THAN NOT > LESS OR EQUAL THAN TO <= GREATEROR EQUAL THAN TO >= >> INVALIDimperative-statement-1 KEY >>< NOT INVALIDimperative-statement-2 END-START KEY
START does not retrieve a record, it positions the current record pointer. READ Sequential Retreival >>READfile-name-1> NEXT RECORD >>
www.mainframe230.blogspot.com
Page 51
date:16.05.2010
Random Retreival
>>READfile-name-1> RECORD INTOidentifier-1 >> KEYdata-name-1 IS >> INVALIDimperative-statement-3 KEY >>< NOT INVALIDimperative-statement-4 END-READ KEY
The READ statement is used to read a record from a file. If it is used to read a sequential file it will read the next record always. If it used to read from an Indexed/Relative file it would read the corresponding key record, the key field must have a value before read. If we have read sequentially from an Indexed file READ NEXT RECORD will retrieve records from a file in sequence REWRITE
>>REWRITErecord-name-1> FROMidentifier-1 >> INVALIDimperative-statement-1 KEY >> NOT INVALIDimperative-statement-2 KEY >>< END-REWRITE
Used for Updating records. Files must be opened in I-O mode. DELETE
>>DELETEfile-name-1> RECORD
www.mainframe230.blogspot.com
Page 52
date:16.05.2010
COBOL Training
>> INVALIDimperative-statement-1 KEY >> NOT INVALIDimperative-statement-2 KEY >>< END-DELETE
Remove records from indexed and relative files only. Not permitted for ESDS and PS files. File must be opened in I-O mode. CLOSE Disconnect the VSAM data set from your program
>>CLOSEfile-name-1><
www.mainframe230.blogspot.com
Page 53
date:16.05.2010
COBOL Training
Space at the end of the data Distributed free space is used for fixed length RRDS empty slots in the set is used for adding records for inserting record and data set are used for adding records. changing their lengths in place For variable - length RRDS, distributed free space is used for adding records and changing their lengths in place A record cannot be deleted but Space given up by a deleted or Space given up by a deleted record can you can reuse its space for a shortened record is be reused record of the same length automatically reclaimed within a control interval Can have spanned records Can be reused as a work file unless it has an alternate index associated with key ranges or exceeds 123 extents per volume can have spanned records can be reused unless it has an associated with exceeds 123 volume Can be reused as a work file as a work file can be reused as work file alternate index key ranges or extents per
www.mainframe230.blogspot.com
Page 54
date:16.05.2010
COBOL Training
www.mainframe230.blogspot.com
Page 55
date:16.05.2010
COBOL Training
7.8.4. COBOL STATEMENTS WITH INDEXED FILES (KSDS) AND RELATIVE (RRDS)
www.mainframe230.blogspot.com
Page 56
date:16.05.2010
COBOL Training
OPEN INPUT X x X
OPEN OUTPUT X X
OPEN I-O X x X X x
OPEN EXTEND X X
X X
X X X
X X X X X X
Random
X X X X
X X X
X X X X X X X
Dynamic
www.mainframe230.blogspot.com
Page 57
date:16.05.2010
COBOL Training
www.mainframe230.blogspot.com
Page 58
date:16.05.2010
COBOL Training
www.mainframe230.blogspot.com
Page 59
date:16.05.2010
COBOL Training
7.8.7.2. KSDS
Applications that require that each record have a key field and require both direct and sequential access. Applications that use High Level Languages which do not support RBA (Relative Byte Address) Applications that require ALTERNATE KEY to access any record . Example : BANK ACCOUNT TRANSACTIONS.
7.8.7.3. RRDS
Applications that require only Direct Access. Applications that have one to one correspondence between records. Applications that require that each record have a key field.
www.mainframe230.blogspot.com
Page 60
date:16.05.2010
COBOL Training
8. SUBPROGRAMS
Compiled, debugged, stored in library to be called when needed. The program that calls another program is referred to as the Calling program and the program it calls is referred to as the Called program (subprogram). Called program may itself call yet another program. When the called program is completed, the program can either transfer control back to the calling program or end the run unit. No specific source code statements or options identify a COBOL program to be a main program or a subprogram. Edit routines Error control checks Standard calculations Summary, total printing
Advantages of Subprograms
Avoids duplication of effort Improves programmer productivity Provides greater flexibility Changes to called program can be made without modifying calling program Results in greater standardization
www.mainframe230.blogspot.com
Page 61
date:16.05.2010
COBOL Training
1. Subprogram Sub1 called and executed in its entirety 2. Control returns to calling program 3. Subprogram Sub2 called and executed in its entirety 4. Control returns to calling program 5. You can use GOBACK instead of EXIT PROGRAM.
www.mainframe230.blogspot.com
Page 62
date:16.05.2010
COBOL Training
Common Data Items in Subprogram Linkage Calling Program Description WORKING-STORAGE SECTION. 01 PARAM-LIST. 05 PARTCODE PIC A. 05 PARTNO PIC X(4). 05 U - SALES PIC 9(5). PROCEDURE DIVISION CALL CALLEDPROG USING PARAM- LIST In the calling program the code for parts (PARTCODE) and the number (PARTNO) are referred to separately In the called program, the code for parts and the part number are combined into one data item (PART-ID ). In the called program a reference to PART-ID is the valid reference to them Called Program Description LINKAGE SECTION. 01 L-PARAM. 10 PART-ID PIC X(5). 10 SALES PIC 9(5). PROCEDURE DIVISION USING L-PARAM
www.mainframe230.blogspot.com
Page 63
date:16.05.2010
COBOL Training When control is returned to a COBOL program from a call, the contents of register 15 are stored into the calling program s RETURN - CODE special register. When control is returned from a COBOL program to the operating system, the special register contents are returned as a user return code.
www.mainframe230.blogspot.com
Page 64
date:16.05.2010
COBOL Training
www.mainframe230.blogspot.com
Page 65
date:16.05.2010
COBOL Training
OVERFLOW executed when pointer is < 1 or > the picture length of the receiving field IDENTIFICATION DIVISION. PROGRAM-ID. STRPGM. ENVIRONMENT DIVISION. DATA DIVISION. WORKING-STORAGE SECTION. 01 ID1 PIC X(5) VALUE "AB,CD". 01 ID2 PIC X(5) VALUE "E,FGH". 01 ID3 PIC X(5) VALUE "IJK#L". 01 ID4 PIC X(5) VALUE "MN#OP". 01 ID5 PIC X(100). 01 STR-COUNT PIC 999 VALUE 1. PROCEDURE DIVISION. PERFORM PROCESS-PARA PERFORM END-PARA. PROCESS-PARA. STRING ID1 "/" ID2 DELIMITED BY "," "/" ID3 "/" ID4 DELIMITED BY "#" INTO ID5 WITH POINTER STR-COUNT DISPLAY ID5 DISPLAY STR-COUNT. END-PARA. STOP RUN. Output is AB/E/IJK/MN 012
www.mainframe230.blogspot.com
Page 66
date:16.05.2010
COBOL Training Unstring statement causes contiguous data in a Sending Field to be separated and placed into multiple receiving fields. How to interpret the above diagram:1)Identifier 1 is the sending field 2)Pieces of the identifier 1 delimited by either identifier 2 or 3 are extracted and sent to the receiving fields which are identifier 4 . 3)Each instance of identifier 4 holds a piece 4)For each instance of identifier 4 you can have a identifier 5 that is set with the delimiter. 5)For each instance of identifier 4 you can have a identifier 6 that is set with the count of bytes in identifier 4 except the delimiter. 6)The pointer identifier 7 is set to the number of bytes examined in the sending field plus 1 7)Identifier 8 is set to the number of receiving data areas that were acted upon. Restrictions Identifiers should be with USAGE clause DISPLAY should not be with P picture LITERALS should be non numeric literals/figurative constant except ALL (considers a character non numeric for figurative constant) Identifier1 must be an alphanumeric item May not be reference modified. Represents the sending field. DELIMITED BY phrase is used to limit the data to be transferred . SIZE sends the complete sending area. INTO represents the receiving field POINTER contains a value that indicates a relative position in the sending field. It should be initialized to 1 It will return the length of the received data plus 1. All data receiving fields have been acted upon and the sending still contain the unexamined character. OVER FLOW executed when pointer is < 1 or > the picture length of the receiving field TALLYING in this count field is increased by the number of data receiving fields acted upon execution. IDENTIFICATION DIVISION. PROGRAM-ID. STRPGM. ENVIRONMENT DIVISION. DATA DIVISION. WORKING-STORAGE SECTION. 01 ID1 PIC X(9) VALUE 01 ID1-D PIC X VALUE 01 ID1-C PIC 9 VALUE 01 ID2 PIC X(9) VALUE 01 ID2-D PIC X VALUE 01 ID2-C PIC 9 VALUE
www.mainframe230.blogspot.com
Page 67
date:16.05.2010
COBOL Training 01 ID3 PIC X(9) VALUE "IJK#L". 01 ID3-D PIC X VALUE "@". 01 ID3-C PIC 9 VALUE 0. 01 ID4 PIC X(9) VALUE SPACES. 01 ID4-C PIC 9 VALUE 0. 01 ID-P PIC 999 VALUE 1. 01 ID-T PIC 999 VALUE 0. 01 DE-ID PIC X VALUE ",". LINKAGE SECTION. 01 JCL-PARM. 02 PARM-L PIC S9(4) COMP. 02 PARM-D PIC X(100). PROCEDURE DIVISION USING JCL-PARM. PERFORM PROCESS-PARA PERFORM END-PARA. PROCESS-PARA. UNSTRING PARM-D DELIMITED BY DE-ID OR ALL "*" INTO ID1 DELIMITER IN ID1-D COUNT IN ID1-C ID2 DELIMITER IN ID2-D COUNT IN ID2-C ID3 DELIMITER IN ID3-D COUNT IN ID3-C ID4 COUNT IN ID4-C WITH POINTER ID-P TALLYING IN ID-T ON OVERFLOW PERFORM FLOW-EXIT-PARA END-UNSTRING DISPLAY "ID1:-" ID1 "ID1-D:-" ID1-D "ID1-C:-" ID1-C DISPLAY "ID2:-" ID2 "ID2-D:-" ID2-D "ID2-C:-" ID2-C DISPLAY "ID3:-" ID3 "ID3-D:-" ID3-D "ID3-C:-" ID3-C DISPLAY "ID4:-" ID4 "ID4-C:-" ID4-C DISPLAY "ID-P:-" ID-P DISPLAY "ID-T:-" ID-T. FLOW-EXIT-PARA. DISPLAY "FLOW-EXIT-PARA EXECUTED". END-PARA. STOP RUN. OUTPUT ID1:-AB ID2:-EFGHI ID3:-PQRS ID4:-GH ID-P:-101 ID-T:-004 ID1-D:-,ID1-C:-2 ID2-D:-,ID2-C:-5 ID3-D:-*ID3-C:-4 ID4-C:-5
www.mainframe230.blogspot.com
Page 68
date:16.05.2010
COBOL Training
Example:
INSPECT ITEM-1 TALLYING C1 FOR ALL SPACES. INSPECT ITEM-1 TALLYING C2 FOR ALL CHARACTERS. INSPECT ITEM-1 TALLYING C3 FOR LEADING SPACES. DATA ITEM1 BB56BB will yield 4 ,2,1 respectively.
FORMAT 2:
>>--INSPECT--identifier-1--REPLACING------------------------------------------------> <-----------------------------------------------------------------------------+ <------------------+ >------CHARACTERS BY----identifier-5------------------------------------------------>< +-literal-3----+ +- phrase 1 +-+ <----------------------------------------------------------+ <--------------+ +---ALL------------identifier-3----BY----identifier-5-----------------------+ +-LEADING- +-literal-1----+ +-literal-3----+ +- phrase 1 +-+ +-FIRST---+ phrase 1: +----BEFORE-------------------identifier-4------------------------------------------- +-AFTER--+ +-INITIAL-+ +-literal-2----+
Example:
INSPECT ITEM-4 REPLACING - BY /.
www.mainframe230.blogspot.com
Page 69
date:16.05.2010
COBOL Training
DATA ITEM1 10-07-97 OUTPUT 20/07/97
www.mainframe230.blogspot.com
Page 70
date:16.05.2010
COBOL Training
9.4. DECLARATIVES
It is used for tracing the paragraph or procedures executed by the program as per the Instruction given by the programmer. DEBUG-NAME is a special register storing the name of the paragraph it is executing. We have to specify the Option " WITH DEBUGGING MODE" option in the SOURCE-COMPUTER The following rules should be followed. It is the first Section To Be Defined in Procedure Division. The rest of the paragraphs may be placed in User defined Section. The Debug Declarative-paragraph should immediately follow the "USE" instruction. IDENTIFICATION DIVISION. PROGRAM-ID. DECLRPGM. ENVIRONMENT DIVISION. CONFIGURATION SECTION. SOURCE-COMPUTER. IBM-390 WITH DEBUGGING MODE. OBJECT-COMPUTER. IBM-390. DATA DIVISION. WORKING-STORAGE SECTION. PROCEDURE DIVISION. DECLARATIVES. DEBUG-DECLARATIVES SECTION. USE FOR DEBUGGING ALL PROCEDURES. DEBUG-DECLARATIVES-PARA. DISPLAY "TRACING:-" DEBUG-NAME. END DECLARATIVES. MAIN SECTION. MAIN-PARA. PERFORM PARA-1 PERFORM PARA-2 PERFORM PARA-3 PERFORM END-PARA. PARA-1. DISPLAY "IN PARA-1". PARA-2. DISPLAY "IN PARA-2". PARA-3. DISPLAY "IN PARA-3". END-PARA. STOP RUN. This declarative section of the program will be executed before the start of execution of each paragraph defined in the program. i.e. "TRACING paraname " will be displayed before the execution of each paragraph. Make SURE you code PARM=/DEBUG in the EXEC JCL card before you run your program!.
www.mainframe230.blogspot.com
Page 71
date:16.05.2010
COBOL Training
File-name -1 is the name given in the SD entry that describes the records to be sorted. data-name-1 specifies a KEY data item on which the SORT statement will be based. Each data name must identify a data item in a record associated with file-name -1
There can be any number of data names in file-name1 and each can be either sorted in ascending or descending order. The first key referred is taken as major key and the next referred is taken as the next significant key and so on Sorting in ascending order will be done from lowest key value to highest key value. Sorting in descending order will be done from highest key value to lowest key value. Duplicates are specified to place the similar records in the order they are accessed.
Alphabet-name1 must be specified in the alphabet clause of SPECIAL-NAMES paragraph. USING and INPUT-PROCEDURE are mutually exclusive. If USING is specified it means that the sort feature will automatically open file name 2 (s) and read the records as input. You specify an INPUT PROCEDURE when you want to process input records before they are RELEASED to the sort feature for sorting. If you do specify INPUT PROCEDURE it is your responsibility to open input file(s)
www.mainframe230.blogspot.com
Page 72
date:16.05.2010
COBOL Training and read the input records. You must then RELEASE the records via RELEASE to the SORT feature. You do this when the INPUT PROCEDURE is driven. File-name-2 is the actual file to be sorted. Multiple files can specified. You must define this file(s) in the standard way. Input-Procedure is specified to Select or Modify the Input Record before Sort. This Procedure can do any modifications in the record and copy Using RELEASE Statement.
The OUTPUT PROCEDURE is mutually exclusive with USING. If you specify USING file name 3 then the file name 3 is automatically opened by the sort feature and used as the sort output file. If you specify OUTPUT PROCEDURE, you must open the output file and write the sorted records when the OUTPUT PROCEDURE is driven. You must define file name 3 in the usual way. Example with USING IDENTIFICATION DIVISION. PROGRAM-ID. SORT1. ENVIRONMENT DIVISION. INPUT-OUTPUT SECTION. FILE-CONTROL. SELECT SORT-WORK-ONE ASSIGN TO SORTFILE. SELECT INFILE ASSIGN TO INFILE. SELECT OUTFILE ASSIGN TO OUTFILE. DATA DIVISION. FILE SECTION. SD SORT-WORK-ONE RECORD CONTAINS 80 CHARACTERS DATA RECORD SORT-RECORD. 01 SORT-RECORD. 05 SORT-KEY1 PIC X(5). 05 SORT-KEY2 PIC X(5). 05 SORT-DATA PIC X(70). FD INFILE RECORD CONTAINS 80 CHARACTERS BLOCK 800 RECORDING MODE IS F DATA RECORD IS INFILE-RECORD. 01 INFILE-RECORD PIC X(80). FD OUTFILE RECORD CONTAINS 80 CHARACTERS BLOCK 80 RECORDING MODE IS F DATA RECORD IS OUTFILE-RECORD. 01 OUTFILE-RECORD PIC X(80). WORKING-STORAGE SECTION. PROCEDURE DIVISION. PERFORM MAIN-PARA DISPLAY "SORT RC " SORT-RETURN PERFORM END-PARA. MAIN-PARA. SORT SORT-WORK-ONE ON ASCENDING KEY SORT-KEY2 WITH DUPLICATES IN ORDER USING INFILE GIVING OUTFILE. END-PARA. STOP RUN. Example 2 with INPUT and OUTPUT PROCEDURES
www.mainframe230.blogspot.com
Page 73
date:16.05.2010
COBOL Training IDENTIFICATION DIVISION. PROGRAM-ID. SORT2. ENVIRONMENT DIVISION. CONFIGURATION SECTION. SOURCE-COMPUTER. IBM-390 WITH DEBUGGING MODE. INPUT-OUTPUT SECTION. FILE-CONTROL. SELECT SORTFILE ASSIGN TO SORTFILE. SELECT INFILE ASSIGN TO INFILE FILE STATUS IS INFILE-STAT. SELECT OUTFILE ASSIGN TO OUTFILE FILE STATUS IS OUTFILE-STAT. DATA DIVISION. FILE SECTION. SD SORTFILE RECORD CONTAINS 80 CHARACTERS DATA RECORD SORT-RECORD. 01 SORT-RECORD. 05 SORT-KEY1 PIC X(5). 05 SORT-KEY2 PIC X(5). 05 SORT-DATA PIC X(70). FD INFILE RECORD CONTAINS 80 CHARACTERS BLOCK 800 RECORDING MODE IS F DATA RECORD IS INFILE-RECORD. 01 INFILE-RECORD PIC X(80). FD OUTFILE RECORD CONTAINS 80 CHARACTERS BLOCK 80 RECORDING MODE IS F DATA RECORD IS OUTFILE-RECORD. 01 OUTFILE-RECORD PIC X(80). WORKING-STORAGE SECTION. 01 WS-VARS. 03 INFILE-STAT PIC X(2). 03 OUTFILE-STAT PIC X(2). 03 INFILE-EOF PIC X VALUE "N". 88 INFILEEOF VALUE "Y". 03 OUTFILE-END PIC X VALUE "N". 88 OUTFILEEND VALUE "Y". 03 WS-FLAG PIC X(1) VALUE "0". 03 WS-INFILE-BUFFER PIC X(80). 03 WS-OUTFILE-BUFFER PIC X(80). 03 WS-FLAG PIC X(1) VALUE "0". PROCEDURE DIVISION. PERFORM OPEN-PARA PERFORM MAIN-PARA DISPLAY "SORT RC " SORT-RETURN PERFORM CLOSE-PARA PERFORM END-PARA. OPEN-PARA. OPEN INPUT INFILE OUTPUT OUTFILE IF INFILE-STAT NOT = "00" DISPLAY "INFILE DID NOT OPEN :" INFILE-STAT STOP RUN END-IF IF OUTFILE-STAT NOT = "00"
www.mainframe230.blogspot.com
Page 74
date:16.05.2010
COBOL Training DISPLAY "OUTFILE DID NOT OPEN :" OUTFILE-STAT STOP RUN END-IF. CLOSE-PARA. CLOSE INFILE CLOSE OUTFILE. MAIN-PARA. SORT SORTFILE ON ASCENDING KEY SORT-KEY2 WITH DUPLICATES IN ORDER INPUT PROCEDURE SORT-INPROC OUTPUT PROCEDURE SORT-OUTPROC. SORT-INPROC. PERFORM SORT-READ UNTIL INFILEEOF. SORT-READ. PERFORM READ-INFILE IF NOT INFILEEOF RELEASE SORT-RECORD FROM WS-INFILE-BUFFER. READ-INFILE. D DISPLAY "IN READ" READ INFILE INTO WS-INFILE-BUFFER AT END MOVE "Y" TO INFILE-EOF. SORT-OUTPROC. D DISPLAY "IN OUTPROC" PERFORM SORT-WRITE UNTIL OUTFILEEND. SORT-WRITE. D DISPLAY "IN SORT-WRITE" PERFORM SORT-RETURN-PARA WRITE OUTFILE-RECORD FROM WS-OUTFILE-BUFFER. SORT-RETURN-PARA. D DISPLAY "IN SORT-RETURN" RETURN SORTFILE INTO WS-OUTFILE-BUFFER AT END MOVE "Y" TO OUTFILE-END. END-PARA. STOP RUN. Notes: In the above program the sort feature drives the SORT-INPROC (INPUT PROCEDURE) only once. When it is thus driven you read input records one after the other and issue the RELEASE verb to make these records available to sort. When you have no more input you return from the INPUT PROCEDURE SORTINPROC. The sort feature will store input records you supply in sort work files until your INPUT PROCEDURE returns. After your INPUT PROCEDURE returns, the sort feature will sort the stored input records. As each output record (sorted) is ready your OUTPUT PROCEDURE is driven. Notice that while the INPUT PROCEDURE is driven only once, the OUTPUT PROCEDURE is driven many times, once for each output record. MERGE The MERGE statement is a PROCEDURE DIVISION statement which merges two or more input files on some common key. The merged records are then written to an output file or made available to an output procedure.
www.mainframe230.blogspot.com
Page 75
date:16.05.2010
COBOL Training
< < >>MERGEfile-name-1ASCENDINGdata-name-1> ON DESCENDING KEY >USINGfile-name-2> SEQUENCEalphabet-name-1 COLLATING < >file-name-3> >OUTPUT PROCEDUREprocedure-name1>< < IS THROUGHprocedure-name-2 THRU IS
GIVINGfile-name-4
File-name -1 is the name given in the SD entry that describes the records to be merged. File-name-2 and file-name-3 are files to be merged. Multiple files can specified. File-name-4 is the actual output merged file. GIVING and POUTPUT-PROCEDURE are mutually exclusive Other rules remain same as that of SORT statement.
Example IDENTIFICATION DIVISION. PROGRAM-ID. MERGE1. ENVIRONMENT DIVISION. INPUT-OUTPUT SECTION. FILE-CONTROL. SELECT MERGE-WORK-ONE ASSIGN TO MERGFILE. SELECT INFILE1 ASSIGN TO INFILE1. SELECT INFILE2 ASSIGN TO INFILE2. SELECT OUTFILE ASSIGN TO OUTFILE. DATA DIVISION. FILE SECTION. SD MERGE-WORK-ONE RECORD CONTAINS 80 CHARACTERS DATA RECORD MERGE-RECORD. 01 MERGE-RECORD. 05 MERGE-KEY1 PIC X(5). 05 MERGE-KEY2 PIC X(5). 05 MERGE-DATA PIC X(70). FD INFILE1 RECORD CONTAINS 80 CHARACTERS BLOCK 800 RECORDING MODE IS F
www.mainframe230.blogspot.com
Page 76
date:16.05.2010
COBOL Training DATA RECORD IS INFILE1-RECORD. INFILE1-RECORD PIC X(80). INFILE2 RECORD CONTAINS 80 CHARACTERS BLOCK 80 RECORDING MODE IS F DATA RECORD IS INFILE2-RECORD. 01 INFILE2-RECORD PIC X(80). FD OUTFILE RECORD CONTAINS 80 CHARACTERS BLOCK 800 RECORDING MODE IS F DATA RECORD IS OUTFILE-RECORD. 01 OUTFILE-RECORD PIC X(80). WORKING-STORAGE SECTION. PROCEDURE DIVISION. PERFORM MAIN-PARA DISPLAY "MERGE RC " SORT-RETURN PERFORM END-PARA. MAIN-PARA. MERGE MERGE-WORK-ONE ON ASCENDING KEY MERGE-KEY1 USING INFILE1 INFILE2 GIVING OUTFILE. END-PARA. STOP RUN. 01 FD
www.mainframe230.blogspot.com
Page 77
date:16.05.2010
COBOL Training
NUMVAL - Returns the numeric value represented by the alphanumeric character string specified in the argument NUMVAL-C - Returns the numeric value represented by the alphanumeric character string specified in the argument .Any optional Currency sign mentioned in argument 2 and decimal point are missed on conversion.
11.3. FINANCE
ANNUITY FUNCTION ANNUITY(Argument1, Argument2) Argument1= Rate Of Interest Argument2 = No.of periods Formula is Argument1/(1-(1-Argument1)**Argument2 Or 1/argument2 if Argument1=0 PRESENT VALUE - Returns a Value that approximate the present value of a series of future periodend amounts specified by argument1 at a discount rate argument2
www.mainframe230.blogspot.com
Page 78
date:16.05.2010
COBOL Training
www.mainframe230.blogspot.com
Page 79
date:16.05.2010
COBOL Training
CURRENCY DYNAM
www.mainframe230.blogspot.com
Page 80
date:16.05.2010
COBOL Training
Performance consideration : Using DYNAM = YES eases sub-programs maintenance since the application will not have to be relink-edited if the subprogram is changed. However individual applications may experience some performance degradation due to a longer path length but overall system performance may be slightly improved. Causes the text files of subprograms called with a CALL literal statement to be included with the calling program into a single module file.
NODYNAM
www.mainframe230.blogspot.com
Page 81
date:16.05.2010
COBOL Training
13. APPENDIX
13.1. ERROR CODES
The File Status Code attached to a file will have Picture Clause of length 2 digits. The first digit will denote the general category under which the return code falls. The second digit will denote the particular type of Error/Message under that category. The first digit category are as follows : 0 Returned When Successful Open / Read / Write / Operation. 1 Returned When At End Condition Fails 2 Returned When Index Key Fails 3 Returned When Permanent Open Error Occurs. 4 Returned When Logic Error in opening/closing/deleting etc 9 Implementator Defined 0 Successful Open / Read / Write / Operation. 0 Successful Completion 2 A duplicate key was detected. 4 The length of the record just read did not conform to the fixed file attributes for the file. 5 The referenced optional file was not present when the OPEN statement was executed. If the open mode is I-O or EXTEND, the file is created. (This does not apply to VSAM sequential files; file status 0 is returned.) 7 An OPEN or CLOSE statement was executed with a phrase that implies a tape file (i.NO REWIND, REEL/UNIT, or FOR REMOVAL but the referenced file is not a tape file. 1 When At End Condition Fails 0 A sequential READ was attempted on an optional file that was not present or after end of file was reached. 4 A sequential READ was attempted for a relative file and the relative record number is larger than the maximum number that can be stored in the relative key data item. 2 This Error Will Occur When Index Key Fails 1 A sequence error exists for a sequentially accessed indexed file. 2 An attempt was made to WRITE a record that would create a duplicate key. 3 An attempt was made to access a record that does not exist or an optional file that was not present. 4 An attempt was made to WRITE beyond the boundaries for a relative or indexed file. 3 Permanent Open Error Occurs. 4 An attempt was made to WRITE beyond the boundaries of a sequential file. 5 An OPEN statement was attempted on a non-optional file that was not present. 7 An OPEN statement was attempted on a file that would not support the open mode specified in the OPEN statement. 8 An OPEN statement was attempted on a file previously closed with lock.
www.mainframe230.blogspot.com
Page 82
date:16.05.2010
COBOL Training 9 An OPEN statement was unsuccessful because of a conflict between the fixed file attributes and the attributes specified for the file in the program. 4 Logic Error in opening/closing/deleting etc. 1 An OPEN statement was attempted for a file in the open mode. 2 A CLOSE was attempted for a file not in the open mode. 3 The last I/O statement executed for the file prior to the execution of a REWRITE or DELETE statement was not a successfully executed READ statement. 4 An attempt was made to REWRITE a record with an invalid length. 6 A sequential READ was attempted but no valid next record had been established. 7 A READ was attempted for a file not open in the input or I-O mode. 8 A WRITE was attempted on a file not open in the I-O, output, or extend mode. 9 A DELETE or REWRITE was attempted on a file not open in the I-O mode. 9 Implemention Defined 0 1 2 3 4 5 6 7 No further information. VSAM password error. Logic error. VSAM resource not available. No file position indicator for sequential request. Invalid or incomplete VSAM file information. No DD statement specified for VSAM file. File integrity verified for VSAM file.
13.2. EXERCISES
The following problems are only models. You can select similar problems and write programs.
www.mainframe230.blogspot.com
Page 83
date:16.05.2010
COBOL Training 3. Write a program to display the class obtained by the student based on the marks obtained in three subjects as per the following rules. Basic Condition :.In no subject the boy should have got < 40 Condition For Class III Class : 40 or More but Less than 50 II Class : 50 or More but Less than 60 I Class : 60 or More but Less than 80 DISTINCTION : 80 or More
UNIT OF MEASUREMENT XXX. 4. Write a COBOL Program to maintain the above Stock Master and do necessary update process on Issue/Receipt of the Item. Transaction entries may be saved in a PS Data Set.
www.mainframe230.blogspot.com
Page 84
date:16.05.2010
COBOL Training
13.3. JCL
IGYWCL is a catalogued procedure to Compile and Linkedit any COBOL Program. IGYWCLG is a catalogued procedure to Compile, Linkedit and execute any COBOL Program. JCL for IGYWCL
//&SYSUIDZZ JOB CLASS=B,MSGCLASS=X,MSGLEVEL=(1,1),NOTIFY=&SYSUID, // REGION=2M,TIME=(00,45) //* ---------------------------------------------------------------//* ------------COMPILING & LINK EDITING---------------------------//* ---------------------------------------------------------------//* CHECK JOBNAME AND USERID BEFORE SUBMITTING //* THIS JCL COMPILES AD/CYCLE COBOL/370 PROGRAM SPECIFIED BY //* COB.SYSIN DD NAME AND PRODUCES LOAD MODULE IN THE DATA SET //* MEMBER SPECIFIED BY LKED.SYSLMOD DD NAME . //* ---------------------------------------------------------------//CMPCOB EXEC IGYWCL or IGYWCLG //COBOL.SYSIN DD DSN=&SYSUID.TRG1.COBOL(MEMBER1),DISP=SHR //LKED.SYSLMOD DD DSN=&SYSUID.TRG1.LOAD(MEMBER1),DISP=SHR //
www.mainframe230.blogspot.com
Page 85
date:16.05.2010