v1 Cobol Mat

Embed Size (px)

Citation preview

  • 7/28/2019 v1 Cobol Mat

    1/101

    COBOL

    Version - 126th June 2000

    1

  • 7/28/2019 v1 Cobol Mat

    2/101

    COBOL

    Table of Contents

    2

  • 7/28/2019 v1 Cobol Mat

    3/101

    COBOL

    Day-Wise Schedule

    Day 1

    IntroductionProgramming Structure, Coding FormatsConstants, Identifiers, Figurative ConstantsData Division

    Level numbersElementary and group data namesPicture clauses [Edit and computation]

    Value clause, Usage clause, Redefines clause, Renames clauseProcedure Division

    Input and Output verbsSimple Programs

    Day 2

    Arithmetic verbsMove

    Numeric, Non numeric, Group

    Control verbsIf, Condition names, Goto, PerformSample Programs

    Day 3

    File Organization ConceptsSequential Files

    CreationAccessQuery

    Sort and Merge sequential files

    Copy verb

    Day 4

    Trapping Runtime errorsSimple reportsReports with page totals and net totals

    Day 5

    3

  • 7/28/2019 v1 Cobol Mat

    4/101

    COBOL

    Single level control breakDouble level control breakIndex files

    Creation [Record key and alternate keys]Conversion of sequential to indexed filesRandom and sequential access

    Day 6

    Start verbEdit and deleted records in indexed filesFile maintenanceReport generation with index files

    Day 7

    Master, Transaction filesFile updateUpdate existing masterCreate new master

    Day 8

    Table HandlingOne and two dimensional arraysRedefines clauseFiles and tablesSearch a table

    Day 9

    SubprogramsCall verbLinkage section

    String Handling verbs

    4

  • 7/28/2019 v1 Cobol Mat

    5/101

    COBOL

    1.Introduction

    COBOL was developed in 1959 by a group called the CODASYLCommittee. CODASYL is an abbreviation for Conference on Data Systems Languages.This committee included representatives from academia, user groups, and computermanufacturers. The ultimate objective of this committee was to develop a standardbusiness-oriented language for which all major manufacturers would provide compilers.The Department of Defense convened this conference since it, as well as othergovernment agencies, was particularly dissatisfied with the lack of standards in thecomputing field.

    Features of COBOL

    i. Business-Oriented Language

    As a business-oriented language COBOL is designed specifically for commercialapplications, such as payroll and inventory, that typically operate on a large volume ofdata.

    ii. A Standard LanguageCOBOL compilers are available for most computers. The same COBOL program

    may be compiled and run on a variety of different machines.The universality of COBOL allows computer uses greater flexibility than they

    would have with many other languages. A company is free to acquire different brands ofcomputers while using a single programming language. Similarly, conversion from onemodel computer to a more advanced or newer one presents no great problem as long asthere is a COBOL compiler for each model.

    iii. An English-like Language

    COBOL is an English-like language. All instructions can be coded using Englishwords rather than complex codes. To add two numbers together, for example, we use theword ADD. Similarly, the rules for programming in COBOL conform to many of therules for writing in English, making it a relatively simple language to learn.

    5

  • 7/28/2019 v1 Cobol Mat

    6/101

    COBOL

    iv. Self Documenting

    One advantage of COBOL computer programs is that they can be substantially self-

    documenting. Self-documentation is a characteristic of a language that allows a reader ofa program to understand its function and follow its processing steps. The languageinstructions are very English-like, but the programmer has substantial choice as towhether to make a program self-documenting or obscure.

    Structure of COBOL programs

    COBOL programs are written according to a special structure which is organizedinto a hierarchy of parts.

    A character is the lowest form in the program structure

    A word is made up of one or more characters.

    A clause consists of characters and words and is used to specify an attribute of anentry.

    A statement is a syntactically valid combination of words and characters written

    in the PROCEDURE DIVISION of a COBOL program and beginning with a verb.

    A sentence is a sequence of one or more statements, the last of which isterminated by a period followed by a space.

    A paragraph consists of one or more sentences.

    A section consists of one or more paragraphs.

    A division consists of one or more paragraphs or sections. Every COBOLprogram consists of four divisions in the following order : IDENTIFICATION

    DIVISION, ENVIRONMENT DIVISION, DATA DIVISION and PROCEDUREDIVISION.

    COBOL Words

    A sequence of continuous characters from the character set can form a word.There are two types of COBOL words, reserved words and user-defined words.

    6

  • 7/28/2019 v1 Cobol Mat

    7/101

    COBOL

    COBOL words are formed by using the following character set :

    Digits 0-9

    Letters A-ZSpace (blank)Special characters ( ) . + - * / = $ , ; < >

    Reserved words are words that are defined both syntactically and semantically bythe COBOL language. The programmer cannot use any of these words except in the formspecified by the language.

    User-defined words (data names) are words supplied by the programmer in orderto satisfy the format of a clause or statement in the language.

    Rules for forming user-defined words

    1 to 30 characters. Letters, digits, and hyphens (-) only. No embedded blanks. At least one alphabetic character. May not begin or end with a hyphen. No COBOL reserved words such as DATA, DIVISION, etc.

    Valid datanames Invalid datanames

    HOURS DISCOUNT-SALES-TOTAL AUTHOR SUBJECT1 BASIC+HRAAMOUNT-OF-TRANSACTION-OUT 123

    Divisions

    IDENTIFICATION DIVISION

    The IDENTIFICATION DIVISION is the first division of a COBOL program. Itsupplies the information about the program to others who may read or use the program.

    The IDENTIFICATION DIVISION is divided into the following paragraphs :

    PROGRAM-ID. Program-name. Used to specify the program name. Use names of eight characters or less, letter

    and digits only, because such names are accepted on all systems.

    7

  • 7/28/2019 v1 Cobol Mat

    8/101

    COBOL

    AUTHOR. author-name.

    Used to specify the programmers name.

    DATE-WRITTEN. Date. Specify the date the program was coded.

    DATE-COMPILED. Date. Can be coded with an actual date. But if it is coded without a date entry, the

    compiler itself will automatically fill in the actual date of compilation.

    ENVIRONMENT DIVISION

    The ENVIRONMENT DIVISION is the only machine-dependent division of a

    COBOL program. It is composed of two sections :

    CONFIGURATION SECTION.

    SOURCE-COMPUTER. Computer. Computer used for compiling the program.

    OBJECT-COMPUTER. Computer. Computer used for executing the program.

    INPUT-OUTPUT SECTION.

    Supplies information concerning the input and output devices used. This section is

    required only if the program uses files or prints reports.FILE-CONTROL.

    DATA DIVISION

    The DATA DIVISION defines and describes fields, records, and files in storage.Commonly, it consists of the following sections :

    FILE SECTION.

    Defines all input and output files.

    WORKING-STORAGE SECTION.

    Reserves storage for fields not part of input or output but nonetheless required forprocessing. These include constants, end-of-file indicators, and work areas.

    LINKAGE SECTION.

    Used to identify items that will be passed to the called program from the callingprogram and vice-versa.

    8

  • 7/28/2019 v1 Cobol Mat

    9/101

    COBOL

    PROCEDURE DIVISION

    The PROCEDURE DIVISION is divided into paragraphs. Each paragraph is anindependent module or routine that includes a series of instructions designed to performspecific set of operations. Paragraph names are coined by the programmer following therules for forming data-names.

    A PROCEDURE DIVISION may also consist of several sections. A section maycontain several paragraphs.

    Coding a COBOL program

    A COBOL program file will have an extension .CBL. A COBOL program needsto be coded following the below mentioned coding rules.

    Columns Use Explanation

    1-6 Sequence numbers or Pageand Line numbers (optional)

    Used for sequence-checking

    7 Indicator column Denotes* comments/ page break- continue strings

    8-11 Area A DIVISION, SECTION, paragraphnames and level 01, 77 entries

    12-72 Area B Statements and sentences

    73-80 Comment Ignored by the compiler

    Note : Division, Section, Paragraph Names and Sentences must end with a periodfollowed by at least one space.

    A Sample COBOL Program Eg. 1.1

    IDENTIFICATION DIVISION.PROGRAM-ID. SAMPLE1.

    AUTHOR. XYZ.DATE-WRITTEN. 1-JUN-2000.DATE-COMPILED.

    ENVIRONMENT DIVISION.CONFIGURATION SECTION.SOURCE-COMPUTER. IBM-PC.OBJECT-COMPUTER. IBM-PC.

    DATA DIVISION.

    PROCEDURE DIVISION.

    9

  • 7/28/2019 v1 Cobol Mat

    10/101

    COBOL

    0000-MAIN.STOP RUN.

    10

  • 7/28/2019 v1 Cobol Mat

    11/101

    COBOL

    2.Working with data

    Hierarchical representation of data

    Level numbers

    The record description specifies the format of a record . Record descriptionentries indicate :

    1. The items or fields to appear in the record2. The order in which the fields appear3. How these fields are related to one another

    Data is grouped in COBOL using the concept of a level. Records are consideredthe highest level of data in a file, and are coded on the 01 level. There can be only onedata-name at the 01 level for each record, as it is the all-inclusive data-name. A field ofdata within the record is coded on a level subordinate to 01, i.e 02, 03 and so on. Anylevel number between 02 and 49 may be used to describe fields within a record.

    Elementary and Group ItemsAn elementary item has no subordinate parts. A group item may consist of one or

    more group items. In the code below :FIRST-NAME, ZIP-CODE are elementary items

    CUSTOMER-ADDRESS, STREET are group items

    Eg 2.1:

    01 CUSTOMER-ADDRESS02 NAME

    03 FIRST-NAME03 LAST-NAME

    02 STREET03 S-NUMBER03 STREET-NAME

    02 ZIP-CODE

    PICTURE ClausesGroup items are defined by a level number and a name, which is followed by a

    period. Elementary items must be described with a PICTURE (or PIC, for short) clause.

    Functions of the PICTURE Clause

    1. Specifies the type of data contained within an elementary item.2. Indicates the size of the field.

    11

  • 7/28/2019 v1 Cobol Mat

    12/101

    COBOL

    Types of data fields

    Type Picture clause Explanation

    Alphabetic A A field that may contain only letters or blanks. E.g. aname or item description field.

    Alphanumeric X A field that may contain any character i.e. letters,digits, blanks and/or special characters. E.g. anaddress field.

    Numeric 9 Any signed or unsigned field that will contain only

    digits is considered numeric.

    Size of data fields

    The size of the field is denoted b the number of As, Xs, or 9s used in thePICTURE.

    Eg 2.2:

    05 AMT PIC 99999.

    AMT is an elementary field consisting of five positions of numeric data.Alternatively, the same can be coded as follows :

    Eg 2.3:

    05 AMT PIC 9(5).

    At least one space must follow the word PIC. All As, Xs, or 9s should appearconsecutively with no spaces between the characters. Similarly, if parentheses are used todenote the size of a field, no spaces should appear within the parentheses.

    Eg 2.4:

    01 CUSTOMER-ADDRESS.02 NAME.

    03 FIRST-NAME PIC X(10).03 LAST-NAMEPIC X(15).

    02 STREET.03 S-NUMBER PIC 9(3).03 STREET-NAME PIC X(20).

    02 ZIP-CODE PIC 9(6).

    12

  • 7/28/2019 v1 Cobol Mat

    13/101

    COBOL

    WORKING-STORAGE SECTION

    Defines and describes all data storage fields required for Processing (Includingconstants).These fields are not part of any input/output file.

    Can be used for storing Intermediate results Counters, Flags Input/Output Records Tables etc.

    Types of data

    Variable

    Data entered by the user at run-time.

    Constant

    Data required for processing that is not dependent on the input to the system.

    There are 3 types of literals.

    1. Numeric LiteralsA numeric literal is a constant used primarily for arithmetic operations.

    Rules for forming numeric literals

    a) 1 to 18 digitsb) A + or sign may be used, but it must appear to the left of the number.c) A decimal point is permitted within the literal. The decimal point,

    however may not be the rightmost character of the literal.

    Eg 2.5:+34-8.6.008

    2. Nonnumeric Literals

    13

  • 7/28/2019 v1 Cobol Mat

    14/101

    COBOL

    A nonnumeric literal is a constant that is used in the PROCEDUREDIVISION for all operations except arithmetic.

    Rules for forming nonnumeric literalsa) The literal must be enclosed in quotation marks.b) A maximum of 120 characters are permitted.c) Any character permitted in the COBOL character set may be used

    except the quotation mark.

    Eg 2.6:

    CODE$ 123

    3. Figurative ConstantsA figurative constant is a COBOL reserved word that has special significance

    to the compiler.

    The figurative constants are

    ZERO, ZEROS, ZEROES - references the value of zerosSPACE, SPACES - reference the value of blank QUOTE, QUOTES -references the quotation mark

    -used in nonnumeric literals to include aquote.

    e.g DQuoteSouza will store the valueDSouza.

    LOW-VALUE , LOW-VALUES - references the lowest value in the collatingsequence for the particular computer system.

    HIGH-VALUE, HIGH_VALUES - references the highest value in the collatingsequence for the particular computer system.

    ALL - references one or more occurrences of thesingle character nonnumeric literal.e.g. MOVE ALL A TO HEAD-1, willresult in the field HEAD-1 being filled withAs.

    Special Characters (Numeric Field) :

    Implied decimal point

    The symbol V denotes an implied decimal point, which does not occupy a storageposition. In the code below, AMOUNT occupies 5 positions.

    14

  • 7/28/2019 v1 Cobol Mat

    15/101

    COBOL

    Eg 2.7:

    05 AMOUNT PIC 999V99.

    Eg 2.8:

    PIC Description Value Stored as

    99V9 38.50 3859(4)V99 452.39 045239999 550 550

    Signed numbers

    If a numeric field can have negative contents, then it must have an S in its PIC

    clause. It must be the leftmost character. It does not take any storage space.

    Eg 2.9:

    02 BALANCE PIC S999.

    Edited fields

    The purpose of editing is to make data more suitable for human reading. Thusediting in its most common use is associated with printing data on the printer. Forexample, we may suppress leading zeros, we may use commas to make long numeric

    values more legible, we may insert a dollar sign in front of a value, etc.

    The editing characters are Z * $ - + CR DB . , B 0 /

    Z - Suppressing leading zeros

    The Z PICTURE character is used to replace leading zeros by blanks and thusperforms a function identical to that of the floating . Zero suppression terminates whenthe first nonzero digit or the . character is encountered, whichever occurs first. When Zshave been designated for all positions in a field and the value to be inserted in that field is

    zero, in that case the entire field is blanked. Zs can appear before as well as after thedecimal point. The number of Zs representing integers in the report-item should be equalto the number of integers or 9s in the sending field.

    Eg 2.10:

    PIC Description Value Printed as

    Z99 25 b25ZZZV99 25 b2500

    15

  • 7/28/2019 v1 Cobol Mat

    16/101

    COBOL

    ZZZV99 0.10 bbb10ZZZVZZ 0.052 bbb05ZZZVZZ 0.00 bbbbb

    * - Check ProtectionThe * character is referred to as a check-protect character and is normally used to

    protect dollar amounts written on checks or other negotiable documents. Asterisks arezero-suppression characters that replace each non-significant zero and comma with *instead of a space.

    Eg 2.11:

    PIC Description Value Printed as

    **999 04678 *4678***99 00052 ***52

    $ - Dollar SignBy use of the $ PICTURE character the dollar sign is written in the position in

    which it is to appear in the output. Since the $ sign is counted in the size of the field, thefield should be assigned at least one more position than the maximum number ofsignificant digits expected. The $ may also be floated, by which we mean that it will beentered to the left of the first significant digit in the field and be preceded by blanks. The$ float stops when wither the first nonzero digit or the . or V is encountered. When the $sign appears in all positions and the value is zero, the effect is to blank the entire field(including any . and ,).

    Eg 2.12:

    PIC Description Value Printed as

    $999V99 125.13 $12513$9(5)V99 100.00 $0010000$$99V99 12.49 b$1249$$$$9V99 150.10 $15010$$$$V99 0.15 bbb$15$$$$V$$ 0.0 bbbbb

    . - (Decimal Point)The . (decimal) PICTURE character indicates the position of the decimal point andserves to align the actual decimal values in the field, only one such character may appearin a field. Further, a field cannot contain both a V and a . PICTURE character. It cannotbe the rightmost character. If all the digits before and after the decimal point are zero, theresulting field will be blank.

    16

  • 7/28/2019 v1 Cobol Mat

    17/101

    COBOL

    Eg 2.13:

    PIC Description Value Printed as

    $9,999.99 2,350.22 $2,350.22

    $9,999.99 150.31 $0,150.31$$,999.99 150.31 bb$150.31$$,$$$.99 24.40 bbb$25.40$$,$$$.999 0.019 bbbbb$.019$$$$$.$$$ 0.0 bbbbbbbbb

    , - (Comma)The comma is placed in the positions in which it is desired. A field may include morethan one , (comma) PICTURE character if the size of the field warrants it. A comma will

    be appropriately suppressed if no significant digit precedes it. It cannot appear as theleftmost or rightmost character.

    Eg 2.14:

    PIC Description Value Printed as

    $9,999.99 2,350.22 $2,350.22$9,999.99 150.31 $0,150.31$$,999.99 150.31 bb$150.31$$,$$$.99 24.40 bbb$25.40$$,$$$.999 0.019 bbbbb$.019

    $$,$$$.$$$ 0.009 bbbbb$.009$$,$$$.$$$ 0.0 bbbbbbbbbb$$,$$9.999 2,210.2 $2,210.200$$,999.9 2,210.2 $2,210.2$$,999.9 2,210.256 $2,210.2$9,999.9999 23 $0,023.0000

    - (Minus) and + (Plus)The PICTURE character prints a minus sign only if the quantity is negative and

    omits a sign for all other quantities. It can appear as the leftmost or rightmost character.The PICTURE insertion character differs from the S character in that the use of the Scharacter identifies a field as a signed one for computational purposes, but the sign doesnot occupy a position. Use of the PICTURE character leads to a field in which the signoccupies a character position.

    The + PICTURE character is used to print wither a plus sign or a minus sign forall values. A + sign will be generated for positive or unsigned quantities, and a sign will

    17

  • 7/28/2019 v1 Cobol Mat

    18/101

    COBOL

    be generated for negative quantities. The sending field should have an $ in its PICTUREclause for it to be interpreted as a signed number.

    The + or PICTURE character can appear as the leftmost or rightmost character.They can also be floated, similar to the $ character. However , the +, -, and $ are mutuallyexclusive as floating characters. If we want to have both $ float and + or signrepresentation, we write the + or to the right of the field.

    Eg 2.15:

    PIC Description Value Printed as

    +999.9 35.2 +035.2999.9+ 35.2 035.2+999.9+ -35.2 035.2-++9.9 -001.3 b-1.3

    +++9.99 .05 bb+0.05+++9.99 -.05 bb-0.05++++.++ .01 bbb+.01

    ----.-- 0.0 bbbbbbb--99.99 -10.25 b-10.25--999.99 100.25 b100.25999.9- -10.2 010.2-

    DB/CRIn accounting applications there is often need to identify values that represent

    debits or credits. The COBOL language facilitates such differentiation by means of theDB (debit) and CR (CR) editing characters. The DB or CR symbol is written only to theright of a field in the PICTURE clause, and in both cases it is represented in storage forthe purpose of subsequent output only when the value is negative.

    Summary for use of + - DB CR

    PIC Storage when Storage whenCharacter data positive data negative

    + + -- Blank -DB Blank DBCR Blank CR

    18

  • 7/28/2019 v1 Cobol Mat

    19/101

    COBOL

    Eg 2.16:

    PIC Description Value Printed as

    $999.99DB 135.26 $135.26bb

    $999.99DB -135.26 $135.26DB$,$$99.99CR -10.50 bb$10.50CR

    B (blank)The B insertion editing character results in blanks being entered in the designated

    positions.

    Eg 2.17:

    05 NAME PIC ABABA(10) VALUE RBSMITH.

    NAME = RbBbSMITHbbbbb

    0 - (Zero)The zero insertion character causes zeros to be inserted in the positions in which it

    appears.

    Eg 2.18:

    05 AMOUNT PIC 9(4)000 VALUE 1365.

    AMOUNT = 1365000

    / - (stroke)

    Each / (stroke) in the PICTURE character string represents a character position intowhich the stroke character will be inserted.

    Eg 2.19:

    05 PRINT-DATE PIC 99/99/99 VALUE 040798.

    PRINT-DATE = 04/07/98

    VALUE Clause

    In addition to defining storage fields using the PICTURE clause it is oftendesirable to assign initial values to elementary items in the WORKING-STORAGESECTION. Such a value may remain unchanged throughout the program, or it maychange in the course of program execution. Such initial values are generally not assignedto FILE SECTION items, since such fields either receive their data from the externalmedium or from other storage location as the result of program execution.

    Eg 2.20:

    19

  • 7/28/2019 v1 Cobol Mat

    20/101

    COBOL

    02 PAGE-TITLE PIC A(14) VALUE SAMPLE PROGRAM.02 TAX-RATE PIC V99 VALUE IS 0.03.

    Continuation of Literals from one line to the nextA nonnumeric literal may contain up to 120 characters. The same applies to a

    nonnumeric literal in a VALUE clause. Sometimes a nonnumeric literal may not fit on asingle line, hence it is necessary to continue this literal.

    Rules for continuation of nonnumeric literals

    1. Begin the literal in the VALUE clause with a quotation mark.2. Continue the literal until position 72, the end of the line, is reached. Do not end

    with a quotation mark on this line.3. Place a hyphen on the next line in the position beginning in Area B of the second

    line. Begin with a quotation mark.

    4. End the literal with a quotation mark.

    Eg 2.21:

    01 PAGE-HEADING PIC X(36) VALUE MONTHLY TRANSACTIONS FOR AP- RIL 2000.

    USAGE clause

    Numeric data in a computer may be represented in one of two basic modes. Theymay be represented as character data or as numeric data. The arithmetic registers ofcomputers perform arithmetic with numeric data that is in numeric, not character mode. Ifnumeric data is represented in character mode, it must firstbe converted to numeric mode before arithmetic computations can be performed.

    In COBOL, data in character mode is described in DISPLAY mode, while data innumeric mode is described as being COMPUTATIONAL MODE. All data items areassumed to be in DISPLAY mode unless they are declared to be COMPUTATIONAL.The declaration is done in the DATA DIVISION with the USAGE clause.

    Eg 2.22:

    02 AMOUNT-1 PIC 99.02 AMOUNT-2 PIC 99 USAGE DISPLAY.02 AMOUNT-3 PIC 99 USAGE COMPUTATIONAL.

    02 AMOUNT-4 PIC 99 USAGE COMP.

    REDEFINES Clause

    The REDEFINES clause can be used to allow the same storage location to bereferenced by different data-names or to allow a regrouping or different description of thedata in a particular storage location.

    20

  • 7/28/2019 v1 Cobol Mat

    21/101

    COBOL

    Restrictions

    The REDEFINES clause cannot be used- at the 01 level in the FILE SECTION.- when the levels of data-name-1 and data-name-2 are different.- When the level number is 66 or 88.

    Eg 2.23:

    1 SAMPLE.02 RECEIVABLE.

    03 CUSTOMER-NUMBER PIC 9(8).03 CUSTOMER-NAME PIC X(11).

    03 AMOUNT PIC 9(4)V99.02 PAYABLE REDEFINES RECEIVABLE.

    03 VENDOR-NUMBER PIC 9(6).03 VENDOR-NAME PIC X(12).03 VENDOR-OWED-AMT PIC 9(5)V99.

    RENAMES Clause

    The RENAMES clause provides the programmer with the capability ofregrouping elementary data items. It resembles the REDEFINES clause, except that it canform a new grouping of data items which combines several items. Use of the RENAMESclause is always signaled by the special 66 level number.

    Eg 2.24:01 TAX-RECORD.

    02 SOC-SEC-NUMBER PIC X(9).02 NAME.

    03 FIRST-NAME PIC X(10).03 LAST-NAME PIC X(15).

    02 TOTAL-YTD.03 GROSS-PAY PIC 9(8)V99.

    03 NET-PAY PIC 9(8)V99.

    03 TAX PIC 9(5)V99.66 LAST-GROSS RENAMES LAST-NAME THRU NET-PAY.

    Qualification of names

    A data name that is not unique must be qualified in COBOL verbs.

    Eg 2.25:

    21

  • 7/28/2019 v1 Cobol Mat

    22/101

    COBOL

    01 IN-REC05 NAME PIC X(10).05 AGE PIC 99.

    01 OUT-REC.05 NAME PIC X(10).05 B-DATE PIC 9(6).

    MOVE NAME OF IN-REC TO NAME OF OUT-REC.

    22

  • 7/28/2019 v1 Cobol Mat

    23/101

    COBOL

    3. ACCEPT/DISPLAY verbs

    The PROCEDURE DIVISION contains all the instructions required forprocessing. It is divided into various paragraphs or modules. Each module consists ofsentences.

    The ACCEPT and DISPLAY verbs are used for input and output in conjunctionwith storage fields that are not part of any files.

    ACCEPTThe instruction ACCEPT identifier enable the user to enter input data directly

    from a keyboard.

    Eg 3.1:

    ACCEPT MY-NAME.

    DISPLAYDISPLAY can reference a series of identifiers or literals.

    Eg 3.2:

    DISPLAY MY-NAME.DISPLAY HELLO.

    STOP RUNThe verb STOP RUN terminates program execution. A program may contain more thanone STOP RUN statement, depending on the logic of the program. STOP RUN signifiesthe logical end of the program.

    A program using DISPLAY and ACCEPT.

    Eg 3.3:

    IDENTIFICATION DIVISION.PROGRAM-ID. SAMPLE2.

    *This program accepts the users name and displays a message.

    AUTHOR. XYZ.DATE-WRITTEN. 1-JUN-2000.DATE-COMPILED.

    ENVIRONMENT DIVISION.CONFIGURATION SECTION.SOURCE-COMPUTER. IBM-PC.OBJECT-COMPUTER. IBM-PC.

    23

  • 7/28/2019 v1 Cobol Mat

    24/101

    COBOL

    DATA DIVISION.WORKING-STORAGE SECTION.01 USER-NAME PIC X(15).PROCEDURE DIVISION.

    0000-MAIN.DISPLAY Enter your name : .ACCEPT USER-NAME.

    DISPLAY HELLO USER-NAME.STOP RUN.

    24

  • 7/28/2019 v1 Cobol Mat

    25/101

    COBOL

    4. MOVE statements

    A value can be moved from one storage location to another by the movestatement. Contents of receiving field are changed after the operation.

    Verb

    SENDING FIELD

    RECEIVING FIELD

    MOVE AMT-IN TO AMT-OUT.

    The contents of AMT-In will be copied to thesecond field, AMT-OUT, as a result of the MOVE operation.

    Eg 4.1:

    MOVE TOTAL TO PRINT-TOTALMOVE INVALID TO MESSAGE.MOVE ZEROS TO TOTAL

    Types of MOVE :

    The MOVE statement can be categorized based on the receiving field : numericMOVEs and non-numeric MOVEs.

    Numeric MOVEA numeric move is one in which a numeric field or literal is moved to a numeric

    receiving field.

    25

  • 7/28/2019 v1 Cobol Mat

    26/101

    COBOL

    When Sending and Receiving fields have the same PIC clauses

    If the PIC clauses of both fields are identical, the contents of identifier-2 will bereplaced with the contents of identifier-1 and the sending field will be unchanged.

    When Sending and Receiving fields have different PIC clauses

    Rule 1 : Moving integer portions of numeric fields

    When moving an integer sending field or an integer portion of a numeric sendingfield to a numeric receiving field, move, movement is from right to left. All nonfilledhigh-order (leftmost) integer positions of the receiving field are replaced with zeros.

    Eg 4.2:

    05 AMT-IN PIC 999 VALUE 123.

    05 AMT-OUT PIC 9(4) VALUE 4567.MOVE AMT-IN TO AMT-OUT.

    Result :

    AMT-OUT = 0123

    Avoiding truncation

    In a numeric move, if the receiving field has fewer integer positions than thesending the field, the most significant digits will be truncated.

    Eg 4.3:

    05 AMT-IN PIC 999 VALUE 123.05 AMT-OUT PIC 9(2) VALUE 45.

    MOVE AMT-IN TO AMT-OUT.

    Result :

    AMT-OUT = 23

    Rule 2: Moving decimal portions of numeric fields

    When moving a decimal portion of a numeric sending field to the decimal portionof a numeric receiving field, movement is from left to right, beginning at the implieddecimal point. Low-order (rightmost) non-filled decimal portions of the receiving fieldare replaced with zeros.

    Eg 4.4:

    a. When receiving field has more decimal positions than the sending field05 AMT-IN PIC 99V99 VALUE 12.34.05 AMT-OUT PIC 99V999 VALUE 56.789.

    MOVE AMT-IN TO AMT-OUT.

    26

  • 7/28/2019 v1 Cobol Mat

    27/101

    COBOL

    Result :AMT-OUT = 12.340

    b. When receiving field has fewer decimal positions than the sending field05 AMT-IN PIC V99 VALUE.34.05 AMT-OUT PIC V9 VALUE .5.

    MOVE AMT-IN TO AMT-OUT.

    Result :AMT-OUT = .3

    Note : The same rules apply to numeric literals moved to numeric fields

    Non-numeric MOVEA non-numeric MOVE operation occurs in the following cases :1. Moving an alphanumeric or alphabetic field, defined by a PICTURE of Xs or

    As, to another alphanumeric or alphabetic field.2. Moving a non-numeric literal to an alphanumeric or alphabetic field.3. Moving a numeric field or numeric literal to an alphanumeric field or to any

    group item.

    Rule :

    In a non-numeric move, data is transmitted from the sending field to the receivingfield from left to right. Low-order or rightmost positions of the receiving field that are not

    replaced with sending field characters are filled with spaces.

    Eg 4.4:

    a. When receiving field is larger than the sending field

    05 NAME-IN PIC XXX VALUE ABC.05 NAME-OUT PIC X(5) VALUE DEFGH.

    MOVE NAME-IN TO NAME-OUT.

    Result :NAME-OUT = ABCbb

    b. When receiving field is smaller than the sending field

    05 NAME-IN PIC XXX VALUE ABC.05 NAME-OUT PIC XX VALUE PQ.

    27

  • 7/28/2019 v1 Cobol Mat

    28/101

    COBOL

    MOVE NAME-IN TO NAME-OUT.

    Result :

    NAME-OUT = AB

    c. When the sending field is numeric integer and the receiving field is non-

    numeric

    05 NAME-IN PIC 999 VALUE 32105 NAME-OUT PIC X(5) VALUE DEFGH.

    MOVE NAME-IN TO NAME-OUT.

    Result :

    NAME-OUT = 321bb

    d. When the sending field is a non-numeric literal

    05 NAME-OUT PIC X(5) VALUE DEFGH.

    MOVE XYZ TO NAME-OUT.

    Result :

    NAME-OUT = XYZbb

    e. When the sending field is a figurative constant

    05 NAME-OUT PIC X(5) VALUE DEFGH.

    MOVE SPACES TO NAME-OUT.

    Result :

    NAME-OUT = bbbbb

    A group move is considered a non-numeric move

    All group items, even those with numeric subfields, are treated as alphanumeric

    fields.

    Eg 4.5:

    05 DATE-OUT.10 MONTH-OUT PIC 99 .10 YEAR-OUT PIC 99.

    a. MOVE 1 TO MONTH-OUT.MOVE 99 TO YEAR-OUT.

    28

  • 7/28/2019 v1 Cobol Mat

    29/101

    COBOL

    Result :

    DATE-OUT = 0194

    b. MOVE 194 TO DATE-OUT.

    Result :DATE-OUT = 194b

    Permissible MOVE operations

    Sending Field Receiving Field

    Numeric Alphabetic Alphanumeric Group item

    Numeric x *

    Alphabetic XAlphanumeric x

    ZEROS x

    SPACES x

    Group item x* Numeric integer fields can be moved to alphanumeric fields but numeric fields with a V in the PIC clause cannot bemoved to alphanumeric fields.

    The MOVE CORRESPONDING statement

    In the MOVE CORRESPONDING statement, all elementary items within the

    sending group-item that have the same names as corresponding elementary items in thereceiving group-item will be moved. The same-named fields in the receiving group-itemneed not be in any specific order. Any fields of the sending record, that are not matchedby the same-named fields in the receiving record are ignored.

    29

  • 7/28/2019 v1 Cobol Mat

    30/101

    COBOL

    5. Arithmetic verbs

    All the basic arithmetic operations of ADD, SUBTRACT, MULTIPLY, andDIVIDE require that the fields operated on (1) have numeric PIC clauses and (2) actuallyhave numeric data when the program is executed.

    In all cases, the resultant field must be an identifier or data-name, not a literal.

    ADD statementThe result, or sum, of an ADD operation is always placed in the last field

    mentioned. The only field that is altered as a result of the ADD operation is this last field,which is the one directly following the word TO.

    When using the TO format in an ADD statement, all the data-names and literals

    are added together, and the result placed in the last field specified.When using the GIVING format, all fields and literals preceding the word

    GIVING are added together and the sum is placed in the field following the wordGIVING.

    Eg 5.1:

    05 EMP-BASIC PIC 9(5) VALUE 4000.05 EMP-HRA PIC 9(3) VALUE 650.05 EMP-TOTAL PIC 9(6) VALUE 100.

    a. ADD EMP-BASIC TO EMP-TOTAL.

    Result :EMP-TOTAL = 4100

    b. ADD EMP-BASIC EMP-HRA GIVING EMP-TOTAL.

    Result :

    EMP-TOTAL = 4650

    SUBTRACT statementAll fields and literals preceding the word FROM will be added together and the

    sum subtracted from the field following the word FROM. The result, or difference, willbe placed in this same field if no GIVING option is used. All other fields will remainunchanged.

    Eg 5.2:

    05 EMP-GROSS PIC 9(5) VALUE 4250.05 EMP-PF PIC 9(3) VALUE 250.05 EMP-ITAX PIC 9(3) VALUE 100.

    30

  • 7/28/2019 v1 Cobol Mat

    31/101

  • 7/28/2019 v1 Cobol Mat

    32/101

    COBOL

    Result :EMP-GROSS = 4000

    EMP-NEW-SAL = 1000

    e. DIVIDE EMP-GROSS BY 4 GIVING EMP-NEW-SAL.Result :

    EMP-NEW-SAL = 1000

    Use of the REMAINDER clause in the DIVIDE operationWhen performing a division operation, the result will be placed in the receiving field

    according to the PIC specifications of that field.

    Eg 5.4:

    DIVIDE 130 BY 40 GIVING WS-TOTAL.

    After the operation is performed, 03 is placed in WS-TOTAL.It is sometimes useful to store the remainder of a division operation for additional

    processing. The DIVIDE can be used for this purpose by including a REMAINDERclause.

    ROUNDED OptionA frequent need exists for rounding numeric values.

    Eg 5.5:05 AMT1 PIC 99V999 VALUE 12.857.05 AMT2 PIC 99V999 VALUE 25.142.05 AMT3 PIC 99V99 VALUE 37.99.

    ADD AMT1 AMT2 GIVING AMT3.

    In the code given above, the result 37.999 is placed in an accumulator. When thisvalue is move to the field AMT3 , the low-order decimal position is truncated and 37.99is stored in the field. A more desirable result would be 38.00 since 38 is closer to the sumof 37.999. We consider results more accurate if they are rounded to the nearest decimalposition.

    To obtain rounded results, the ROUNDED option may be specified with any arithmeticstatement. In all case, it directly follows the resultant data-name.

    Eg 5.6:

    ADD AMT1 AMT2 GIVING AMT3 ROUNDED.

    32

  • 7/28/2019 v1 Cobol Mat

    33/101

    COBOL

    If the ROUNDED option is not specified, truncation of decimal positions willoccur if the resultant field cannot accommodate all the decimal positions in the result.With the ROUNDED option, the computer will always round the result to the PICTURE

    specification of the receiving field.

    If ROUNDED and REMAINDER are to be used in the same DIVIDE statement,ROUNDED must appear first.

    ON SIZE ERRORConsider the following :

    Eg 5.7:

    05 AMT1 PIC 999 VALUE 800.05 AMT2 PIC 999 VALUE 150.

    05 AMT3 PIC 999 VALUE 050.

    ADD AMT1 AMT2 TO AMT3.

    The effect of the above statement would be the same as coding MOVE 1000 TOAMT3. In this case, the resultant field is not large enough to store the accumulated sum.In other words , an overflow or size error condition has occurred. This will produceerroneous results.

    Eg 5.8:

    ADD AMT1 AMT2 TO AMT3ON SIZE ERROR MOVE ZERO TO TOTAL-OUT.

    In a divide, the size error condition has additional significance. If an attempt ismade to divide by zero, a size error condition will occur. This is because division by zeroyields a result of infinity which makes it impossible to define a sufficiently largereceiving field.

    If the ON SIZE ERROR option is employed along with the ROUNDED option,the word ROUNDED always precedes ON SIZE ERROR.

    COMPUTE statementIf complex or extensive arithmetic operations are required in a program, the use of

    the four arithmetic verbs may prove cumbersome. The COMPUTE verb provides anothermethod of performing arithmetic. The COMPUTE statement uses the followingarithmetic symbols :

    + Add- Subtract* Multiply/ Divide** exponentiation

    33

  • 7/28/2019 v1 Cobol Mat

    34/101

    COBOL

    Eg 5.9:

    COMPUTE TOTAL = AMT1 + AMT2 AMT3.

    To round the results in a COMPUTE statement to the specifications of thereceiving field, use the ROUNDED option directly following the receiving field. If weneed to test for a size error condition we may use the ON SIZE ERROR clause as the lastone in the statement.

    The sequence in which operations are performed in a COMPUTE statement*** or / (whichever appears first from left to right)+ or - (whichever appears first from left to right)The use of parentheses overrides rules 1-3. That is, operations with parentheses

    are performed first.

    34

  • 7/28/2019 v1 Cobol Mat

    35/101

    COBOL

    6. Decision Making

    A conditional statement is one that performs operations depending on theexistence of some condition. In COBOL, such statements generally begin with the wordIF and are called IF-THEN-ELSE or selection structures.

    An imperative statement , as opposed to a conditional statement, is one thatperforms an operation regardless of any existing condition.

    A condition may test for a specific relation. A simple condition may be a singlerelational test of the following form :

    Eg 6.1:

    IF AMT1 IS EQUAL TO AMT2

    DIVIDE QTY INTO TOTALELSEADD UNIT-PRICE TO FINAL-TOTAL.

    The following symbols for simple relational conditions are valid within a COBOLstatement :

    < IS LESS THAN> IS GREATER THAN= IS EQUAL TO

    The NEXT SENTENCE or CONTINUE clause

    There are times when you might want to execute a series of steps only if a certaincondition does not exist. The COBOL expression NEXT SENTENCE will enable you (1)to avoid performing any operation if a condition exists and (2) to execute instructionsonly if the ELSE condition is met.

    Eg 6.2:

    IF AMT1 = AMT2

    NEXT SENTENCEELSE

    ADD 1 TO TOTAL.

    Compound conditionalThe compound conditional offers even greater flexibility for selection and enables the IF

    statement to be used for more complex problems. The compound conditionals are asfollows :

    ORPerforms an operation or a series of operations if any one of several conditions exists.

    35

  • 7/28/2019 v1 Cobol Mat

    36/101

    COBOL

    ANDIf a statement or statements are to be executed only when all of several conditions

    are met.

    Negating conditionalsNOTAll simple relation, class or sign tests may be coded using a negated conditional (NOT).

    Eg 6.3:

    IF AMT1 IS NOT EQUAL TO AMT2PERFORM 200-NOT-EQUAL-RTN.

    Hierarchy rules for compound conditionals1. NOT is evaluated first.2. Conditions surrounding the word AND are evaluated first.3. Conditions surrounding the word OR are evaluated last.4. When there are several AND or OR connectors, the AND conditions are evaluated

    first, as they appear in the statement, from left to right. Then the OR conditionsare evaluated, also from left to right.

    5. To override Rules 1-3, use parentheses around conditions you want to beevaluated first.

    Sign test

    We can test whether a field is POSITIVE, NEGATIVE, or ZERO with a sign test.If a numeric field contains an amount less than zero, it is considered negative. If it has anamount greater than zero, then it is considered positive.

    Eg 6.4:

    IF AMT IS POSITIVEPERFORM 200-CALC-RTN.

    Class testWe can test for the type of data (ALPHABETIC or NUMERIC) using the class test.

    Eg 6.5:

    IF AMT-IN IS NUMERICPERFORM 300-CALC-RTN.

    36

  • 7/28/2019 v1 Cobol Mat

    37/101

    COBOL

    Condition namesA condition-name is a user-defined word established in the DATA DIVISION that gives aname to a specific value that an identifier can assume. An 88-level coded in the DATA

    DIVISION is a condition-name that denotes a possible value for an identifier. Acondition-name is always coded on the 88 level and has only a VALUE clause associatedwith it. Since a condition-name is not the name of a field, it will not contain a PICTUREclause.

    Eg 6.6:

    05 MARITAL-STATUSPIC X.

    88 SINGLEVALUE S.

    Either of the following tests may be used in the PROCEDURE DIVISION .

    Eg 6.6a:

    IF MARITAL-STATUS = SPERFORM 1000-SINGLE-ROUTINE.

    or

    Eg 6.6b:

    IF SINGLEPERFORM 1000-SINGLE-ROUTINE.

    37

  • 7/28/2019 v1 Cobol Mat

    38/101

    COBOL

    7. Iterations

    PERFORM statementThe simple PERFORM statement, is used to execute a specified routine from one

    or more points in a program.

    The PERFORM statement will :1. Execute all instructions in the named paragraph.2. Transfer control to the next instruction in sequence, after the PERFORM.

    Eg 7.1:

    PROCEDURE DIVISION.

    100-MAIN-MODULE.:

    PERFORM 400-HEADING-RTN.:

    200-CALC-RTN.::

    400-HEADING-RTN.::

    PERFORM..THRU

    The PERFORM executes all statement beginning at pargraph-name-1 until theend of paragraph-name-2 is reached. Control is then transferred to the statement directlyfollowing the PERFORM.

    Eg 7.2:

    100-MAIN.PERFORM 300-PARA THRU 500-PARA.

    :200-PARA.

    :300-PARA.

    :

    400-PARA.:

    500-PARA.EXIT.

    600-PARA.:

    The EXIT statement

    38

  • 7/28/2019 v1 Cobol Mat

    39/101

    COBOL

    EXIT is a COBOL reserved word that performs no operation. It is used to allowexecution to pass over other statements or to transfer control back to the statementfollowing the original PERFORM. It is used, when necessary, as an end point in a

    paragraph.

    PERFORM..UNTILIteration may be performed in COBOL using a PERFORM..UNTIL statement. Thecontents of the identifiers used in the UNTIL clause should be changed within theparagraph(s) being performed. The condition in a PERFORM .. UNTIL is tested beforethe named paragraph is executed even once. If the condition indicated in the UNTILclause is met at the time of execution, then the name paragraph(s) will not be executed.

    Procedure used in LoopingParagraph containing the PERFORM that Loops

    1. Initialize the field to be tested (e.g., MOVE 0 TO COUNTER1).2. Code a PERFORM..UNTIL using a separate paragraph. For example, PERFORM

    UNTIL COUNTER1 = 5.

    Loop to be performed

    1. Code the steps required in the loop as a separated paragraph.2. Increase or decrease the value in the field to be tested (e.g., ADD 1 TO

    COUNTER1).

    Eg 7.3:

    * This program displays Hello 3 times.

    MOVE 1 TO COUNTER1.PERFORM 200-DISP-RTN UNTIL COUNTER1 = 4.

    :STOP RUN.

    200-DISP-RTN.DISPLAY HELLO.ADD 1 TO COUNTER1.

    PERFORM..TIMESThe PERFORM..TIMES construct is used to execute a sequence of steps a fixed number

    of times. It is not necessary to establish a counter that must be incremented each timethrough the loop. When using the TIMES format (PERFORM paragraph-name-1identifier-1 TIMES) : (1) the identifier must be specified in the DATA DIVISION; (2) itmust have a numeric PICTURE clause; and (3) it must contain only integers or zeros.

    Eg 7.4:

    * This program displays the Hello 3 times.MOVE 3 TO COUNTER1.

    PERFORM 200-DISP-RTN COUNTER1 TIMES.:

    39

  • 7/28/2019 v1 Cobol Mat

    40/101

    COBOL

    STOP RUN.200-DISP-RTN.

    DISPLAY HELLO.

    GO TOA GO TO permanently transfers control to another paragraph. It does not return

    control to the paragraph from where it was issued.

    40

  • 7/28/2019 v1 Cobol Mat

    41/101

    COBOL

    8.File Handling

    File Organization methods

    It is important that file be so organized that efficient processing can beaccomplished by matching the file data characteristics, processing method, and fileorganization. Basically, three methods of file organization are available on disk systems :sequential, indexed sequential, and relative file organization.

    Sequential file organization

    Sequential file organization indicates that the records in the file are positioned in asequential order, such as according to part number.

    Indexed sequential file organizationIndexed sequential file organization is one in which the records are filed

    sequentially, but a table (index is available which identifies the location of groups ofrecords, thereby reducing access time.

    Relative file organizationRelative file organization is such that the logical order and physical order of the

    records do not necessarily correspond with one another. For such a file, a technique, or

    rule, is required to determine the location of the record in the disk system.

    This chapter shows you how to process sequential files in COBOL.

    Any program that (1) reads data from input files or (2) produces output files,requires an INPUT-OUTPUT SECTION and a FILE SECTION to describe the input andoutput areas.

    INPUT-OUTPUT SECTION

    The INPUT-OUTPUT SECTION of the ENVIRONMENT DIVISION follows theCONFIGURATION SECTION and supplies information concerning the input and outputdevices used in the program. In the FILE-CONTROL paragraph, a file-name is selectedfor each file to be used in the program; in addition, each file-name selected is assigned toa device. The SELECT statement is coded in Area B.

    Eg 8.1:

    ENVIRONMENT DIVISION.

    41

  • 7/28/2019 v1 Cobol Mat

    42/101

    COBOL

    :INPUT-OUTPUT SECTION.FILE-CONTROL.

    SELECT EMPLOYEE-FILE

    ASSIGN TO EMP.DATORGANIZATION IS LINE SEQUENTIAL.

    FILE SECTION.Each file is described in the FILE SECTION with an FD sentence that may

    consist of a series of clauses. After the clauses are specified, the FD sentence ends with aperiod. FD is an abbreviation for File Description. Each FD entry will describe a filedefined in a SELECT statement in the ENVIRONMENT DIVISION.

    The two entries, DATA DIVISION and FILE SECTION, are coded in Area A. FDis also coded in Area A. The file-name, however, is typically coded in Area B.

    Eg 8.2:

    DATA DIVISION.FILE SECTION.FD EMPLOYEE-FILE

    LABEL RECORDS ARE STANDARDRECORD CONTAINS 70 CHARACTERSBLOCK CONTAINS 10 RECORDS.

    Label Records

    Label records are usually created as the first and last records of a disk or tape to

    provide identifying information about the file on disk or tape. Labels are created onoutput files so that, when the same file is later read as input, the labels may be checked toensure that the file being accessed is the correct one. Labels are created on output filesand checked on input files. The COBOL compiler will supply the routine for writinglabels on output files or for checking labels on input file if the entry LABEL RECORDSARE STANDARD is included.

    This LABEL RECORDS clause will result in the following :1. For output files, the first record on disk or tape file will be created as a standard

    80-position header label identifying the file to the system; similarly, the lastrecord on the disk or tape will be created as a trailer label.

    2. For input files, these labels will be computer-checked to ensure that the file beingprocessed is the correct one.

    The clause LABEL RECORDS ARE STANDARD is permitted for disk and tape filesonly. Devices such as printers do not use label records, since identifying information isunnecessary where data is visible to the human eye. The clause LABEL RECORDS AREOMITTED is used for such files.

    42

  • 7/28/2019 v1 Cobol Mat

    43/101

    COBOL

    RECORD CONTAINS clauseThe RECORD CONTAINS clause indicates the size of each record. For printer

    files the RECORD CONTAINS clause may include one extra position that is used to

    control the spacing of the form (e.g., single spacing, double spacing). Thus, for 132character printers, a record size is sometimes set as 133 characters. In such cases, the firstor leftmost position in these 133-position print records is the form control position; it isnot actually printed.

    BLOCK CONTAINS clauseThe BLOCK CONTAINS clause is included in the File Description entry only for

    files in which disk or tape records have been blocked. Blocking is a technique thatincreases the speed of input/output operations and makes more effective use of storagespace on disk and tape. A group of logical records is included within one block to

    maximize the efficient use of a disk or tape area. For example, reading in a block of 10disk records, is more efficient than reading in each disk record separately. Even ifblocking is used, the program processes records in the standard way, that is, one logicalrecord at a time.

    Record Description entriesA record is a unit of information consisting or related data items within a file.

    Most often, a file consists of records that all have the same length and format. These arecalled fixed-length records.

    For each file defined, we have one record format.

    Eg 8.3:01 EMPLOYEE-REC.

    05 EMP-NAME.10 EMP-FIRST-NAME PIC X(10).10 EMP-LAST-NAME PIC X(15).

    05 EMP-DEPT PIC X(4).05 EMP-SALARY PIC 9(5)V99.05 EMP-DOJ PIC 9(6).

    Input/output verbsThere are 4 input/output verbs : OPEN, READ, WRITE, CLOSE.

    OPEN statement

    Before an input or an output file can be used by the program it must be opened.An OPEN statement, designates files as either input or output. It also accesses thespecific devices, and makes the files available for processing. It performs header labelroutines if label records are STANDARD. The OPEN statement checks the header labelto determine if the correct file has been accessed.

    43

  • 7/28/2019 v1 Cobol Mat

    44/101

    COBOL

    Eg 8.4:

    OPEN INPUT EMPLOYEE-FILE.

    OPEN OUTPUT REPORT-FILE.

    The order in which files are opened is not significant. The only restriction is that afile must be opened before it may be read or written; a file must be accessed before itmay be processed. Since the OPEN statement accesses the files, it is generally on of thefirst instructions coded in the PROCEDURE DIVISION.

    READ statement

    After an input file has been opened, it may be read. A READ statement transmitsdata from the input device, assigned in the ENVIRONMENT DIVISION, to the input

    storage area, defined in the FILE SECTION of the DATA DIVISION.The primary function of the READ statement is to transmit one data record to the

    input area reserved for that file. That is, each time a READ statement is executed, onerecord is read into primary storage.

    The READ statement has, however, several other functions. Like the OPENstatement, it performs certain checks. It checks the length of each input record to ensurethat it corresponds to the length specified in a RECORD CONTAINS clause in the dataDIVISION. If a discrepancy exists, an error message prints, and a program interruptoccurs.

    The READ statement will also use the BLOCK CONTAINS clause, if specified,to perform a check on the blocking factor.

    The AT END clause in the READ statement tests to determine if there is any moreinput. An AT END clause of the READ statement tells the computer what to do if there isno more data to be read.

    Eg 8.5:

    READ EMPLOYEE-FILEAT END

    MOVE YES TO END-OF-FILE.

    WRITE statement

    The WRITE instruction takes data in the output area defined in the DATADIVISION and transmits it to the device specified in the ENVIRONMENT DIVISION.

    Note that although files are read, we write records. The record-name appear on the01 level and is generally subdivided into fields. The record description specifies theformat of the output.

    44

  • 7/28/2019 v1 Cobol Mat

    45/101

    COBOL

    Eg 8.6:

    WRITE EMPLOYEE-REC.

    CLOSE statement

    A CLOSE statement is coded at the end of the job after all records have beenprocessed to release these files and deactivate the devices. All files that have been openedat the beginning of the program are closed at the end of a program. The CLOSEstatement, like the OPEN, will perform additional functions. When creating disk or taperecords, for example, the CLOSE will create trailer labels; it will also rewind a tape.

    Eg 8.7:CLOSE EMPLOYEE-FILE.

    COPY statementA COPY statement is used to bring into a program a series of prewritten COBOL

    entries that have been stored in a library. Copying entries from a library, rather thancoding them, has the following benefits : (1) it could save a programmer a considerableamount of coding and debugging time; (2) it promotes program standardization since allprograms that copy entries from a library will be using common data-names and/or

    procedures; (3) it reduces the time it takes to make modifications and reduces duplicationof effort; if a change needs to be made to a data entry, it can be made just once in thelibrary without the need to alter individual programs; and (4) library entries areextensively annotated so that they are meaningful to all users; this annotation results inbetter-documented programs and systems.

    Most often, the COPY statement is used to copy FD and 01 entries that define anddescribe files and records. In addition, standard modules to be used in the PROCEDUREDIVISION of several programs may also be stored in a library and copied as needed.

    Contents of EMP.REC

    Eg 8.8a:

    01 EMPLOYEE-REC.05 EMP-NAME.10 EMP-FIRST-NAME PIC X(10).10 EMP-LAST-NAME PIC X(15).

    05 EMP-DEPT PIC X(4).05 EMP-SALARY PIC 9(5)V99.05 EMP-DOJ PIC 9(6).

    The DATA DIVISION entry using a COPY statement

    45

  • 7/28/2019 v1 Cobol Mat

    46/101

    COBOL

    Eg 8.8b:

    DATA DIVISION.FILE SECTION.FD EMPLOYEE-FILE

    LABEL RECORDS ARE STANDARDRECORD CONTAINS 70 CHARACTERSBLOCK CONTAINS 10 RECORDS.

    COPY EMP.REC.

    A program to create the employee file.

    Eg 8.9:

    IDENTIFICATION DIVISION.

    PROGRAM-ID. FILE-CRT.* This program creates a sequential EMPLOYEE file.

    ENVIRONMENT DIVISION.INPUT-OUTPUT SECTION.FILE-CONTROL.

    SELECT EMPLOYEE-FILE ASSIGN TO "EMP.DAT"ORGANIZATION IS LINE SEQUENTIAL.

    DATA DIVISION.FILE SECTION.FD EMPLOYEE-FILE

    LABEL RECORDS STANDARD.01 EMPLOYEE-REC.

    05 EMP-NO PIC 9(4).

    05 EMP-NAME.10 EMP-FIRST-NAME PIC X(10).10 EMP-LAST-NAME PIC X(15).

    05 EMP-DEPT PIC X(4).05 EMP-SALARY PIC 9(5)V99.05 EMP-DOJ PIC 9(6).

    WORKING-STORAGE SECTION.01 WS-ANS PIC X(01) VALUE "Y".

    88 ANS-NO VALUE "N" "n".

    PROCEDURE DIVISION.0000-MAIN.

    OPEN OUTPUT EMPLOYEE-FILE.PERFORM 1000-ACPT-PARA UNTIL ANS-NO.CLOSE EMPLOYEE-FILE.STOP RUN.

    1000-ACPT-PARA.DISPLAY "ENTER YOUR EMP CODE : " WITH NO ADVANCING.ACCEPT EMP-NO.DISPLAY "ENTER YOUR FIRST NAME : " WITH NO ADVANCING.

    46

  • 7/28/2019 v1 Cobol Mat

    47/101

    COBOL

    ACCEPT EMP-FIRST-NAME.DISPLAY "ENTER YOUR LAST NAME : " WITH NO ADVANCING.ACCEPT EMP-LAST-NAME.DISPLAY "ENTER YOUR DEPARTMENT : " WITH NO ADVANCING.

    ACCEPT EMP-DEPT.DISPLAY "ENTER YOUR SALARY : " WITH NO ADVANCING.ACCEPT EMP-SALARY.DISPLAY "ENTER YOUR DATE OF JOINING : " WITH NO ADVANCING.ACCEPT EMP-DOJ.WRITE EMPLOYEE-REC.DISPLAY "DO YOU WANT TO ADD MORE RECORDS : "

    WITH NO ADVANCING.ACCEPT WS-ANS.

    47

  • 7/28/2019 v1 Cobol Mat

    48/101

    COBOL

    9.Sorting and merging files

    SortingRecords in files frequently must be sorted into specific sequences for updating,

    answering inquiries, or generating reports. Sorting is a common procedure used forarranging records into a specific order so that sequential processing can be performed.

    COBOL has a SORT verb, which can make it very useful as part of a COBOLprogram.

    The programmer must specify whether the key field is to be an ASCENDINGKEY or a DESCENDING KEY, depending on which sequence is required :

    ASCENDING : From lowest to highestDESCENDING : From highest to lowest

    The SORT verb may be used to sequence records with more than one key field.For example, to sort an employee file so that it is in alphabetic sequence by name withineach department.

    Eg 9.1:

    SORT SORT-FILE

    ON ASCENDING KEY S-EMP-DEPTON ASCENDING KEY S-EMP-NAMEUSING EMPLOYEE-FILEGIVING SORT-EMPLOYEE-FILE.

    There are three major files used in a sort :1. Input file : File of unsorted input records.2. Work or sort file : File used to store records temporarily during the sorting

    process.3. Output file: File of sorted output records.

    All these files would be defined in the ENVIRONMENT DIVISION using standardASSIGN clauses, which are system dependent. The SORT-FILE is actually assigned to atemporary work area that is used during processing but not saved. Only the unsorted diskfile and the sorted output disk file are assigned standard file-names so that they can bepermanently stored.

    FDs are used in the DATA DIVISION to define and describe the input and output filesin the usual way. The sort or work file is described with an SD (sort file description)entry. The only difference between SD and FD entries is that an SD must not have aLABEL RECORDS clause. Note, too, that the field(s) specified as the KEY field(s) forsorting purposes must be defined as part of the sort record format.

    Eg 9.2:

    ENVIRONMENT DIVISION.

    48

  • 7/28/2019 v1 Cobol Mat

    49/101

    COBOL

    :INPUT-OUTPUT SECTION.FILE-CONTROL.

    SELECT SORT-FILE

    ASSIGN TO DISK.:

    DATA DIVISION.FILE SECTION.SD SORT-FILE.01 SORT-REC.

    05 S-EMP-NAME.10 S-EMP-FIRST-NAME PIC X(10).10 S-EMP-LAST-NAME PIC X(15).

    05 S-EMP-DEPT PIC X(4).05 FILLER PIC 9(13).

    The SORT statement can, however, be used in conjunction with procedures thatprocess records just before they are sorted and/or process records after they are sorted.

    INPUT PROCEDURE

    The INPUT PROCEDURE processes data from the incoming file prior to sorting.An INPUT PROCEDURE may be used to perform the following operations prior tosorting : (1) validate data in the input records, (2) eliminate records with blank fields, (3)count input records.

    With COBOL 74, the procedure-name of an INPUT PROCEDURE must be a

    section-name and not a paragraph-name. A section is a series of PROCEDUREDIVISION paragraphs that is treated as a single entry or unit. Rule for forming section-names are the same as rules for forming paragraph-names. The word SECTION,however, follows a section-name (e.g., A000-ERROR SECTION). The end of a section isrecognized when another section-name is encountered, or when the end of the program isreached.

    Code for an INPUT PROCEDUREEg 9.3:

    SORT SORT-FILE

    ON ASCENDING KEY S-EMP-DEPT

    ON ASCENDING KEY S-EMP-NAMEINPUT PROCEDURE A000-TEST-ITGIVING SORT-EMPLOYEE-FILE.

    STOP RUNA000-TEST-IT SECTION.A100-PARA-1.

    OPEN INPUT IN-FILE.READ IN-FILEAT END MOVE NO TO ARE-THERE-MORE RECORDS.

    49

  • 7/28/2019 v1 Cobol Mat

    50/101

    COBOL

    PERFORM A200-TEST-RTNUNTIL THERE-ARE-NO-MORE-RECORDS.

    CLOSE IN-FILE.GO TO A300-TEST-IT-EXIT.

    A200-TEST-RTN.IF QTY = ZEROS

    NEXT SENTENCEELSEMOVE IN-REC TO SORT-RECRELEASE SORT-REC.

    READ IN-FILEAT END MOVE NO TO ARE-THERE-MORE RECORDS.

    A300-TEST-IT-EXIT.EXIT.

    Explanation1. The first section in the PROCEDURE DIVISION contains the SORT instruction,

    any processing to be performed before or after the SORT verb is executed, and aSTOP RUN.

    2. The second section begins with the main module of the INPUT PROCEDURE. Itopens the input file, reads the first record, and then performs a process routine (ina separate paragraph within this second section) until there is no more data.

    3. After the separate paragraph is executed until ARE-THER-MORE-RECORDS =NO, control returns to the main module of the second section to be terminated,control must pass to the last statement within the section. This means that a GOTO is required. We code GO TO A300-TEST-IT-EXIT as the last sentence. Since

    no operations are required in this last paragraph, EXIT is coded, which passescontrol back to the SORT statement, where the file is then sorted.

    OUTPUT PROCEDURE

    The OUTPUT PROCEDURE is used to process the sorted records prior to, orperhaps even instead of, placing them in the output file. The OUTPUT PROCEDUREcan be used instead of the GIVING option. The OUTPUT PROCEDURE is similar to theINPUT PROCEDURE. When the INPUT PROCEDURE is complete, the file is thensorted. An OUTPUT PROCEDURE processes all sorted records in the sort file andhandles the transfer of these records to the output file.

    In an INPUT PROCEDURE we RELEASE records to a sort file rather thanwriting them. In an INPUT PROCEDURE we RETURN records from the sort file ratherthan reading them.

    MergingThe MERGE statement combines two or more files into a single file. Its format is

    similar to the SORT. The file to be merged is a work file designated as an SD. At least

    50

  • 7/28/2019 v1 Cobol Mat

    51/101

    COBOL

    two file-names must be included for a merge, but more than two are permitted. Unlike,the SORT, however, an INPUT PROCEDURE may not be specified with the MERGEstatement. That is, using the MERGE statement, you may only process records after they

    have been merged, not before. The OUTPUT PROCEDURE has the same format as withthe SORT.

    Eg 9.4:

    MERGE MERGE-FILEON ASCENDING KEY M-EMP-DEPTUSING OLD-PAYROLL

    NEW-PAYROLLGIVING EMPLOYEE-FILE.

    10.Trapping Runtime errors

    The FILE STATUS clause

    The FILE STATUS clause can be used with the SELECT statement to determinethe exact type of input or output error that has occurred when either reading from orwriting to a file. The SELECT statement could include FILE STATUS as its last clause.The data name specified with the FILE STATUS clause must appear in the WORKING-STORAGE as a two-position alphanumeric field.

    Eg 10.1:SELECT EMPLOYEE-FILE

    ASSIGN TO EMP.DATORGANIZATION IS LINE SEQUENTIALFILE STATUS IS WS-STATUS.

    :WORKING-STORAGE SECTION.01 WS-STATUS PIC X(2).

    The possible values that may be placed in the FILE STATUS field when an inputor output operation is performed are listed in Appendix E.

    OPEN INPUT EMPLOYEE-FILE.IF WS-STATUS NOT = 00

    DISPLAY ERROR OPENING EMPLOYEE FILESTOP RUN.

    READ EMPLOYEE-FILE .::

    51

  • 7/28/2019 v1 Cobol Mat

    52/101

    COBOL

    11.CONTROL BREAK PROCESSING

    What is a control break procedure ?

    A control break procedure is used if records are in sequence by a control field andthe number of records in each control field is variable.

    Steps for a Control Break

    2. Read the initial record.1. Move the control field to a hold area in WORKING-AREA.

    2. As long as the control field is equal to the hold area, execute the detail routine forthe input record. This means : Add the appropriate amount to a control total, printthe detail record and read the next record.

    3. If the control field is not equal to the hold area :Print the control total.Initialize the control total field to zero.Reinitialize the hold field with the new control field value if there are morerecords.Process the detail record as in step 3.Print headings on a new page if each control total is to appear on a separate page.

    4. If required, after all records have been processed perform a control break to print

    the last control group.

    52

  • 7/28/2019 v1 Cobol Mat

    53/101

    COBOL

    12.Indexed Files

    An indexed file is really two files the data file, which is created in sequence butcan be accessed randomly, and the index file, which contains the value of each key fieldand the disk address of the record with that corresponding key field. To access an indexedrecord randomly, the key field is looked up in the index file to find the disk address of therecord; then the record is accessed in the indexed data file directly.

    The index on a disk is similar to a books index, which has unique subjects (keys)and their corresponding page numbers (addresses). There would be two ways to find atopic in the book. You can read the book sequentially, from the beginning, until that topicis found, but this would be very time consuming and inefficient. The best method would

    be to look up the topic in the index, find the corresponding page number, and go directlyto that page. This is precisely how records can be accessed on a disk file that has anindex.

    With an indexed file, records can be accessed either sequentially or randomly,depending on the users needs. The term random access implies that records are to beprocessed or accessed in some order other than the one in which they were physicallywritten on the disk.

    Creating an Indexed FileIndexed files are created in sequence; that is, the indexed file is created by reading

    each record from an input file, in sequence by the key field, and writing the outputindexed disk records in the same sequence. Note, however, once the indexed file iscreated, it can be accessed randomly.

    The ORGANIZATION clauseThe clause ORGANIZATION IS INDEXED indicates that the file is to be created with anindex.

    The ACCESS clauseSince indexed files may be accessed either sequentially or randomly, the ACCESS

    clause is used to denote which method will be used in the specific program. If theACCESS clause is omitted, the compiler will assume that the file is being processed inSEQUENTIAL mode.

    The RECORD KEY clauseThe RECORD KEY clause names the key field within the disk record that will be used toform the index. This field must be in the same physical location in each index record.Usually, it is the first field. It must have a unique value for each record.

    53

  • 7/28/2019 v1 Cobol Mat

    54/101

    COBOL

    Eg 12.1:

    ENVIRONMENT DIVISION.INPUT-OUTPUT SECTION.FILE-CONTROL.

    SELECT IND-EMP-FILEASSIGN TO INDEMP.DATORGANIZATION IS INDEXEDACCESS IS SEQUENTIALRECORD KEY IS I-EMP-NO.

    DATA DIVISION.FILE SECTION.FD IND-EMP-FILE

    LABEL RECORDS STANDARD.01 IND-EMP-REC.

    05 I-EMP-NO PIC 9(4).05 I-EMP-NAME PIC X(25).05 I-EMP-DEPT PIC X(4).05 I-EMP-SAL PIC 9(5)V99.

    The INVALID KEY clauseWith WRITE

    The INVALID KEY clause is used with a WRITE instruction to test for two

    possible errors : (1) a key field that is not in sequence or (2) a key field that is the same asone already on the indexed file. If any of these conditions exist, we call this an INVALIDKEY condition. The computer checks for an INVALID KEY prior to writing the record.

    Thus, if you use an INVALID KEY clause with the WRITE statement and arecords has an erroneous key, the record is not written and the statement(s) followingINVALID KEY would be executed.

    Eg 12.2:

    WRITE IND-EMP-RECINVALID KEY

    PERFORM 2000-ERROR-PARA.

    With READWhen reading a disk file randomly, we do not test for an AT END condition

    because we are not reading the file in sequence; instead, we include an INVALID KEYtest. If there is no record in the INDEXED-FILE with a RECORD KEY equal to T-EMP-NO, the INVALID KEY clause will be executed.

    Eg 12.3:

    54

  • 7/28/2019 v1 Cobol Mat

    55/101

    COBOL

    DISPLAY ENTER EMPLOYEE CODE :ACCEPT T-EMP-EMP-NO.MOVE T-EMP-NO TO I-EMP-NO.READ IND-EMP-FILE

    INVALID KEYPERFORM 600-ERR-RTN.

    DELETE verb

    The DELETE verb can be used to delete records from indexed files. Note that weuse the file-name with the DELETE verb, but the word RECORD can be specified aswell. That is, both the statements DELETE INDEXED-FILE and DELETE INDEXED-FILE RECORD can be used to delete the record in the INDEXED-FILE storage area.

    To delete a record from an indexed file, you should first read the record into

    storage and then instruct the computer to delete it.

    Eg 12.4:

    MOVE Y TO WS-FOUND.MOVE 1001 TO I-EMP-NO.READ IND-EMP-FILE

    INVALID KEYMOVE N TO WS-FOUND.

    IF WS-FOUND = YDELETE IND-EMP-FILEINVALID KEYDISPLAY ERROR DELETING RECORD.

    Using ALTERNATE RECORD KEYs

    Indexed files may be created with, and accessed by, more than one identifying keyfield. That is, we may want to access employee records using the name as the key field.To enable a file to be accessed randomly using more than one key field, we would need toestablish an ALTERNATE RECORD KEY.

    To establish multiple key fields for indexing, we use an ALTERNATE RECORDKEY clause in the SELECT statement.

    Note:

    1. More than one ALTERNATE record key can be used.2. WITH DUPLICATES means than an ALTERNATE RECORD KEY need not be

    unique. Thus, fields like EMP-DEPT can be used as a key even though numerousrecords may have the same department no.

    3. A record can be accessed by its RECORD KEY or any of its ALTERNATERECORD KEYs.

    Eg 12.5:

    55

  • 7/28/2019 v1 Cobol Mat

    56/101

    COBOL

    SELECT IND-EMP-FILEASSIGN TO INDEMP.DATORGANIZATION IS INDEXEDACCESS IS SEQUENTIAL

    RECORD KEY IS I-EMP-NOALTERNATE RECORD KEY IS I-EMP-DEPT WITH DUPLICATES.

    Accessing records randomly by alternate record key

    The program that accesses the file by key field has the same SELECT clauseexcept that ACCESS IS RANDOM rather than SEQUENTIAL. In the PROCEDUREDIVISION , we can access records by either I-EMP-NO , the record key, or I-EMP-DEPT, the alternate key.

    The KEY clause is used with the READ statement when an indexed file hasALTERNATE RECORD KEYs that we want to use to randomly access a record. If theKEY clause is omitted when accessing a file randomly, the RECORD KEY is assumed tobe the KEY used for finding the record.

    Suppose ALTERNATE RECORD KEY WITH DUPLICATES was specified inthe ENVIRONMENT DIVISION and there is more than one record with the sameALTERNATE RECORD KEY. The first one that was actually placed on the disk will bethe one retrieved by the READ.

    The START statement

    The START statement enables a program to begin processing an indexed file

    sequentially but at a record location other than the first or next physical record in the file.The access of the file is to be in sequence (ACCESS IS SEQUENTIAL) if we use theRECORD KEY for finding a record, even though we want to start the access at somepoint other than the beginning. The ACCESS IS DYNAMIC clause is used if we want tobegin the processing an indexed file based on the contents of the ALTERNATE RECORDKEY.

    When the record to be accessed has a key equal to the one placed in the RECORDKEY, the KEY clause in the START statement is not required. The INVALID clause isexecuted only if no such record is found.

    Note that the START locates the desired record but it does not READ it intostorage. The record must always be brought into storage with a READ statement.

    Eg 12.6:

    MOVE Y TO WS-FOUND.MOVE 1001 TO I-EMP-NO.START IND-EMP-FILE

    KEY > I-EMP-NOINVALID KEY DISPLAY THERE IS NO EMP NO > 1001

    MOVE N TO WS-FOUND.IF WS-FOUND = Y

    56

  • 7/28/2019 v1 Cobol Mat

    57/101

    COBOL

    READ IND-EMP-FILEAT END

    MOVE Y TO WS-EOF.

    Suppose we wish to begin processing with an I-EMP-NO greater than 006. Wemust include a KEY clause with the START because we wish to position the file atallocation greater than the value of a RECORD KEY. The KEY clause can be omittedonly if the record to be located has a RECORD KEY equal to the one stored.

    The ACCESS IS DYNAMIC clause

    Sometimes we wish to access an indexed file both randomly and sequentially in asingle program. For this, we say that ACCESS IS DYNAMIC.

    In addition to using ACCESS IS DYNAMIC for combining sequential and

    random access techniques in a single program, we can use this clause to access records byALTERNATE RECORD KEY. Also, when records ate to be accessed by both RECORDKEY and ALTERNATE RECORD KEY, use ACCESS IS DYNAMIC.

    Rules for using the START statement

    1. The file must be accessed with (a) ACCESS IS SEQUENTIAL for readingrecords in sequence by the RECORD KEY or (b) ACCESS IS DYNAMIC forreading records in sequence by an ALTERNATE RECORD KEY.

    2. The file must be opened as either input or I-O.3. If the KEY phrase is omitted, the relational operator IS EQUAL TO is implied

    and the primary record key is assumed to be the key of reference.4. We use KEY =, >, NOT < for accessing records by ALTERNATE RECORD KEY.

    We also use KEY >, NOT < for accessing records by a value that iscorrespondingly >, NOT < the primary RECORD KEY.

    The READNEXT RECORD Instruction

    To process records both randomly and sequentially in a single file, ACCESS ISDYNAMIC must be specified. To indicate that we wish to read records in sequence bysome key field from a file accessed dynamically, we must use a NEXT RECORD clause.

    The READNEXT RECORD is also used for sequentially reading from a file by thisALTERNATE RECORD KEY, or, for beginning a sequential read at some point otherthan the beginning of a file.

    57

  • 7/28/2019 v1 Cobol Mat

    58/101

    COBOL

    13.Table Handling

    Use of OCCURS clause

    1. An OCCURS clause is used in COBOL to indicate the repeated occurrence offields with the same format.

    2. Defining a series of totals in WORKING-STORAGE to which amounts areadded; after all data is accumulated, the total can be printed.

    3. Defining a table in WORKING-STORAGE to be accessed by each input record.For example, using the contents of some input field to look up the required datain the table.

    Eg 13.1:

    01 TEMP-REC.05 TEMPERATURE OCCURS 24 TIMES PIC S9(3).

    Subscript

    A subscript is used in the PROCEDURE DIVISION to indicate which specificitem within the array we wish to access.

    The subscript is used along with the identifier that is defined with an OCCURS, torefer to an item within an array.

    Eg 13.2:

    MOVE TEMPERATURE (2) TO TEMP-OUT.

    Rules for OCCURS and subscriptsThere must be at least one space between the identifier and the left parenthesis

    that precedes the subscript. Similarly, the subscript must be enclosed in parentheses withno spaces within the parentheses.

    A subscript may be a numeric literal or a data-name with a numeric PICTUREclause.

    An OCCURS clause may be used on levels 02-49 only, because 01 level is usedfor defining records not fields.

    COBOL 74 permits only upto three levels of OCCURS clauses

    Using OCCURS with VALUE and REDEFINES clausesSometimes we want to initialize elements in a table or an array with specific values.With COBOL 74, we cannot use a VALUE clause with an entry defined by an

    OCCURS clause. Instead we can define the field with one value and then redefine that

    58

  • 7/28/2019 v1 Cobol Mat

    59/101

    COBOL

    storage area into separate array elements. As a result, each array element will have adifferent value.

    Eg 13.3:

    01 MONTH-NAMES.05 STRING-1 PIC X(36) VALUE

    JANFEBMARAPRMAYJUNJULAUGSEPOCTNOVDEC.05 MONTH REDEFINES STRING-1 OCCURS 12 TIMES PIC XXX.

    The SEARCH statementThe best method for searching a table is with the use of a SEARCH statement.

    The identifier used with the SEARCH verb is the table entry name specified on the

    OCCURS level, not on the 01 level. The WHEN clause indicates what action is to betaken when the condition specified is actually met. Additional comparisons betweensearch and table arguments can be made using other WHEN clauses. The AT END clausespecifies what should be done if the table has been completely searched and no match isfound.

    To use a SEARCH statement, two additional entries are required : the INDEXEDBY clause along with OCCURS, and the SET statement in the PROCEDURE DIVISION.

    Table entries must be specified with an index rather than a subscript. An index issimilar to a subscript, but it is defined along with the table entries as part of the OCCURSdescription.

    Eg 13.4:01 SALES-TAX-TABLE,

    05 TABLE-ENTRIES OCCURS 1000 TIMES INDEXED BY X1.10 WS-ZIPCODE PIC 9(5).10 WS-TAX-RATE PIC V999.

    However, unlike a subscript, an index is not defined separately in WORKING-STORAGE. The compiler automatically provides an appropriate PICTURE clause. Anindex is processed more efficiently than a subscript, because the index contains thedisplacement from the start of the table.

    A SEARCH statement does not automatically initialize the index at 1 becausesometimes we may want to begin searching a table at some point other than thebeginning. Initializing an index at 1 must be performed by a SET statement prior to theSEARCH.

    Eg 13.5:

    SET X1 TO 1.SEARCH TABLE-ENTRIES

    AT END MOVE 0 TO WS-SALES-TAXWHEN ZIP-IN = WS-ZIPCODE (X1)

    59

  • 7/28/2019 v1 Cobol Mat

    60/101

    COBOL

    COMPUTE ........

    An index can be used to reference an element only in the table or array for which it

    was defined.With 2 WHEN clauses, the computer begins by performing the first comparison. Only

    if the condition in the first WHEN is not met does it test the second WHEN.To search for multiple matches in a table, it is better to use a PERFORM rather than a

    SEARCH statement for processing the entire table.If you have parallel table with CUST-NO-TABLE storing 25 customer numbers and

    CUST-ARRAY storing the corresponding BAL-DUE for each. In such a case theSEARCH... VARYING can be used.

    Eg 13.6:

    SET X1, X2 TO 1.

    SEARCH EACH-CUST-NO VARYING X2AT END PERFORM 300-ERR-RTNWHEN CUST-NO-IN = EACH-CUST-NO (X1)

    ADD AMT-IN TO BAL-DUE (X2).

    Binary searchWhen table entries are arranged in sequence by some field, such as T-CUSTOMER-

    NO, the most efficient type of look-up is a binary search. The SEARCH ALL verb isused to perform a binary search. A SET statement is not necessary with the SEARCHALL, since the computer sets the index to the appropriate point initially when each binarysearch begins.

    Limitations of the SEARCH ALL1. The condition following the word WHEN can test only for equality.2. If the condition following the word WHEN is a compound conditional :

    Each part of the conditional can only consist of a relational test thatinvolves an equal condition.

    The only compound condition permitted is with ANDs, not Ors.3. Only one WHEN clause can be used with a SEARCH ALL.4. The VARYING option may not be used with the SEARCH ALL.5. The OCCURS item and its index, which define the table argument, must appear to

    the left of the equal sign.

    Eg 13.7:

    WHEN S-AMT (X1) = AMT1 .....

    To use the SEARCH ALL statement, we must indicate which table entry will serve asthe key field. The identifier used in the ASCENDING KEY clause must be an entrywithin the table and it must appear before the INDEXED BY clause.

    60

  • 7/28/2019 v1 Cobol Mat

    61/101

    COBOL

    Eg 13.8:

    01 TABLE-1.05 DISCOUNT-TABLE OCCURS 50 TIMES

    ASCENDING KEY T-CUSTOMER-NOINDEXED BY X1.10 T-CUSTOMER-NO PIC 9(4).10 T-DISCOUNT-PCT PIC V999.

    Eg 13.9:

    01 INVENTORY-TABLE.05 WAREHOUSE OCCURS 50 TIMES INDEXED BY X1.

    10 ITEM-X OCCURS 100 TIMES INDEXED BY X2.15 PART-NO PIC 9(4).15 UNIT-PRICE PIC 999V99.

    The identifier used with the SEARCH refers to the lowest-level OCCURS entry.e.g. SEARCH ITEM-X.Note that SEARCH ITEM-X increments the lowest-level index only. Hence if X1 is setto 1 initially, the SEARCH will perform a look-up on items in warehouse 1 only, that is(1,1) to (1,100).

    To search all warehouses, the major index X1 must be incremented.

    61

  • 7/28/2019 v1 Cobol Mat

    62/101

    COBOL

    14.Call statement

    Structured programs should consist of a series of independent modules that areexecuted from the main module.

    When programs are properly structured :1. Each module may be written, compiled, and perhaps even tested

    independently.2. The modules may be written in different stages, in atop-down manner.

    They may even be coded by different programmers.3. If a specific module needs to be modified, the entire logical flow should

    still function properly without the need for extensive revision to other

    parts of the program.

    Modules within a program can be viewed as subroutines that are called orexecuted from the main module. But a program may also CALL or reference independentsubprograms stored in a library that are entirely separate from the main program itself.The main program that references or calls a subprogram is referred to as the callingprogram. The subprogram that is linked and executed within the main program is referredto as the called program.

    The called program would need to be compiled so that it may be called whenneeded.

    When the CALL is performed, data is passed from the calling to the called program (if

    the calling program has assigned values to fields used in the called program). Theentire called program is executed, data is passed from the called program back to thecalling program, and control return to the calling program.

    Called Program Requirements

    PROGRAM-ID.The literal used in the CALL statement of the main program to extract a subprogram orroutine from a library and execute it must be identical to the called programs

    PROGRAM-ID. Note that the literal is enclosed in quotation marks when used in theCALL statement.

    LINKAGE SECTIONA LINKAGE SECTION must be defined in the called program for identifying thoseitems that (1) will be passed to the called program from the calling program and (2)passed back from the called program to the calling program. The LINKAGE SECTIONof the called program, then, describes all items to be passed between the two programs.

    62

  • 7/28/2019 v1 Cobol Mat

    63/101

    COBOL

    The LINKAGE SECTION, if used, is coded after the FILE and WORKING-STORAGESECTIONs of the called program. This section is similar to WORKING-STORAGEexcept that VALUE clauses from initializing fields are not permitted in the LINKAGE

    SECTION.

    EXIT PROGRAMThe last executed statement in the called program must be the EXIT PROGRAM. Itsignals the computer to return control back to the calling program. With COBOL 74,EXIT PROGRAM must be the only statement in the last paragraph.

    Calling Program Requirements

    PROCEDURE DIVISION USING

    The identifiers specified in the USING clause in the PROCEDURE DIVISION entryinclude all fields defined in the LINKAGE SECTION; these identifiers will be passedfrom one program to the other. They are passed to and from corresponding identifiers inthe CALL USING of the main program.The USING clause of the CALL statement is required if the subprogram performs anyoperations in which data is to be passed from one program to another. The CALL USING identifies fields in the main or calling program that will be either passed to thecalled program before it is executed, or passed back to the calling program after thecalled program has been executed.

    Sample Program

    Calling Program : MAIN.CBLEg 14.1a:

    IDENTIFICATION DIVISION.PROGRAM-ID. MAIN.ENVIRONMENT DIVISION.DATA DIVISION.WORKING-STORAGE SECTION.01 MY-NAME PIC X(20).PROCEDURE DIVISION.MAIN.

    DISPLAY "ENTER NAME ".

    ACCEPT MY-NAME.CALL "DISP" USING MY-NAME.STOP RUN.

    Called Program : DISP.CBLEg 14.1b:

    IDENTIFICATION DIVISION.PROGRAM-ID. DISP.

    63

  • 7/28/2019 v1 Cobol Mat

    64/101

    COBOL

    ENVIRONMENT DIVISION.DATA DIVISION.LINKAGE SECTION.01 NM PIC X(20).

    PROCEDURE DIVISION USING NM.MAIN.

    DISPLAY "HELLO " NM.EXIT PROGRAM.

    64

  • 7/28/2019 v1 Cobol Mat

    65/101

    COBOL

    15.String Handling

    The STRING StatementA STRING statement may be used to combine several fields to form once concisefield. This process is called concatenation.

    05 NAME.10 LAST-NAME PIC X(10) VALUE EDISON.10 FIRST-NAME PIC X(10) VALUE THOMAS.10 MIDDLE-NAME PIC X(10) VALUE ALVA.

    01 NAME-OUT PIC X(33).

    STRING

    FIRST-NAME DELIMITED BY DELIMITED BY SIZEMIDDLE-NAME DELIMITED BY DELIMITED BY SIZELAST-NAME DELIMITED BY DELIMITED BY SIZEINTO NAME-OUT.

    Output

    NAME-OUT = THOMAS ALVA EDISON

    OVERFLOW OptionThe OVERFLOW option specifies the operation(s) to be performed if the receivingfield is not large enough to accommodate the result.

    POINTER OptionWe may count the number of characters actually moved to the receiving field if it isinitialized at zero.

    01 WS-COUNT PIC 99 VALUE 0.

    MOVE 1 TO WS-COUNT.

    STRING FIRST-NAME DELIMITED BY INTO NAME-OUTWITH POINTER WS-COUNT.

    Output

    WS-COUNT = 6

    65

  • 7/28/2019 v1 Cobol Mat

    66/101

    COBOL

    Rules for using the STRING statement

    1. The DELIMITED BY clause is required. It can indicate :SIZE : The entire sending field is transmitted.

    Literal : The transfer of data is terminated when the specified literal isencountered; the literal itself is not moved.Identifier : The transfer of data is terminated when the contents of theidentifier is encountered.

    2. The receiving field must be an elementary data item with no editingsymbols or JUSTIFIED RIGHT clause.

    3. All literals must be described as non-numeric.4. The identifier specified with the POINTER clause must be an elementary

    numeric item.5. The STRING statement move data from left to right just like

    alphanumeric fields are moved, but a STRING does not pad data from left

    to right just like alphanumeric fields are moved, but a STRING does notpad with low-order blanks, unlike an alphanumeric MOVE.

    The UNSTRING statementThe UNSTRING statement may be used to convert keyed data to a more compact formfor storing it on disk. For example, we can instruct the computer to separate the NAME-OUT into its components and store them without the commas.

    MOVE THOMAS,ALVA,EDISON TO NAME-OUT.UNSTRING NAME-OUT

    DELIMITED BY ,

    INTO FIRST-NAMEMIDDLE-NAMELAST-NAME.

    Output

    FIRST-NAME = THOMASMIDDLE-NAME = ALVALAST-NAME = EDISON

    Rules for using the UNSTRING statement

    1. The sending field must be non-numeric. The receiving fields may benumeric or non-numeric.2. Each literal must be non-numeric.3. The [WITH POINTER identifier] and [ON OVERFLOW imperative-

    statement] clauses may be used in the same way as with the STRING.

    66

  • 7/28/2019 v1 Cobol Mat

    67/101

    COBOL

    The INSPECT statementThe INSPECT statement may be used for replacing a specific character in a field withanother character. It can also be used for counting the number of occurrences of a given

    character.

    01 CTR-1 PIC 9 VALUE 0.01 WS-NAME PIC X(10).

    ACCEPT WS-NAME.INSPECT WS-NAME TALLYING CTR-1 FOR ALL SPACES.

    This code will check for the number of spaces in the field WS-NAME and store thevalue in the field CTR-1.

    Applications of the IN