Cobol400 Final

11
The COBOL Divisions The IDENTIFICATION DIVISION. 1. Identifies the program to the computer system. 2. PROGRAM-ID is the only required entry within the division. 3. A program name can be up to 10 characters and can consist of letters and digits. 4. All other paragraphs and identifying information in this division are optional. They could be included as comments. 5. Blank lines can be used anywhere in a COBOL program to make the program easier to read. 6. Comments can be included in the IDENTIFICATION DIVISION, as well as in all other divisions, by specifying an * in position 7. This makes the entire line a comment. A slash (/) in column 7 will cause the subsequent lines to be printed on the next page of the source listing. The ENVIRONMENT DIVISION. The ENVIRONMENT DIVISION consists of two sections: CONFIGURATION SECTION and INPUTOUTPUT SECTION. The CONFIGURATION SECTION is optional and is usually omitted. It supplies documentation about the source and object computers being used. The INPUT-OUTPUT SECTION is also optional but must be included if files are assigned to devices in a program. Since most programs use files, the INPUT-OUTPUT SECTION is normally included. The FILE-CONTROL paragraph of the INPUT-OUTPUT SECTION defines the files that are used in the program. The SELECT statement is used in the FILE-CONTROL paragraph to assign a file- name to each file used in the program, and specifies the device where the file is located. The DATA DIVISION. Defines and describes the formats of all input, output, and work areas used 1. for processing. FILE SECTION Each file-name defined in the ENVIRONMENT DIVISION must be described in an FD in the DATA DIVISION. Each record format within every file is defined as an 01 entry. Fields within each record are described with a PICTURE (PIC) clause that specifies the size and type of data. WORKING-STORAGE SECTION Defines any work areas needed for processing. An end-of-file indicator is specified in the WORKING-STORAGE SECTION; we will refer to this field as ARE-THERE-MORE-RECORDS. In our examples, the field called ARE- THEREMORE-

description

final cobol400

Transcript of Cobol400 Final

The COBOL DivisionsThe IDENTIFICATION DIVISION.1. Identifies the program to the computer system.2. PROGRAM-ID is the only required entry within the division.3. A program name can be up to 10 characters and can consist of letters and digits.4. All other paragraphs and identifying information in this division are optional. They could be included as comments.5. Blank lines can be used anywhere in a COBOL program to make the program easier to read.6. Comments can be included in the IDENTIFICATION DIVISION, as well as in all other divisions, by specifying an * in position 7. This makes the entire line a comment. A slash (/) in column 7 will cause the subsequent lines to be printed on the next page of the source listing.The ENVIRONMENT DIVISION.The ENVIRONMENT DIVISION consists of two sections: CONFIGURATION SECTION and INPUTOUTPUT SECTION.The CONFIGURATION SECTION is optional and is usually omitted. It supplies documentation aboutthe source and object computers being used.The INPUT-OUTPUT SECTION is also optional but must be included if files are assigned to devices in aprogram. Since most programs use files, the INPUT-OUTPUT SECTION is normally included.The FILE-CONTROL paragraph of the INPUT-OUTPUT SECTION defines the files that are used in theprogram.The SELECT statement is used in the FILE-CONTROL paragraph to assign a file-name to each file usedin the program, and specifies the device where the file is located.The DATA DIVISION.Defines and describes the formats of all input, output, and work areas used 1. for processing.FILE SECTIONEach file-name defined in the ENVIRONMENT DIVISION must be described in an FD in theDATA DIVISION. Each record format within every file is defined as an 01 entry.Fields within each record are described with a PICTURE (PIC) clause that specifies the size andtype of data.WORKING-STORAGE SECTIONDefines any work areas needed for processing.An end-of-file indicator is specified in the WORKING-STORAGE SECTION; we will refer to thisfield as ARE-THERE-MORE-RECORDS. In our examples, the field called ARE-THEREMORE-RECORDS will contain the value 'YES' when there are input records to process, and avalue of 'NO' when there are no more records.The PROCEDURE DIVISION.Is subdivided into paragraphs or modules.Includes all instructions required to process input and produce output.All instructions are executed in sequence, but PERFORM transfers control to another paragraph; after aparagraph is executed with a PERFORM command, control returns to the next instruction, in sequence,following the PERFORM.The following are typical entries in the PROCEDURE DIVISION:Files are designated as INPUT or OUTPUT and activated in a. an OPEN statement.An initial READ brings the first record into storage.PERFORM paragraph-nameUNTIL ARE-THERE-MORE-RECORDS='NO'temporarily transfers control to some processing module that processes a record and then readsthe next record; this processing routine is repeated until there are no more records.A CLOSE statement deactivates all the files.STOP RUN terminates processing.

