User manual for cobol

2,384 views

Published on

User manual for cobol

Published in: Education, Technology
0 Comments
9 Likes
Statistics
Notes
  • Be the first to comment

No Downloads
Views
Total views
2,384
On SlideShare
0
From Embeds
0
Number of Embeds
8
Actions
Shares
0
Downloads
0
Comments
0
Likes
9
Embeds 0
No embeds

No notes for slide

User manual for cobol

  1. 1. User Manual for COBOL_______________________________________________________________________________ COBOL Training Material_______________________________________________________________________________ COBOL 1
  2. 2. User Manual for COBOL_______________________________________________________________________________Table of Contents1. OBJECTIVES...........................................................................................................92. INTRODUCTION TO COBOL................................................................................102.1. LANGUAGE CONSTRUCTION..........................................................................102.2. COBOL CHARACTER SET................................................................................102.3. COBOL WORDS.................................................................................................102.4. COBOL CODING FORM.....................................................................................112.5. DIVISIONS OF COBOL......................................................................................113. IDENTIFICATION DIVISION..................................................................................123.1. SYNTAX OF IDENTIFICATION DIVISION.........................................................124. ENVIRONMENT DIVISION....................................................................................134.1. CONFIGURATION SECTION.............................................................................134.1.1. SOURCE-COMPUTER....................................................................................134.1.2. OBJECT COMPUTER.....................................................................................134.1.3. SPECIAL-NAMES............................................................................................134.2. INPUT-OUTPUT SECTION.................................................................................154.2.1. FILE-CONTROL...............................................................................................154.2.2. I-O CONTROL..................................................................................................155. DATA DIVISION.....................................................................................................175.1. LITERALS AND FIGURATIVE CONSTANTS....................................................175.1.1. NUMERIC LITERAL.........................................................................................18_______________________________________________________________________________ COBOL 2
  3. 3. User Manual for COBOL_______________________________________________________________________________5.1.2. NON-NUMERIC LITERAL...............................................................................185.1.3. LIST OF FIGURATIVE CONSTANTS............................................................185.2. DATA DESCRIPTION.........................................................................................195.2.1. LEVEL NUMBERS...........................................................................................195.2.1.1. SPECIAL LEVEL NUMBERS.......................................................................205.2.2. DATA NAMES..................................................................................................205.2.2.1. FILLER..........................................................................................................215.2.3. CLAUSE...........................................................................................................215.2.3.1. PICTURE CLAUSE.......................................................................................225.2.3.1.1. EDITED PICTURE CLAUSES...................................................................225.2.3.1.2. EDIT TYPES - NUMERIC DATA...............................................................225.2.3.1.3. EDIT TYPES - ALPHABETIC....................................................................245.2.3.1.4. EDIT TYPES - ALPHANUMERIC..............................................................245.2.3.1.5. EDIT COMBINATION................................................................................245.2.3.2. VALUE CLAUSE...........................................................................................255.2.3.3. USAGE CLAUSE..........................................................................................255.2.3.3.1. DISPLAY USAGE......................................................................................255.2.3.3.2. COMPUTATIONAL(COMP) USAGE........................................................265.2.3.4. SIGN CLAUSE..............................................................................................275.2.3.5. OCCURS CLAUSE.......................................................................................285.2.3.6. REDEFINES CLAUSE..................................................................................285.2.3.6.1. EXPLICIT USE OF REDEFINES...............................................................28_______________________________________________________________________________ COBOL 3
  4. 4. User Manual for COBOL_______________________________________________________________________________5.2.3.6.2. RESTRICTIONS ON USE OF REDEFINES..............................................295.2.3.7. RENAMES.....................................................................................................295.2.3.8. JUSTIFIED CLAUSE....................................................................................295.2.4. CONDITION NAMES.......................................................................................306. PROCEDURE DIVISION........................................................................................316.1. STATEMENTS IN PROCEDURE DIVISION.......................................................316.2. DATA MOVEMENT.............................................................................................336.2.1. MOVE...............................................................................................................336.2.1.1. DATA MOVEMENT RULES.........................................................................336.2.1.2. NUMERIC DATA TRANSFER RULES.........................................................336.2.1.3. ALPHANUMERIC DATA TRANSFER.........................................................346.2.1.4. GROUP MOVES............................................................................................346.2.1.5. MOVE CORRESPONDING...........................................................................356.3. ARITHMETIC VERBS ........................................................................................366.3.1. ADD..................................................................................................................366.3.2. SUBTRACT......................................................................................................376.3.3. MULTIPLY........................................................................................................376.3.4. DIVIDE..............................................................................................................376.3.5. ROUNDED OPTION.........................................................................................386.3.6. ON SIZE ERROR.............................................................................................396.3.7. COMPUTE........................................................................................................406.3.8. ARITHMETIC OPERATORS............................................................................40_______________________________________________________________________________ COBOL 4
  5. 5. User Manual for COBOL_______________________________________________________________________________6.4. MISCELLANEOUS..............................................................................................416.4.1. ACCEPT...........................................................................................................416.4.2. DISPLAY..........................................................................................................426.4.3. CONTINUE.......................................................................................................426.4.4. STOP RUN.......................................................................................................426.4.5. EXIT..................................................................................................................426.4.6. INITIALIZE........................................................................................................426.5. CONDITIONAL AND UNCONDITIONAL TRANSFER.......................................446.5.1. SIMPLE IF STATEMENT.................................................................................446.5.2. NESTED IF.......................................................................................................456.5.3. EVALUATE STATEMENT...............................................................................456.5.4. GO TO STATEMENT.......................................................................................466.5.5. GO TO WITH DEPENDING.............................................................................466.5.6. ALTER..............................................................................................................476.6. PERFORM STATEMENTS.................................................................................476.6.1. BASIC PERFORM............................................................................................476.6.2. PERFORM WITH TIMES PHRASE.................................................................496.6.3. PERFORM WITH UNTIL PHRASE..................................................................496.6.4. PERFORM WITH VARYING PHRASE............................................................506.7. CONDITIONAL EXPRESSIONS.........................................................................516.7.1. RELATIONAL CONDITION.............................................................................516.7.2. SIGN CONDITION............................................................................................52_______________________________________________________________________________ COBOL 5
  6. 6. User Manual for COBOL_______________________________________________________________________________6.7.3. CLASS CONDITION........................................................................................526.7.4. CONDITION-NAME CONDITION....................................................................536.7.5. NEGATED SIMPLE CONDITION....................................................................536.7.6. COMPOUND CONDITION...............................................................................537. FILE HANDLING....................................................................................................537.1. SEQUENTIAL FILES..........................................................................................547.2. INDEXED FILES..................................................................................................547.3. DIRECT ACCESS FILES....................................................................................547.4. STEPS INVOLVED IN FILE HANDLING............................................................547.4.1. EXAMPLE........................................................................................................567.5. SELECT STATEMENT.......................................................................................567.5.1. ORGANIZATION CLAUSE..............................................................................577.5.2. ACCESS MODE CLAUSE..............................................................................587.5.3. RECORD KEY PHRASE..................................................................................587.5.4. ALTERNATE KEY PHRASE...........................................................................587.6. FD ENTRY...........................................................................................................587.6.1. BLOCK CONTAINS / RECORD CONTAINS PHRASE..................................597.6.2. RECORDING MODE CLAUSE.......................................................................597.7. INPUT / OUTPUT STATEMENTS......................................................................617.8. VSAM DATA SET...............................................................................................627.8.1. VSAM DATASETS AND ACCESS MODES....................................................637.8.1.1. ORGANIZATION CLAUSE...........................................................................63_______________________________________________________________________________ COBOL 6
  7. 7. User Manual for COBOL_______________________________________________________________________________7.8.2. ENVIRONMENT DIVISION ENTRIES FOR VSAM FILES..............................647.8.3. COBOL STATEMENTS USABLE WITH SEQUENTIAL FILES.....................657.8.4. COBOL STATEMENTS WITH INDEXED FILES (KSDS) AND RELATIVE(RRDS).......................................................................................................................657.8.5. PROCEDURE DIVISION STATEMENTS TO UPDATE VSAM FILES...........667.8.6. ERROR PROCESSING FOR VSAM................................................................677.8.7. APPLICATIONS BEST SUITED TO VSAM DATASETS................................677.8.7.1. ESDS.............................................................................................................677.8.7.2. KSDS.............................................................................................................687.8.7.3. RRDS.............................................................................................................688. SUBPROGRAMS...................................................................................................698.1. LINKAGE SECTION............................................................................................698.1.1. STATIC / DYNAMIC CALLS............................................................................698.1.2. PASSING RETURN CODE INFORMATION...................................................719. STRING , UNSTRING AND INSPECT STATEMENTS.........................................729.1. STRING STATEMENT........................................................................................729.2. UNSTRING STATEMENT...................................................................................739.3. INSPECT STATEMENT......................................................................................749.4. DECLARATIVES.................................................................................................7610. SORT AND MERGE STATEMENTS...................................................................7711. INTRINSIC FUNCTIONS.....................................................................................7911.1. NUMBER HANDLING......................................................................................79_______________________________________________________________________________ COBOL 7
  8. 8. User Manual for COBOL_______________________________________________________________________________11.2. DATE / TIME....................................................................................................7911.3. FINANCE..........................................................................................................8011.4. MATHEMATICS & STATISTICS.....................................................................8012. COMPILER OPTIONS........................................................................................8113. APPENDIX...........................................................................................................8313.1. ERROR CODES...............................................................................................8313.2. EXERCISES:....................................................................................................8413.2.1. BASIC PROBLEM SOLVING........................................................................8413.2.2. REPORT PREPARATION.............................................................................8513.2.3. ARRAY HANDLING.......................................................................................8613.2.4. FILE HANDLING............................................................................................8613.3. JCL...................................................................................................................87_______________________________________________________________________________ COBOL 8
  9. 9. User Manual for COBOL_______________________________________________________________________________1. OBJECTIVESThe 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._______________________________________________________________________________ COBOL 9
  10. 10. User Manual for COBOL_______________________________________________________________________________2. INTRODUCTION TO COBOLCOBOL => COMMON BUSINESS ORIENTED LANGUAGECobol can be referred to as : 1. High-Level Programming language oriented towards business applications. 2. A procedure oriented language. A Procedure is a module having its own task. A Problem may be segmented into several tasks. Each task is written as a Paragraph in Procedure Division and executed in a logical sequence as mentioned. 3. Problem Oriented and Machine independent2.1. LANGUAGE CONSTRUCTION ♦ COBOL is like an English language. ♦ Character is the lowest component. ♦ Word is made up of one or more characters. ♦ Clause consists of word and characters. It specifies an attribute for an entry ♦ Statement is syntactically valid combination of words and clauses ♦ 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 divisions2.2. COBOL CHARACTER SETSet of 52 characters 0,..9 DIGITS A,..Z LETTERS b SPACE/BLANK CHARACTER +,-,*,/,(,),= SPECIAL SYMBOLS $,;,“,>,<,.,:2.3. COBOL WORDSUser defined words_______________________________________________________________________________ COBOL 10
  11. 11. User Manual for COBOL_______________________________________________________________________________ ♦ Words declared by programmer to coin identifiers, paragraph names, file names and section names.Reserved words ♦ Words which are having specific meaning to the compiler.2.4. COBOL CODING FORMColumns1- 6 Sequence numbers identifying pages or lines of a program7 Continuation, comment or starting of a new page8-11 Area A12-72 Area B73-80 Remarks2.5. DIVISIONS OF COBOL ♦ IDENTIFICATION DIVISION ♦ ENVIRONMENT DIVISION ♦ DATA DIVISION ♦ PROCEDURE DIVISION_______________________________________________________________________________ COBOL 11
  12. 12. User Manual for COBOL_______________________________________________________________________________3. IDENTIFICATION DIVISIONThe Identification division is: ♦ First division of a Cobol program. ♦ 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.3.1. SYNTAX OF IDENTIFICATION DIVISIONIDENTIFICATION DIVISION.PROGRAM-ID. PROGRAM NAME.AUTHOR.COMMENT ENTRY.INSTALLATION.COMMENT ENTRY.DATE-WRITTEN.COMMENT ENTRY.DATE-COMPILED.COMMENT ENTRY.SECURITY.COMMENT ENTRY.]Security here does not pertain to the operating system security, but the information that is passed tothe user of the program about the Security features of the program._______________________________________________________________________________ COBOL 12
  13. 13. User Manual for COBOL_______________________________________________________________________________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. CONFIGURATION SECTIONMentions the name of the source-computer, object-computer, special-names, symbolic-names andclasses defined by programmer.The paragraphs of configuration section are SOURCE-COMPUTER, OBJECT-COMPUTER, andSPECIAL-NAMES.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.2. OBJECT COMPUTER ♦ Used to specify the target system. ♦ The program collating sequence can also be specified in this paragraph for SORTING and STRING COMPARISON. By default, EBCDIC code is followed in MVS. We can change it to ASCII, or any other order if we define SPECIAL-NAMES.4.1.3. SPECIAL-NAMES ♦ Specifies collating sequence._______________________________________________________________________________ COBOL 13
  14. 14. User Manual for COBOL_______________________________________________________________________________ ♦ 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._______________________________________________________________________________ COBOL 14
  15. 15. User Manual for COBOL_______________________________________________________________________________4.2. INPUT-OUTPUT SECTION ♦ Contains information regarding files to be used in the program. ♦ Consists of two paragraphs FILE-CONTROL and I-O CONTROL.4.2.1. FILE-CONTROLAssociates 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 entriesCoding rules for FILE-CONTROL paragraph. ♦ SELECT clause must appear first ♦ Other clauses may appear in any order ♦ Each clause must start in Area B4.2.2. I-O CONTROL ♦ Optional Paragraph. ♦ Specifies when check points are to be taken. ♦ Specifies the storage areas to be shared by different files. ♦ The key word I-O control must begin in Area A._______________________________________________________________________________ COBOL 15
  16. 16. User Manual for COBOL_______________________________________________________________________________Example:IDENTIFICATION DIVISION.PROGRAM-ID. DSQ001.AUTHOR. A343.INSTALLATION. AT DSQ. MAD.DATE-WRITTEN. 06-04-94.DATE-COMPILED. XX-XX-XX.SECURITY. ONLY FOR TRAINEES.ENVIRONMENT DIVISION.CONFIGURATION SECTION.SOURCE-COMPUTER. ES-9000 WITH DEBUGGING MODE.OBJECT-COMPUTER. ES-9000 PROGRAM COLLATING SEQUENCE IS A01.SPECIAL-NAMES. C12 IS BOTTOM OF PAGE. ALPHABET A01 IS “A” ALSO “a” “B” ALSO “b” ALPHABET A IS STANDARD-1. SYSIN IS PROGIN CURRENCY IS @ DECIMAL POINT IS COMMAINPUT-OUTPUT SECTION.FILE-CONTROL SELECT OUTFILE ASSIGN TO DD1 ORGANIZATION IS SEQUENTIAL ACCESS MODE SEQUENTIAL FILE STATUS IS WS-FST._______________________________________________________________________________ COBOL 16
  17. 17. User Manual for COBOL_______________________________________________________________________________5. DATA DIVISIONData division is used to define data needed to be accessed by the program. The three sections of thisdivision areFILE SECTION ♦ Describes Record Structure of FilesWORKING-STORAGE SECTION ♦ Succeeds file section ♦ Used for intermediate storage ♦ Common practice to group related items together.LINKAGE SECTION ♦ Describes data made available from another program.5.1. LITERALS AND FIGURATIVE CONSTANTS. ♦ Literal is a constant representing a number or Non number (Alpha numeric) like 45.3 / ‘MANAGER’. ♦ Literals are storable in a memory Location having a user defined name called Data Name or Identifier or ♦ Variable or Field ♦ Figurative Constant is a COBOL RESERVED WORD representing frequently used constants like ♦ ZEROS / SPACES. ♦ Figurative constants are used in the program as such for better readability as. ♦ MOVE SPACES TO WS-REC. ♦ Literals are classified in to NUMERIC and NON-NUMERIC literals._______________________________________________________________________________ COBOL 17
  18. 18. User Manual for COBOL_______________________________________________________________________________5.1.1. NUMERIC LITERAL ♦ Must contain at least one digit ♦ 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.. ♦ Here 100 is a numeric constant assigned in identifier TOTAL-MARK.5.1.2. NON-NUMERIC LITERAL ♦ 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 chars05 COMPANY-NAME PIC X(3) VALUE ‘DSQ’.Here ‘DSQ’ is a Alphanumeric constant assigned in identifier COMPANY-NAME,5.1.3. LIST OF FIGURATIVE CONSTANTSZERO/ZEROES/ZEROS ONE OR MORE ZEROSQUOTE/QUOTES ONE OR MORE“SPACE/SPACES ONE OR MORE SPACEHIGH VALUE/HIGHEST VALUE HIGHEST VALUE IN COLLATING SEQUENCELOW VALUE/LOWEST VALUE LOWEST VALUE IN COLLATING SEQUENCEALL ONE or MORE STRING CHARACTERS_______________________________________________________________________________ COBOL 18
  19. 19. User Manual for COBOL_______________________________________________________________________________5.2. Data DescriptionThe general format of the data description is ♦ Level-number data-name/FILLER clauses5.2.1. LEVEL NUMBERS ♦ A Record is a Collection of all the fields pertaining to an Item/person/Transaction. ♦ A record structure is a structure of all the fields of a record. It may be available in the FILE SECTION/ WORKING-STORAGE SECTION/ LINKAGE SECTION. ♦ The level-number specifies the hierarchy of data within a record, and identifies special- purpose data entries. A level-number begins a data description entry, a renamed or redefined item, or a condition-name entry. ♦ A level-number has a value taken from the set of integers between 1 and 49, or from one of the special level-numbers, 66, 77, or 88.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.02-49 These level numbers specify group and elementary items within a record. ♦ They may begin in Area A or B ♦ Group Level Data Names Will not have Picture Clause. EMPLOYEE-REC. EMP-NO PIC XXXX. EMP-NAME 03 FIRST-NAME PIC A(30). 03 INITIAL-1 PIC A. 03 INITIAL-2 PIC A. 03 INITIAL-3 PIC A._______________________________________________________________________________ COBOL 19
  20. 20. User Manual for COBOL_______________________________________________________________________________5.2.1.1. SPECIAL LEVEL NUMBERS66 Identifies items that contain the RENAMES clause.77 Identifies independent elementary data item.Identifies any condition name that is associated with a particular value of conditional variableLevel Number 01 can appear as a Group Level Number as well as Elementary data item level number.It can appear only as an elementary Data Item Level Number.Example01 TOTAL PIC 9(5).77 WS=FLAG PIC XX.5.2.2. Data Names ♦ Identifier name/ Data name should not exceed 30 Characters. ♦ There should be at least one Alphabet anywhere in the name. ♦ Only special Character allowed is – ( Hyphen) but should not be at the Beginning or EndExamples of datanames ♦ EMPLOYEE-NAME , COMPANY, 0001AB, 999-EMPLOYEE-RECORD ♦ Dataname must be unique within a Record ♦ 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._______________________________________________________________________________ COBOL 20
  21. 21. User Manual for COBOL_______________________________________________________________________________5.2.2.1. FILLERWhenever there is no need for specific reference to fields in a record structure, we name it asFILLER Or F . This Data name is used wherever there is fixed information like SPACES OrHeadingsAre stored.Example 01 Detail-record. 05 FILLER PIC X(10) VALUE SPACES. 05 NAME PIC X(30) . 05 FILLER PIC X(10) VALUE SPACES. . 05 BASIC PIC 9(05) . 05 FILLER PIC X(10) VALUE SPACES. 05 DA PIC 9(04) . 05 FILLER PIC X(10) VALUE SPACES. 05 GROSS PIC 9(6).5.2.3. CLAUSEA Clause specifies certain characteristics of the data item being described.The detail of each Clause is explained below._______________________________________________________________________________ COBOL 21
  22. 22. User Manual for COBOL_______________________________________________________________________________5.2.3.1. PICTURE CLAUSEDescribes the class (numeric, alphabetic, alphanumeric), sign(signed/unsigned), decimal pointlocation and size characteristics of a data item.Specified for every elementary itemFormat of Picture Clause.PICTURE character string TYPE CHARACTER STRING REMARK Numeric 9 Used for arithmetic operations V Assumed Decimal Point S Data item is Signed P Position of Assumed decimal point when the point lies outside the data item Alphabetic A Data Item contains only a Letter Or Space B Blank insertion character Alpha X Data item contains any allowable character Numeric from the COBOL character set.EXAMPLESVAR1 PIC A(04) (Defines 4 alphabetic characters.)VA R2 PIC 9(4) (Defines 4 Numeric digits)5.2.3.1.1. EDITED PICTURE CLAUSESUsed to display Numeric and other data in the human readable form.Displaying the number as 77,419.56 is easier to read instead of 7741958.5.2.3.1.2. EDIT TYPES - NUMERIC DATAZ ZERO SUPPRESSION_______________________________________________________________________________ COBOL 22
  23. 23. User Manual for COBOL_______________________________________________________________________________* ASTERISK$ CURRENCY SIGN- MINUS SIGN+ PLUS SIGNCR DB CREDIT DEBIT SIGNPERIOD(.) COMMA(,) BLANK(‘b‘) ZERO(0) SLASH(/)BLANK WHEN ZERO (Inserts blanks when data value is zero)_______________________________________________________________________________ COBOL 23
  24. 24. User Manual for COBOL_______________________________________________________________________________5.2.3.1.3. EDIT TYPES - ALPHABETICUsed To Insert Blank within Alphabetic Characters.Example: 10 Var1 PIC A(2)BBBA(3) VALUE “xyabc”5.2.3.1.4. EDIT TYPES - ALPHANUMERIC BLANK, ZERO and SLASH INSERTION PIC OF THE NUMERIC VALUE EDITED VALUE FIELD ZZZV99 38^4 b3840 * * 999 00052 * * 052 $ * *999 985 $**985 -ZZZV99 -46^52 -b4652 +999 -382 -382 +999 382 +382 9999+ -382 0382- ZZ,Z99 2456 b2,456 ZZZZ.ZZ 5 bbbb.05 $$$$9.99 342 b$342.00 99B99B99 46 00b00b46 09990 456 04560 999/999/99 3254 000/032/54 BLANK WHEN ZERO ZZZ.99 BLANK WHEN ZERO 25 bb2.50 999.99 BLANK WHEN ZERO 0 bbbbbb5.2.3.1.5. EDIT COMBINATION_______________________________________________________________________________ COBOL 24
  25. 25. User Manual for COBOL_______________________________________________________________________________+, -, CR, DB Are Mutually Exclusive$, +, -, Z, * Are Mutually Exclusive.$ can appear at first place and * as Floating($****.**)V and . Are Mutually Exclusive5.2.3.2. VALUE CLAUSEAssigns initial value to a data itemVALUE IS literalLiteral can be Numeric, Non-Numeric(in Quotes) or Figurative ConstantNot for items whose size is variableValue of item should not exceed picture sizeConsistent with class of PIC clauseEXAMPLES01 WS-REC. 05 WS-NAME PIC X(30) VALUE ‘ABC COMPANY ‘. 05 FILLER PIC X(20) VALUE ALL ‘ - ‘.01 WS-NUM1 PIC 9(5) VALUE ZERO.01 WS-CHAR2 PIC X(5) VALUE ZERO.5.2.3.3. USAGE CLAUSESpecifies how a data item is represented internally. ♦ DISPLAY ♦ COMPUTATIONALSYNTAXUSAGE IS [DISPLAY ][COMPUTATIONAL] [COMP-1] [COMP-2] [COMP-3]5.2.3.3.1. DISPLAY USAGE.Each character of the data is represented in one byte .The number of bytes required is equal to the size of the item.DISPLAY is the default usage_______________________________________________________________________________ COBOL 25
  26. 26. User Manual for COBOL_______________________________________________________________________________5.2.3.3.2. COMPUTATIONAL(COMP) USAGE ♦ Maintained in binary ♦ Only integral numbers ♦ Depending on the size of the data item, it can be stored either in half word or full word ♦ Should be numeric only ♦ NUMBER OF digits IN PIC LENGTH OF ITEM IN BYTES 1 TO 4 2 5 TO 9 4 10 TO 18 801 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 operationsWS - NUM USAGE COMP1 .COMP-2 USAGE. ♦ Same as COMP 1 except that data is represented internally in two words ♦ Increases the precision of the data_______________________________________________________________________________ COBOL 26
  27. 27. User Manual for COBOL_______________________________________________________________________________COMP-3 USAGE. ♦ 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 signEXAMPLE FOR USAGE COMP - 310 A PIC S9(3) USAGE COMP-3 VALUE 123.Internally stored as0001 0010 0011 11111 2 3 F10 A PIC S9(04) USAGE COMP-3 VALUE 123.is stored as0000 0000 0001 0010 0011 1111(Extra Byte)5.2.3.4. SIGN CLAUSE ♦ [SIGN IS][LEADING] [SEPARATE CHAR ] [TRAILING] ♦ Specifies the position and mode of representation of sign ♦ Only for numeric elementary items ♦ Picture string should contain ‘S’ ♦ Usage should be DISPLAY ♦ Default is TRAILING without separate characterEXAMPLE PIC VALUE SIGN REPRESENTATION S9(3) -243 LEADING K 4 3 S9(3) -243 TRAILING 2 4 L_______________________________________________________________________________ COBOL 27
  28. 28. User Manual for COBOL_______________________________________________________________________________5.2.3.5. OCCURS CLAUSEThere may be a need to keep a table of values in the memory for calculations. We use OCCURSclause to allocate physically contiguous memory locations to store the table values and access themwith subscripts.Example,01 WS-TABLE. 10 WS-NAME OCCURS 10 TIMES PIC X(20).01 WS-TABLE. 10 WS-REC OCCURS 10 TIMES. 15 X PIC X(5). 15 Y PIC X(8).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.5.2.3.6. REDEFINES CLAUSEThis clause allows the same area of memory to be referenced by more than one data-name withdifferent formats and sizes.SYNTAX AND RULES< Lvl.no > <Data-item-1> REDEFINES <Data-item-2>.Redefines clause must immediately follow <data-item-1>Level nos of <data-item-1> and <data-item-2> must be identical and not 66 or 88.5.2.3.6.1. Explicit Use OF REDEFINESREDEFINES must be used for remapping record areas outside the FILE-SECTION, or within theFILE-SECTION if a part of a record is to be remapped._______________________________________________________________________________ COBOL 28
  29. 29. User Manual for COBOL_______________________________________________________________________________Example,FD CARDIN.01 D-TYPE-1 05 CARD-CODE PIC 9. 05 NAME-ADD. 10 NAME PIC X(20). 10 ADD-LN-1 PIC X(40). 05 NAME-DD REDEFINES NAME-ADD. 10 ADD-LN-2 PIC X(60).5.2.3.6.2. RESTRICTIONS ON USE OF REDEFINESRedefinition ceases whenever a level number is less than or equal to that of <data-item-1 > or <data-item-2>Multiple redefinition’s of the same area is possible provided there are no new storage areasdefined in between . < data-item - 1 > and <data-item-2>Value clauses are allowed only in condition names in a redefined data-item.At 01 level, OCCURS and REDEFINES cannot be combined<data-name-2> may not have an OCCURS clause5.2.3.7. RENAMESRegrouping of elementary data items in a Record.01 W-RESPONSE 05 W-CHAR-123 PIC XXX. 05 W-CHAR-4 PIC X. 05 W-CHAR-56 PIC XX. 66 ADD-RESPONSE RENAMES W-CHAR-123. 66 VIEW-RESPONSES RENAMES W-CHAR-123 THRU W-CHAR-4. 66 DELETE-RESPONSE RENAMES W-CHAR-123 THRU W-CHAR-56.5.2.3.8. JUSTIFIED CLAUSE_______________________________________________________________________________ COBOL 29
  30. 30. User Manual for COBOL_______________________________________________________________________________Overrides standard positioning rules for a receiving stem of the alphabetic or alphanumericcategories.01 WS-CHAR PIC X(05) JUSTIFIED RIGHT.Default is left justifiedJustified clause does not alter initial settings as determined by the value clauseJustified clause must not be specified with level 66(RENAMES) and level 88(condition-names)clauses.5.2.4. CONDITION NAMES ♦ Allow users to assign acceptable values for data names ♦ Are used as an abbreviation for condition checking01 W-PERSON-STATUS PIC 9(02). 88 C-PER-MINOR VALUE O THRU 20 88 C-PER-ADULT VALUE 21 THRU 99_______________________________________________________________________________ COBOL 30
  31. 31. User Manual for COBOL_______________________________________________________________________________6. PROCEDURE DIVISION ♦ Last division in a COBOL program ♦ Contains statements which specify the operations to be performed ♦ Contains the following structureSTRUCTURE PROCEDURE DIVISION SECTION PARAGRAPHS SENTENCES STATEMENTS VERBS ♦ 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 name6.1. STATEMENTS IN PROCEDURE DIVISIONProcedure division statements can be broadly classified as ♦ Imperative Statements ♦ Statement which directs the program to take a specific action during executionExamples MOVE, ADD, GOTO, EXIT. ♦ Conditional Statements ♦ A conditional statement specifies that the truth value of a condition is to be determined, and that the subsequent action of the object program is dependent on this truth value.Examples: IF , EVALUATE , ADD...ON SIZE ERROR, ADD...NOT ON SIZE ERROR COMPUTE...ON SIZE ERROR, COMPUTE...NOT ON SIZE ERROR ♦ Compiler DirectivesA Compiler-Directing Statement is a statement, beginning with a compiler directing verb, that causesthe compiler to take a specific action during compilation.Example : Use, Copy_______________________________________________________________________________ COBOL 31
  32. 32. User Manual for COBOL_______________________________________________________________________________ ♦ Explicit Scope TerminatorAn EXPLICIT SCOPE TERMINATOR terminates the scope of conditional statements. This includesstatements which contain conditional expression (like IF statement) and imperative statements (likeADD)ExamplesEND-ADD, END-IF, END-COMPUTE, END-DELETE, END-SUBTRACT, END-EVALUATE,END-CALL,END-MULTIPLY ♦ Implicit Scope TerminatorAt the end of any sentence, an IMPLICIT SCOPE TERMINATOR is a separator period thatterminates the scope of all previous statements not yet terminated._______________________________________________________________________________ COBOL 32
  33. 33. User Manual for COBOL_______________________________________________________________________________6.2. Data Movement6.2.1. MOVETo move data into a place in memory.Format,MOVE {IDENTIFIER-1/LITERAL-1} TO IDENTIFIER-2[,IDENTIFIER-3],..6.2.1.1. DATA MOVEMENT RULESOnly one sending field , One or more receiving fields.Value of the sending field remains unaltered after the statement execution.6.2.1.2. NUMERIC DATA TRANSFER RULESWhen sending field is numeric or numeric-edited, the Data Movement is called Numeric.The dominant factor in the numeric data transfer is the alignment of decimal points of the two fieldsIf the decimal point is not explicitly indicated, the decimal point is assumed to be at the right of therightmost digitIf the receiving field is not large enough to hold the data received, truncation can take place at eitherends or at both endsIf the significant integral positions are likely to be lost, a warning to that effect is issued by thecompilerIf the receiving field is larger than the sending field, the unused positions will be filled with zeros,which is known as ‘ zero fill ‘_______________________________________________________________________________ COBOL 33
  34. 34. User Manual for COBOL_______________________________________________________________________________6.2.1.3. ALPHANUMERIC DATA TRANSFER.When both the sending and receiving fields are alphabetic, alphanumeric or alphanumeric edited, theData Movement is called as Alphanumeric.In the case of alphanumeric data transfer, receiving area is filled from left to rightWhen the receiving field is smaller, truncation occurs from the right and the compiler gives a warningto that effectIf the receiving field is larger than the field, the unused positions will be filled with spaces, which iscalled as ‘ space fill ‘ExamplesMOVE A TO BA BPIC 9999 PIC ZZZ9bb34MOVE 15 TO A.If A has picture of 999, after execution A will contain 015MOVE “DSQ SOFTWARE Ltd.“ TO COMPANY-MNAME.MOVE A TO B, C, D.6.2.1.4. GROUP MOVESWhen At Least One Of The Fields is a Group item, it is called A Group MoveEXAMPLE 01 REC-1. 05 A1 PIC 9999. 05 A2 PIC AA. 05 A3 PIC XXXX. 01 REC-2 05 B1 PIC 9999. 05 B2 PIC AA. 05 B3 PIC XXXX.MOVE REC-1 TO REC-2_______________________________________________________________________________ COBOL 34
  35. 35. User Manual for COBOL_______________________________________________________________________________6.2.1.5. MOVE CORRESPONDINGWhen the names of the corresponding data item of two groups are the same, this statement can beused to substitute for a set of move statementsMOVE CORRESPONDING IDENTIFIER-1 TO IDENTIFIER-2.Source and destination groups can include data names that are not commonOnly the fields having identical names in the two records will take part in the data movementsThe remaining data items in the destination group will remain unchangedEXAMPLE01 PAY-REC. 05 EMP-NO. PIC 9(5) 05 EMP-NAME PIC X(30). 05 EMP-BASIC PIC 9(5)V99.01 PRINT-REC. 05 FILLER PIC X(5). 05 EMP-NO PIC 9(5). 05 FILLER PIC X(5). 05 EMP-NAME PIC X(30). 05 FILLER PIC X(5). 05 EMP-SAL PIC 9(6).99.MOVE CORRESPONDING PAY-REC TO PRINT-REC._______________________________________________________________________________ COBOL 35
  36. 36. User Manual for COBOL_______________________________________________________________________________6.3. ARITHMETIC VERBS6.3.1. ADDThis verb is used to find the sum of two or more numbers and to store the resultant sumADD [CORRESPONDING] [IDENTIFIER-1 / LITERAL-1] [IDENTIFIER-2 / LITERAL-2] [TO / GIVING] IDENTIFIER-3, IDENTIFIER-4, ...Example:-ADD A TO BADD 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 summationand 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 thesummation and only the result is stored there.In the group add, numeric elementary items in the group referred to by first identifier are added toand 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 dataname and same qualifiersCorresponding items can have different locations within the group and the field sizes Can also bedifferent_______________________________________________________________________________ COBOL 36
  37. 37. User Manual for COBOL_______________________________________________________________________________6.3.2. SUBTRACTThis verb is used to subtract one or the sum of two or more numbers from one or more numbers.Subtract [CORR] [IDENTIFIER-1 / LITERAL-1] [IDENTIFIER-2 / LITERAL-2] [FROM / GIVING] IDENTIFIER-3, IDENTIFIER-4, ...If GIVING option is used literals can be used in place of identifier-4.Example:-SUBTRACT A FROM B.SUBTRACT A B FROM C.SUBTRACT A B FROM C D.SUBTRACT 10 FROM A.6.3.3. MULTIPLYMultiply verb is used to multiply one or more multiplicands by multiplier .MULTIPLY {IDENTIFIER-1 BY IDENTIFIER-2 [ , IDENTIFIER-3]... LITERAL - 1} [GIVING IDENTIFIER-4 [ , IDENTIFIER-5]...]If giving option is used, LITERALS can be used in place of identifier-2 and identifier-3.Example:-MULTIPLY A BY B.MULTIPLY A BY B C D.MULTIPLY A BY B GIVING C.MULTIPLY 0.5 BY B.6.3.4. DIVIDEDivide verb is used to divide one number by another._______________________________________________________________________________ COBOL 37
  38. 38. User Manual for COBOL_______________________________________________________________________________DIVIDE {Identifier-1 / Literal-1} INTO[BY] identifier-2 { ,identifier-3}.... [GIVING identifier-4 [ ,identifier-5]...] [REMAINDER identifier-6].ExampleDIVIDE 5 INTO A ..DIVIDE A INTO B GIVING C.DIVIDE A BY B GIVING C D.DIVIDE A BY B GIVING C REMAINDER D.6.3.5. ROUNDED OPTIONThis can be used with any arithmetic verb. It will round the number to the picture clause.ADD A B GIVING C ROUNDED.This phrase cannot be specified for the identifier that receives the remainder in a divide operation._______________________________________________________________________________ COBOL 38
  39. 39. User Manual for COBOL_______________________________________________________________________________6.3.6. ON SIZE ERRORIf after an arithmetic operation, the result exceeds the largest value that can be accommodated in theresult field, the error is called a “size error”.Can be used with any arithmetic verbWhen a size error occurs, the contents of the resultant field after the operation is unpredictable.When a size error occurs, the processing is not terminated and the next statement will be taken up forfurther execution.When this phrase is used, the statement becomes a conditional statement.Example:-ADD A TO B ON SIZE ERROR GO TO ERROR-PARA._______________________________________________________________________________ COBOL 39
  40. 40. User Manual for COBOL_______________________________________________________________________________6.3.7. COMPUTEThis verb is very powerful and is used as a substitute for any of the other arithmetic verbs like add,subtract, multiply, divide.COMPUTE IDENTIFIER-1 [ROUNDED] [ , IDENTIFIER-2 [ ROUNDED ] ] = ARITHMETIC-EXPRESSION [ : ON SIZE ERROR] IMPERATIVE STATEMENT.6.3.8. ARITHMETIC OPERATORSArithmetic expression is formed with numeric operands, numeric LITERALS and numeric operators..Arithmetic expression always assumes a numeric value.Arithmetic operators permitted in COBOL are ** => EXPONENTIATION. / => DIVISION. * => MULTIPLICATION.. - => SUBTRACTION. + => ADDITION._______________________________________________________________________________ COBOL 40
  41. 41. User Manual for COBOL_______________________________________________________________________________6.4. Miscellaneous6.4.1. ACCEPTAccept statement can transfer data from an input/output device, or system information contained inthe specified conceptual data items DATE, DAY, DAY-OF-WEEK, or TIMEFormatACCEPT IDENTIFIER [ FROM mnemonic name DATE DAY TIME DAY-OF-WEEK}]When ‘FROM’ is omitted, the data is read from the operator’s 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-NAMESparagraph.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 formatThe day-of-week option returns a single digit [9(1)] value as follows:1 - Monday, 2 - Tuesday, 3 - Wednesday and so on.ACCEPT THIS-DATE FROM DATE._______________________________________________________________________________ COBOL 41
  42. 42. User Manual for COBOL_______________________________________________________________________________6.4.2. DISPLAYDisplay statement is used to display data on the terminal.FormatDISPLAY {identifier-1 identifier-2 literal-1} , literal - 2 [upon mnemonic-name]ExampleDISPLAY “RESULT b IS “,THE-RESULT6.4.3. CONTINUEThe CONTINUE statement allows you to specify a no operation statement. CONTINUE indicatesthat no executable instruction is present.6.4.4. STOP RUNThis verb is used to terminate the execution of the program.6.4.5. EXITThe EXIT statement provides a common end point for a series of paragraphs.This statement indicates a no operation and when executed, no action takes place6.4.6. INITIALIZEThis statement is used to initialize value of either an elementary or group item._______________________________________________________________________________ COBOL 42
  43. 43. User Manual for COBOL_______________________________________________________________________________ INITIALIZE {identifier-1 } . . . REPLACING { ALPHABETIC ALPHANUMERIC NUMERIC ALPHA-NUMERIC-EDITED. NUMERIC-EDITED } DATA BY { identifier-2 literal-2 }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 bbbbINITIALIZE B REPLACING NUMERIC DATA BY 105 REPLACING ALPHANUMERIC DATA BY “ LOVE “ REPLACING NUMERIC-EDITED DATA BY 1500.23._______________________________________________________________________________ COBOL 43
  44. 44. User Manual for COBOL_______________________________________________________________________________6.5. CONDITIONAL AND UNCONDITIONAL TRANSFER6.5.1. SIMPLE IF STATEMENT.IF condition [THEN] statement-1ELSE statement-2END-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, thestatements represented in the THEN part will be executed. Statement-1 and Statement-2 representsone or more COBOL statementsIf the condition is found to be FALSE, the statements represented in the ELSE part will be executedAfter execution of this statement control will be implicitly transferred to the next sentence followingthe IF statement.Period (.) is placed only at the end of the IF structure. i.e. after the connective END-IF.Example:-IF AMOUNT > 5000 THEN ADD AMOUNT TO TOTAL-1ELSE ADD AMOUNT TO TOTAL-2END-IF.NEXT SENTENCE will be used to make either the THEN or ELSE part dummy._______________________________________________________________________________ COBOL 44
  45. 45. User Manual for COBOL_______________________________________________________________________________6.5.2. NESTED IFInclusion 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.6.5.3. EVALUATE STATEMENTUsed for decisions and can be used when many branches are thereCan be used as a substitute for nested if “sEVALUATE SUBJECT-1 [ALSO SUBJECT-2]... {WHEN OBJECT-1 [ALSO OBJECT-2}...} [WHEN OTHER {IMPERATIVE-STATEMENT-2}...][END-EVALUATE]A list of subjects and several lists of objects are associated with an Evaluate.A list of subjects is specified between the word EVALUATE and the first appearance of WHENSubject can be an Identifier / Literal /Expression or key words True / FalseEach WHEN specifies a list of objectsNumber of subjects and number of objects should tallySubjects and their corresponding objects should be comparableWHEN phrases are taken up for a “ match “ in the order they appear.Imperative statements corresponding to the matching WHEN will be executed.WHEN OTHER phrase is selected only if none of the previous WHEN phrases are selected.Objects can be Numeric value , Non-numeric value, Range of Numeric values, Range of non-numeric values, Conditional Values, keywordsExample:-_______________________________________________________________________________ COBOL 45
  46. 46. User Manual for COBOL_______________________________________________________________________________EVALUATE TRUE WHEN MONTH = 4 OR 6 OR 9 OR 11 MOVE 30 TO NO - OF - DAYS WHEN MONTH = 2 MOVE 28 TO NO - OF - DAYS WHEN OTHER MOVE 31 TO NO - OF - DAYS END-EVALUATE.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 COMMISSIONEND-EVALUATE.6.5.4. GO TO STATEMENT.It is an unconditional transferring control to the specified paragraph .There is no any comparison being made by the system before branching off .Example GOTO DISPLAY-PARA.Note:- It is advised to avoid GOTO Statements as the structured programmingTechnique does not support unconditional transfer statements.6.5.5. GO TO WITH DEPENDINGThis verb is used to conditionally transfer the control to elsewhere in the program.Depending on whether the value of the identifier is 1,2 . . . n , the control is transferred to procedure-name-1, procedure-name-2, . . . procedure-name-n respectively.If the value of the identifier is anything other than the specified range of 1, 2 . n, the said go to isineffective and the control is transferred to the next statement._______________________________________________________________________________ COBOL 46
  47. 47. User Manual for COBOL_______________________________________________________________________________GO TO PROCEDURE-NAME-1 [ , PROCEDURE-NAME-2 ]... PROCEDURE-NAME-NDEPENDING ON identifierThe statement transfers control to one of the procedures named in the statement depending on thevalue of the identifierThe identifier specified in the statement must be a numeric, integral, elementary item.Example:-GO TO RECEIPT-PARA, ISSUE-PARA, ADJUSTMENT-PARADEPENDING ON TRANSACTION - TYPE.6.5.6. ALTERThe alter statement can be used to modify the targets of goto statements written elsewhere in theprocedure division. ALTER PROCEDURE-NAME-1 TO [PROCEED TO ] PROCEDURE-NAME-2 [PROCEDURE-NAME-3 TO {PROCEED TO } PROCEDURE-NAME-4 ]....Each of the PROCEDURE-NAME-1, PROCEDURE-NAME-3 is the name of the paragraph thatcontains only one sentence.This sentence must consist of a single goto statement without the depending clause.During the execution each of the PROCEDURE-NAME-1 , PROCEDURE-NAME-3, . . . will bereplaced by PROCEDURE-NAME-2, PROCEDURE-NAME-4 ...respectively.6.6. PERFORM STATEMENTSA PERFORM statement is used to execute a group of consecutive statements specified elsewhere inthe program, under a paragraph.6.6.1. Basic PerformPERFORM PARA-name-1.Para-name-1 specifies the range , which contains statements to be executed_______________________________________________________________________________ COBOL 47
  48. 48. User Manual for COBOL_______________________________________________________________________________Upon execution of this statement the control is transferred to the beginning of the paragraph andexecutes all the statements in that paragraph till the last statement.After executing the said range, the control is implicitly , transferred to the next line after thePERFORM statementExample:- 100-MAIN-PARA. PERFORM 1000-ACCEPT-PARA. PERFORM 2000-COMPUTE-PARA.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 thepara-name-2If there are any other paragraphs placed in between these two paragraphs, those are also executedAfter executing the said range, the control is implicitly , transferred to the next line after thePERFORM statementExample:-PERFORM BEGIN-PARA THRU END-PARA._______________________________________________________________________________ COBOL 48
  49. 49. User Manual for COBOL_______________________________________________________________________________6.6.2. PERFORM with TIMES phrasePERFORM para-name-1 [THRU para-name-2] {identifier-1/ integer} TIMES.This is a looping statement which executes the specified range of statements a fixed no. of times.Example:- PERFORM READ-RTN 10 TIMES.This statement executes all the stmts. in READ-RTN 10 times without testing any condition.6.6.3. PERFORM with UNTIL phrasePERFORM para-name-1 [THRU para-name-2] UNTIL condition.This statement executes a series of instructions in para-name-1 repeatedly till the condition becomesTRUE.Initially the condition is expected to be false.The condition should be made true, within the paragraph being performed.If the condition is true initially, the range is not executed at allExample:- MOVE 0 TO I. PERFORM 000-CALC-PARA UNTIL I>10000-CALC-PARA. -- ADD 1 TO I.Here 000-CALC-PARA is performed 10 TIMES._______________________________________________________________________________ COBOL 49
  50. 50. User Manual for COBOL_______________________________________________________________________________6.6.4. PERFORM with VARYING phrase.PERFORM VARYING{identifier-1 FROM {identifier-2 /integer-1} BY {identifier-3 / integer-2} UNTIL Condition-1 AFTER identifier-1 FROM {identifier-2/integer-1} BY {identifier-3/integer-2} UNTIL Condition-2 [imperative statement]END-PERFORM.This is also a looping statement which executes imperative statement for all possible values ofidentifier-1 and identifier-3 until Condition-1 and Condition-2 are TRUE.ExamplePERFORM VARYING I FROM 1 BY 1 UNTIL I > 3AFTER J FROM 1 BY 1 UNTIL J > 4Display SALE ( I , J )END-PERFORM. The above example will take I and j vales as1,1 1,2 1,3 1,4 2,1 2,2 2,3 2,4 3,1 3,2 3,3 3,4_______________________________________________________________________________ COBOL 50
  51. 51. User Manual for COBOL_______________________________________________________________________________6.7. Conditional expressionsIn COBOL there are various types of conditions as follows :RELATIONAL CONDITIONSIGN CONDITIONCLASS CONDITION.CONDITION - NAME CONDITIONNEGATED SIMPLE CONDITION.COMPOUND CONDITION6.7.1. RELATIONAL CONDITIONA relation condition compares two operands, either of which can be an identifier, literal, arithmeticexpression, or index-name. A non-numeric literal can be enclosed in parentheses within a relationcondition.The relational operators are < <= > >= NOT GREATER THAN / LESSER THAN / EQUAL TO IF A > B DISPLAY “ A IS GREATER THAN B “ ELSE DISPLAY “ A IS LESS THAN OR EQUAL TO B “ END-IF ._______________________________________________________________________________ COBOL 51
  52. 52. User Manual for COBOL_______________________________________________________________________________6.7.2. SIGN CONDITIONThe sign condition determines whether or not the algebraic value of a numeric operand is greaterthan, less than, or equal to zero. The Possible SIGN conditions are POSITIVE / NEGATIVE /ZERO. IF CURR-STOCK-ISS-QTY IS NEGATIVE DISPLAY “ ISSUE CANNOT BE MADE” GO TO NO-STOCK-PARA END-IF.6.7.3. CLASS CONDITIONThe class condition determines whether the content of a data item is alphabetic,alphabetic-lower, alphabetic-upper, numeric, or contains only the characters in the set of charactersspecified by the CLASS clause as defined in the SPECIAL-NAMES paragraph of the EnvironmentDivision.The possible CLASS Conditions are ALPHABETIC / NUMERIC / ALPHANUMERIC IF A IS NUMERIC ADD 1 TO A ELSE DISPLAY “ NON - NUMERIC DATA FOR A “ GO TO ERROR-PARA END-IF._______________________________________________________________________________ COBOL 52
  53. 53. User Manual for COBOL_______________________________________________________________________________6.7.4. CONDITION-NAME CONDITIONA condition-name condition tests a conditional variable to determine whether its value is equal to anyvalue(s) associated with the condition-name.Condition names are not memory locations but names assigned to data ranges .Comparisons can be simplified . 01 WS-MARITAL-STATUS 88 SINGLE VALUE IS 0. 88 MARRIED VALUE IS 1. IF SINGLE ADD 1 TO SINGLE-COUNT.6.7.5. NEGATED SIMPLE CONDITIONA simple condition is negated through the use of the logical operator NOT. IF NOT MARRIED DISPLAY “ELIGIBLE FOR RS 1000 ONLY “ END-IF.6.7.6. COMPOUND CONDITIONTwo or more conditions can be logically connected to form a compound condition.The possible Compound Conditions are AND OR NOT . IF AGE IS LESS THAN 18 AND MARRIED DISPLAY “EARLY MARRIAGE “ END-IF.7. FILE HANDLING_______________________________________________________________________________ COBOL 53
  54. 54. User Manual for COBOL_______________________________________________________________________________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 ♦ RELATIVE7.1. Sequential FilesIf a file organization is Sequential then ♦ Records can be accessed in the order in which they appear in the file ♦ Records can be appended at the end of the file but can not be inserted. ♦ Records cannot be deleted7.2. Indexed FilesIndexed File Organization is to do random Processing or sequential processing.In this Organization ♦ Record can be accessed in any order by key called Primary Key. ♦ There can be more than one key called Alternate Keys to access records ♦ Records can be inserted added and deleted.7.3. Direct Access FilesThis record is best suited if there are records which can be accessed on record number basis ratherthan based on a field of the record.Each record has a unique address and is identified by a relative record number.7.4. STEPS INVOLVED IN FILE HANDLINGTo handle a file the following operations are to done.Step 1._______________________________________________________________________________ COBOL 54
  55. 55. User Manual for COBOL_______________________________________________________________________________ ♦ All files handled in a program should be assigned a logical name chosen by the programmer as per the syntax similar to Cobol identifier. The organization of the file ,DDNAME assigned for the dataset and its primary and alternate key, if any, have to be specified in the File-Control para of Environment Division. This is specified using SELECT statement of File-Control para.Step2. ♦ 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. This statements are coded in procedure division . Files can be opened for reading, writing ,appending .Step4. If we have to read a file READ statement is used in procedure Division. SYNTAX Read File -name. 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 WRITE Record -name.Step5. After read / write operations are over the file must be closed to disconnect the file from theCobol program. SYNTAX CLOSE File-name._______________________________________________________________________________ COBOL 55
  56. 56. User Manual for COBOL_______________________________________________________________________________7.4.1. EXAMPLEIDENTIFICATION DIVISION.PROGRAM-ID. F1.ENVIRONMENT DIVISION.INPUT-OUTPUT SECTION.FILE -CONTROL. SELECT CUSTOMER-FILE ASSIGN TO DD1. ORGANIZATION IS INDEXED. ACCESS MODE IS SEQUENTIAL. RECORD KEY IS CUST-NO. FILE STATUS KEY IS WS-FST1.FILE SECTION.FD CUSTOMER - FILE RECORDING MODE IS F RECORD CONTAINS 80 CHARACTERS DATA RECORD IS CUSTOMER - RECORD.01 CUSTOMER RECORD. 05 CUSTOMER - NAME PIC X(13). 05 CUSTOMER - UNITS PIC 9(3). 05 ADDRESS PIC X(64).WORKING-STORAGE SECTION..01 WS-FST1 PIC X(2).PROCEDURE DIVISION.000-MAIN-PARA. OPEN OUTPUT CUSTOMER-FILE. DIASPLAY WS-FST1.100-WRITE-PARA. ACCEPT CUSTOMER-NAME ACCEPT CUSTOMER-UNITS ACCEPT ADDRESS WRITE CUSTOMER-RECORD. Display WS-FST1.200-CLOSE -PARA. CLOSE CUSTOMER-FILE. STOP RUN.7.5. SELECT Statement_______________________________________________________________________________ COBOL 56
  57. 57. User Manual for COBOL_______________________________________________________________________________ Syntax : SELECT file-name ASSIGN TO DD Name ORGANIZATION IS [ SEQUENTIAL/INDEXED/RELATIVE] ACESS MODE IS [SEQUENTIAL/DYNAMIC/RELATIVE] [RECORD KEY data-name-1 ] [ALTERNATE KEY IS data-name-2. WITH DUPLICATE] FILE-STATUS IS data-name-2.7.5.1. ORGANIZATION CLAUSEThis clause should appear in Environment division.We can choose File OrganizationSEQUENTIAL This Option is to handle a PS data set or VSAM ESDS .Once the file is Organised as Sequential records can be placed as first come first served basis.INDEXED This Option is to handle a VSAM KSDS . There should be a key field to choose this operating. This is called primary key And is to be unique like ROLLNO, EMPLYEE NO.RELATIVE This Option is to handle a VSAM RRDS . Record Numbers areidentifying the records to access.._______________________________________________________________________________ COBOL 57
  58. 58. User Manual for COBOL_______________________________________________________________________________7.5.2. ACCESS MODE CLAUSEThis clause should appear in the access mode statement of Environment division.Files can be accessed in different modes based on the type of Organization.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 / RelativeFileprovided the Record key must be set before read / write operation.Dynamic Access Mode will facilitate to access a particular record or in sequential order froman Indexed File / Relative FileWe give below all possible access modes on organization. Seq. access Random access Dynamic access Organization Sequential Order of write Invalid Invalid Relative Ascending rel rec. no Value of rel key Seq. or random Indexed Ascending key value Value of rel key Seq. or random7.5.3. RECORD KEY PHRASEThis Phrase is to be defined in the Environment Division and essential when the file is an IndexedFile.This key is to be Unique and is to be a part of the Indexed record.7.5.4. ALTERNATE KEY PHRASEThis Phrase is to be defined in the Environment Division when we want to access record in the orderof some other Key apart from primary key.This key need not be unique but to be a part of the record.7.6. FD entry_______________________________________________________________________________ COBOL 58
  59. 59. User Manual for COBOL_______________________________________________________________________________Syntax FD FILENAME[BLOCK CONTAINS [int-1 TO ] int-2 RECORDS[CHARACTERS][RECORD CONTAINS [int-3 TO } int-4 CHARACTERS[LABEL RECORD/RECORDS IS/ARE STANDARD/OMITTEDRECORDING MODE IS [ F/ V][; DATA RECORD/RECORDS IS/ARE data-name-2]]record-name field1 Pic ..7.6.1. BLOCK CONTAINS / RECORD CONTAINS PHRASEWe can specify the length of the file record in this Clause in terms of Characters this is to be specifiedin Data Division. We can specify the number of characters / records contains in a Block.7.6.2. RECORDING MODE CLAUSEThis Phrase is to be defined in the File section Data division. When the file is with Fixed lengthrecord is chosen as F.This key represent s only the record Number.RECORDING MODE [F] [V].F => Fixed which Indicates that every record of same length FD BLOCK CONTAINS 500 CHARACTERS. RECORD CONTAINS 50 CHARACTERS. DATA-RECORD IS EMP-RECORD.EMP-RECORD01 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._______________________________________________________________________________ COBOL 59
  60. 60. User Manual for COBOL_______________________________________________________________________________V => Variable which Indicates that record length may vary from I1 to I2.ExampleFD BLOCK CONTAINS 320 THRU 640 CHARACTERS. RECORD CONTAINS 32 THRU 64 CHARACTERS.DATA RECORD IS MARK-RECORD.01 MARK-RECORD02 P1 PIC 99.02 MARKS OCCURS 10 THRU 20 Depending P1 03 SCORE PIC 999.The above record length is based on P1._______________________________________________________________________________ COBOL 60
  61. 61. User Manual for COBOL_______________________________________________________________________________7.7. INPUT / OUTPUT STATEMENTSOpen statement can be used for opening the file with different mode.OPEN OUTPUT file-name :- connects data set to your COBOL programto write records when the file is even with out a single record..OPEN INPUT file-name :- connects data set to your COBOL programto records from the first .OPEN EXTEND file-name :- connects data set to your COBOL program for writing new recordsin an existing non empty file.OPEN I-O file-name :- connects data set to your COBOL program for processingto do read/write /Rewrite.WRITE :- Add records to a file or load a file . When this command is used in an Indexed filekey must be newer one always.START Establish the current location in the cluster for a READ NEXT statementSTART does not retrieve a record , it only sorts the current record pointer described under FilePosition indicatorREAD statement is used to read a record from a file. If it is used to read a sequential file it willread the next record always . If it used to read from an Indexed/Relative file it will read thecorresponding key record, The key field must have value before read.If we have read sequentially from an Indexed file READ NEXT RECORD will retrieve recordsfrom a file in sequence REWRITE Update records. DELETE Logically remove records from indexed and relative files only CLOSE Disconnect the VSAM data set from your program_______________________________________________________________________________ COBOL 61
  62. 62. User Manual for COBOL_______________________________________________________________________________7.8. VSAM DATA SETCOMPARISON ON VSAM DATASETS Entry - sequenced Key - sequenced Relative - Record Records are in order in which Records are in collating Records are in relative record they are written sequence by key field number order Access is sequential Access is by key through an Access is by relative record index number , which is treated like a key May have one or more May have one or more May not have alternate indexes alternate indexes alternate indexes. A record’s RBA cannot change A record RBA can change A record relative record number cannot change Space at the end of the data set Distributed free space is used for fixed length RRDS empty is used for adding records for inserting record and slots in the data set are used changing their lengths in place for adding records. 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 you can reuse its space for a shortened record is record can be reused record of the same length automatically reclaimed within a control interval Can have spanned records can have spanned records Can be reused as a word file Can be reused as a word file can be reused as a work file can be reused as work file unless it has an alternate index unless it has an alternate index associated with key ranges or associated with key ranges or exceeds 123 extents per volume exceeds 123 extents per volume_______________________________________________________________________________ COBOL 62
  63. 63. User Manual for COBOL_______________________________________________________________________________7.8.1. VSAM DATASETS AND ACCESS MODES File Organization Seq. Ran. Dyna. Fixed Variable Access Access Access Length Length VSAM sequential Yes No No Yes Yes (ESDS) VSAM indexed Yes Yes Yes Yes Yes (KSDS) VSAM Relative Yes Yes Yes Yes NO (RRDS)7.8.1.1. ORGANIZATION CLAUSEORGANIZATION IS SEQUENTIAL - for VSAM sequential files (ESDS)ORGANIZATION IS INDEXED - for VSAM indexed files (KSDS)ORGANIZATION IS RELATIVE - for VSAM relative files (RRDS)._______________________________________________________________________________ COBOL 63
  64. 64. User Manual for COBOL_______________________________________________________________________________7.8.2. ENVIRONMENT DIVISION ENTRIES FOR VSAMFILESExample :VSAM Sequential File SELECT S -FILE ASSIGN TO SEQUENTIAL - FILE ORGANIZATION IS SEQUENTIAL ACCESS IS SEQUENTIAL FILE STATUS KEY IS FSTAT - CODE VSAT - CODEExample :VSAM Indexed File SELECT I - FILE ASSIGN TO INDEXED - FILE ORGANIZATION IS INDEXED ACCESS IS DYNAMIC RECORD KEY IS IFILE - RECORD - KEY ALTERNATE RECORD KEY IS IFILE - ALTREC - KEY FILE STATUS KEY IS FSTAT - CODE VSAM - CODEExample:- VSAM Relative File SELECT R - FILE ASSIGN TO RELATIVE - FILE ORGANIZATION IS RELATIVE ACCESS IS RANDOM RELATIVE KEY IS RFILE - RELATIVE - KEY FILE STATUS KEY IS FSTAT - CODE VSAM - CODE_______________________________________________________________________________ COBOL 64
  65. 65. User Manual for COBOL_______________________________________________________________________________7.8.3. COBOL STATEMENTS USABLE WITH SEQUENTIALFILESAccess Mode COBOL / OPEN OPEN OPEN OPEN 370 INPUT OUTPUT I-O EXTEND StatementSequential OPEN X X X X WRITE X X START READ X X REWRITE X DELETE CLOSE X X X X7.8.4. COBOL STATEMENTS WITH INDEXED FILES(KSDS) AND RELATIVE (RRDS) Access COBOL/370 OPEN OPEN OPEN OPEN Mode Statement INPUT OUTPUT I-O EXTEND Sequential OPEN X X X X WRITE X X START x x READ X X REWRITE X DELETE x CLOSE X X X X Random OPEN X X X WRITE X X START READ X X REWRITE X DELETE X CLOSE X X X_______________________________________________________________________________ COBOL 65

×