Types of DataVariable data are data that vary with each run.Constants and Literals are data that are defined within the program; they are not entered as input to thesystem.A numeric literal is a constant that may be used in the WORKING-STORAGE SECTION orPROCEDURE DIVISION for arithmetic operations.An alphanumeric (nonnumeric) literal is a constant enclosed in quotation marks that neverchanges throughout the execution of the program.A figurative constant is a reserved word with special significance to the compiler (e.g., ZERO orZEROES or ZEROS; SPACE or SPACES).The FILE SECTIONFD EntriesFD a. is coded in Area A.The file-name, which is typically coded in Area B, must be the same name as that used in theSELECT statement.All clauses in the FD are optional.After any clauses have been specified, a single period ends the FD.Record Description EntriesRecord-names are coded on the 01 level.Field-names are coded on levels 0249. Usually, 05, 10, 15, and so on are used to allow forinsertions if they become necessary.Level 01 is coded in Area A. All other levels are coded in Area B for clarity, although Area A can beused.Fields with higher level numbers are considered subordinate to, or contained within, fields withlower-level numbers. Usually, subordinate fields are indented for the sake of clarity.Group fields are further divided; elementary fields are not.Only elementary fields have PICTURE or PIC clauses to describe the data:X alphanumericA alphabetic (not recommended)9 numericV implied decimal position (used only with numeric fields)A period must follow a PICTURE clause in an elementary field; a period directly follows a groupfield name.Fields must be defined in the DATA DIVISION in the same sequence as they appear in the recordbeing described.The WORKING-STORAGE SECTION1. Used for storing intermediate results, counters, and end-of-file indicators.2. VALUE clauses may be used to initialize fields.

Printing OutputThe BEFORE or AFTER ADVANCING option should be used with each WRITE instruction to indicatethe spacing of the form. BEFORE ADVANCING 1, 2, or 3 lines, for example, will cause zero, one, or twoblank lines, respectively, to appear before the record is written.Records defining all printed output, including heading and detail lines, should be established inWORKING-STORAGE so that VALUE clauses can be used. These records must be moved to the printarea defined in the FILE SECTION. A WRITE . . . FROM instruction may be used in place of a MOVEand a WRITE to print these lines.Use a printer spacing chart to determine the print positions to be used.Prior to a detail record being printed, a test for the end of the page should be performed. If the desirednumber of lines has been printed, the heading routine should be executed that will print headings on anew page.The appropriate editing symbols should be specified in the PIC clauses of report-items within the detailrecord.6. Characters that may be used in a PICTURE clause are as follows:

Rules for numeric MOVE, when sending and receiving fields are both numeric:a. Integer portion.b. Movement is from right to left.c. Nonfilled high-order positions are replaced with zeros.d. Truncation of high-order digits occurs if the receiving field is not large enough to hold the results.e. Decimal portion.f. Decimal alignment is maintained.g. Movement is from left to right, beginning at the decimal point.h. Nonfilled low-order positions are replaced with zeros.Rules for alphanumeric MOVE, when receiving field is alphanumeric (non-numeric):a. Movement is from left to right.b. Low-order nonfilled positions are replaced with spaces.c. Truncation of low-order characters occurs if the receiving field is not large enough to hold theresults.The format of the receiving field determines the type of MOVE operation that is performedeithernumeric or alphanumeric (nonnumeric).A field-name may be qualified by using OF or IN with the name of a record or group item of which thefield is a part.

The ADD, SUBTRACT, MULTIPLY, and DIVIDE verbs all have a GIVING format. With this GIVINGformat, the receiving field is not part of the arithmetic and can be a report-item.A COMPUTE statement can be used for performing multiplication, division, addition, subtraction,exponentiation, or a combination of these.The COMPUTE can save coding if used in place of the ADD, SUBTRACT, MULTIPLY, 3. and DIVIDE verbs.The COMPUTE statement can use the following operations.+ Addition- Subtraction* Multiplication./ Division** ExponentiationIf several operations are performed with one COMPUTE statement, the order of evaluation isa. ( ) (parentheses).b. ** (exponentiation).c. * or / (in sequence left to right).d. + or -(in sequence left to right).The ROUNDED, ON SIZE ERROR, and NOT ON SIZE ERROR options can be used with the fourarithmetic verbs and the COMPUTE.When using ON SIZE ERROR or NOT ON SIZE ERROR with any arithmetic verb, use a scopeterminator (END-ADD, END-SUBTRACT, END-MULTIPLY, END-DIVIDE, END-COMPUTE).

The simple PERFORM statementCauses execution of the instructions at the a. named paragraph(s).Transfers control to the statement directly following the PERFORM after execution of the namedparagraph(s)..With the PERFORM . . . UNTIL statementThe identifier(s) used in the UNTIL clause must be altered within the paragraph(s) beingperformed; otherwise, the paragraphs will be performed indefinitely.If the condition in the UNTIL clause is met at the time of execution, then the named paragraph(s)will not be executed at all.The WITH TEST AFTER clause can be used with the PERFORM statement to test the condition after theparagraph has been executed at least once.With the PERFORM . . . TIMES statement, a numeric identifier, integer, or arithmetic expression canprecede the word TIMES.For the PERFORM . . . VARYING statementThe counter or loop control field must be defined in the DATA DIVISION, typically in WORKINGSTORAGE.An initial VALUE for the loop control field is not required.The PERFORM . . . VARYING automaticallyInitializes the counter with the value specified in the FROM clause.Tests the counter for the condition specified in the UNTIL clause.Continues with the statement directly following the PERFORM if the condition specified in theUNTIL clause is satisfied.Executes the named paragraph(s) if the condition specified in the UNTIL clause is not met.After execution of the named paragraph(s), increases (or decreases) the counter by the value ofthe integer or identifier specified in the VARYING clause.Additional considerationsThe THRU option can be included with all versions of the PERFORM.PERFORM statements within PERFORM statements are permissible. These are called nestedPERFORMS.In-line PERFORMS are permitted with all PERFORM options; with in-line PERFORMS it is not.

The COPY statementIs used to copy entries stored in a source member 1. to a user program.Can copy ENVIRONMENT, DATA, and PROCEDURE DIVISION entries.Most often is used for copying standard file and record description entries or modules to be used in thePROCEDURE DIVISION.The format is: COPY source-member {OF/IN} library-name-file-name.The CALL statement1. Is used to call or reference entire programs stored in a library.The main program is referred to as the calling program; the program being accessed from the mainprogram is referred to as the called program or subprogram.Two types of calls include dynamic program call and static procedure call.To pass data from the called program to the calling program:The CALL statement includes a USING clause that lists the names of the fields in the callingprogram that are passed to the called program and fields that will be passed back from the calledprogram.The PROCEDURE DIVISION statement of the called program also includes a USING clause toindicate identifiers specified in this called program that will correspond to identifiers in thecalling program.Identifiers in the called and calling program may be the same or they may be different.The called program must have a LINKAGE SECTION in which fields to be passed to and from thecalling program are defined. This is the last section of the DATA DIVISION.The called program must end with an EXIT PROGRAM statement.

Three types of report that can be produced areDetail or transaction reports.Exception reports.Summary or group reports.a control break program, all input records must be in sequence by minor control fields withinintermediate control fields within major control fields. If the records are not already in this order, thenthe file must be sequenced into the required order before it can be processed.Files required for control-break processing can be reorganized into the proper sequence by using logicalfiles.Control break routines are executed as follows:a. Higher-level breaks force lower-level breaks.b. Appropriate control total line is printed.c. Appropriate control field is initialized.d. Appropriate control total field is initialized.

Keyed File Processing1. ENVIRONMENT DIVISIONa. SELECT clause specifies ORGANIZATION IS INDEXEDACCESS IS:1. RANDOM for non-sequential updates, inquiries, and so on.2. SEQUENTIAL as the default setting when accessing a file sequentially.3. DYNAMIC for accessing a keyed file both sequentially and randomly in the same program.RECORD KEY is the key field in each keyed record that is used for establishing the index and foraccessing records.PROCEDURE DIVISIONReading sequentially from a keyed fileSame as all sequential processing.Use READ . . . AT END.Reading randomly from a keyed file1. ACCESS IS RANDOM (or DYNAMIC if sequential access is also desired) in SELECT clause.2. If a keyed record is to be updated, OPEN I-O is used.3. Transaction key is moved to the RECORD KEY and READ . . . INVALID KEY is used.4. To write updated records back onto the disk, REWRITE is used.5. Use START for positioning a keyed file at some point other than the beginning.

Another permissible test that may be used with any arithmetic operation is NOT ON SIZE ERROR.

The LIKE clause allows the attributes of a data item to be defined by copying them from a previously defineddata item. Two attributes include the PICTURE and USAGE clauses of the existing item.

There are times when you might want to execute a series of steps only if a certain condition does not exist. The COBOL expression CONTINUE enables you (1) to avoid performing any operation if a condition exists, and (2) to execute instructions only if the ELSE condition is met.CONTINUE must be the only clause following a condition, since it indicates that no action is to be performed.

One function of the SET statement is to set a condition-name to TRUE. Consider the following SET statement. SET NO-MORE-RECORDS TO TRUE.

PERFORM 220-PARAGRAPH-2 THRU 230-PARAGRAPH-3 PERFORM . . . UNTIL is a type of iteration used for programming a loop, which is a sequence of steps that is executed repeatedly until a condition exists.

When a series of operations is to be executed a predefined number of times, the PERFORM . . . TIMESstatement is used.PERFORM 220-WRITE-LABEL-RTN 5 TIMES

Iteration using a PERFORM . . . UNTIL is the same as a Do/While in other languages. With this structure, a test for the condition is made first, even before the sequence of steps within the PERFORM is executed. If the condition is not met initially, the instructions are PERFORMed once. If the condition is met initially, the instructions being PERFORMed are not executed at all.Most languages also have an iteration structure (Do/Until) that executes the instructions to be PERFORMedeven before the test is made. This ensures that the sequence of steps being performed is executed at least once.A PERFORM . . . UNTIL can be made equivalent to a DO . . . UNTIL with the use of a WITH TEST AFTERclause. This means we instruct the program to test for the condition in the PERFORM . . . UNTIL after the instructions are executed.

To connect comparisons together, simply leave a space in position 17 of the DDS. Then, all the comparisons that are connected in this fashion must be met before the record is selected or omitted. That is, the comparisons are ANDed together.

JDFTVAL: File-level keyword that indicates that the system will provide default values for secondary file fieldswhen a secondary record does not exist. Unless the DFT keyword is used in the physical file, the default valueis blanks for character fields and zeros for numeric fields. The JDFTVAL keyword has no parameters.JDUPSEQ: Record-level keyword that indicates a secondary file field-name used to sort records when duplicatejoin field values exist. By default, the sort order is ascending; descending can be specified. The format isJDUPSEQ(sequencing-field-name *DESCEND).JFILE: Record level keyword that identifies the files to be included in the join. The format isJFILE(library-name/physical-file-name {..32}).The first file-name specified is considered the primary file.JFLD: Join-level keyword that indicates which fields are used to join the files. At least one JFLD keyword mustbe used on each join specification. The format isJFLD(from-file-field-name to-file-field-name).JOIN: Join-level keyword that identifies which pair of files is joined. The format isJOIN(from-file to-file).JREF: Field-level keyword used when the same field-name is used among the joined files and you need toindicate the file from which the field value is to be used. The format isJREF(file-name or relative-file-number).

ENVIRONMENT DIVISIONa. SELECT clause specifies ORGANIZATION IS INDEXEDACCESS IS:1. RANDOM for non-sequential updates, inquiries, and so on.2. SEQUENTIAL as the default setting when accessing a file sequentially.3. DYNAMIC for accessing a keyed file both sequentially and randomly in the same program.RECORD KEY is the key field in each keyed record that is used for establishing the index and foraccessing records.

OCCURS clausesAre used in the DATA DIVISION to specify the repeated occurrence of items with the sameformat.May be written b. on levels 0249.May specify an elementary or group item.Use an OCCURS clause to define arrays and tables.a. Array: an area used for storing data or totals.b. Table: a set of fields used in a table look-up.Use of the SEARCH statement for table-handling:a. The identifier used with the SEARCH verb is the one specified on the OCCURS level.The AT END clause specifies what is to be done if the table has been searched and the requiredcondition has not been met.The WHEN clause indicates what to do when the condition is met.When using a SEARCH statement, table entries are specified with the use of an index, rather thana subscript.The index is defined along with the OCCURS. For example: 01 UNIT-PRICE-TABLE.05 STORED-ENTRIES OCCURS 500 TIMESINDEXED BY SUB.An index cannot be modified with a MOVE, ADD, or SUBTRACT statement. Use a SET statementwhen altering the contents of an index, or use the PERFORM . . . VARYING.The index is SET to 1 to 1 before using a SEARCH.Use a PERFORM . . . VARYING to load the table.The SEARCH ALL statementuses and limitations.a. Used to perform a binary search.b. Can test only an equal condition.If using a compound condition: (a) each part can test only an equal condition and (b) only ANDsare permitted.Only one WHEN clause can be used.The ASCENDING or DESCENDING KEY is specified along with the OCCURS and INDEXED BYclauses of a table entry.

Multiple-level OCCURS.Multiple-level OCCURS may be used for an 1. array or a table.The lowest-level OCCURS data-name or an item subordinate to it is used to access an entry in the arrayor the table.If we use a SEARCH for accessing a multiple-level table, INDEXED BY must be used on all OCCURSlevels.The identifier used with the SEARCH statement should typically be the one on the lowest OCCURSlevel. Only the index on the same level as the OCCURS level will be incremented by the SEARCH. Thatis, SEARCH XXX, for example, will vary only the index specified with XXX. Consider the following.05 MONTHLY-AMOUNT OCCURS 10 TIMES INDEXED BY SUB1. SUB1 is the only index incremented in the search regardless of whether MONTHLY-AMOUNT issubordinate to an OCCURS or contains another level of OCCURS.