DT266/2 Information Systems COBOL Revision. Chapters 1 & 2 Hutty & Spence Divisions of a Cobol...

31
DT266/2 Information Systems COBOL Revision

Transcript of DT266/2 Information Systems COBOL Revision. Chapters 1 & 2 Hutty & Spence Divisions of a Cobol...

Page 1: DT266/2 Information Systems COBOL Revision. Chapters 1 & 2 Hutty & Spence Divisions of a Cobol Program Identification Division Program-ID. Environment.

DT266/2 Information Systems COBOL

Revision

Page 2: DT266/2 Information Systems COBOL Revision. Chapters 1 & 2 Hutty & Spence Divisions of a Cobol Program Identification Division Program-ID. Environment.

Chapters 1 & 2 Hutty & Spence

Divisions of a Cobol Program

Identification Division Program-ID.

Environment Division Configuration

Section Special-Names

Input-Output Section File-Control

Data Division File Section Working-Storage

Section Screen Section Report Section Linkage Section

Procedure Division

Page 3: DT266/2 Information Systems COBOL Revision. Chapters 1 & 2 Hutty & Spence Divisions of a Cobol Program Identification Division Program-ID. Environment.

Basic Statements

MoveAddSubtractMultiplyGivingDividePerform

Page 4: DT266/2 Information Systems COBOL Revision. Chapters 1 & 2 Hutty & Spence Divisions of a Cobol Program Identification Division Program-ID. Environment.

Paragraphs

Paragraph Grouping of statements that can be placed

anywhere within a program Execute using PERFORM statement

PERFORM para-name. Use for maintainability and readability When a paragraph is executed, control

returns to the statement after the perform

Displaced paragraph Occurs after the stop run and performed

from elsewhere in the program

Page 5: DT266/2 Information Systems COBOL Revision. Chapters 1 & 2 Hutty & Spence Divisions of a Cobol Program Identification Division Program-ID. Environment.

Chapter 2 Hutty & Spence

Data & ConditionsConditions

<,=,>, and, or, not, less than, …..

Data Pictures Computational and display data types PIC Types 9, A, X, Z, $, etc Decimals V, . Signs +. -, CR, DB Commas, currency

MOVE Rules for different data types

Page 6: DT266/2 Information Systems COBOL Revision. Chapters 1 & 2 Hutty & Spence Divisions of a Cobol Program Identification Division Program-ID. Environment.

Chapter 2 Hutty & Spence

LEVELS

Group Item 01 can contain

subordinate items which must have higher number

Level 88 Condition Name Specifies that a

condition is true when value of data item changes to that defined in the associated picture clause

01 WS-MONTH.88 VALID-MONTHVALUE 1 THROUGH 12.88 FEB VALUE 2.88 30-DAY VALUE 4,6,9,11.

EVALUATE TRUEWHEN 30-DAYWHEN FEBWHEN OTHER

END-EVALUATE

Page 7: DT266/2 Information Systems COBOL Revision. Chapters 1 & 2 Hutty & Spence Divisions of a Cobol Program Identification Division Program-ID. Environment.

ValidationRange Checks

Level 88s

Selection checks If

Alphabetic checks Special names

Complex checks Combine checks and, or, not

Especially important when working with filesNo record should be written to a file unless the data is validReason for accepting into working-storage not directly into a file

Page 8: DT266/2 Information Systems COBOL Revision. Chapters 1 & 2 Hutty & Spence Divisions of a Cobol Program Identification Division Program-ID. Environment.

Chapter 3, 4, 5, 6 Hutty & Spence

Control Constructs - Selection

IF statementIF …. THENELSEENDIF

EVALUATE statementEVALUATEWHEN A> B PERFORM AWHEN A<B PERFORM BWHEN OTHER PERFORM CEND-EVALUATE

Page 9: DT266/2 Information Systems COBOL Revision. Chapters 1 & 2 Hutty & Spence Divisions of a Cobol Program Identification Division Program-ID. Environment.

Chapter 3, 4, 5, 6 Hutty & Spence

Control Constructs - Iteration

PERFORM statementLEADING EDGE While Loop Test Before

TRAILING EDGE Repeat Loop Test After

VARYING For Loop

Page 10: DT266/2 Information Systems COBOL Revision. Chapters 1 & 2 Hutty & Spence Divisions of a Cobol Program Identification Division Program-ID. Environment.

Chapter 3, 4, 5, 6 Hutty & Spence

Control Constructs – Iteration

LEADING EDGEDisplay “Enter Age : “.Perform Enter-Age until AGE-IN > 3 and AGE-IN < 19.

Condition is evaluated at the start of each loop

Execute 0 or more times

TRAILING EDGEPerform Accept-Option WITH TEST AFTER UNTIL WS-OPTION >0 AND WS-OPTION < 6.

Forces code to be executed at least once and then condition is tested

Page 11: DT266/2 Information Systems COBOL Revision. Chapters 1 & 2 Hutty & Spence Divisions of a Cobol Program Identification Division Program-ID. Environment.

Chapter 3, 4, 5, 6 Hutty & Spence

Control Constructs - Iteration

FOR LOOPPerform Display-

Array-Value varying WS-ARRAY-INDEX from 1 by 1

Execute statements a set number of times.

FOR LOOPPerform Display-

Array-Value varying WS-ARRAY-INDEX from 1 by 1 until ARRAY-INDEX > 10

Execute statements a set number of times.

Page 12: DT266/2 Information Systems COBOL Revision. Chapters 1 & 2 Hutty & Spence Divisions of a Cobol Program Identification Division Program-ID. Environment.

Chapter 7 & 8 Hutty & Spence

Arrays

An array is a group item each of its elements belongs to it Each element has the same picture and the

element ‘occurs’ a number of times Subscripting always starts at 1. Arrays are fixed length

Indexing Can be part of the definition Can be controlled by another working

storage variable

Page 13: DT266/2 Information Systems COBOL Revision. Chapters 1 & 2 Hutty & Spence Divisions of a Cobol Program Identification Division Program-ID. Environment.

Chapter 7 & 8 Hutty & Spence

1- D Arrays

Definition 01 months-of-the-year.

03 month-name pic AAA occurs 12 times indexed by month-no.

IndexingSet month-no to 1.Set month-no up by 1.

Referencing Display Month-Name (Month #)

Page 14: DT266/2 Information Systems COBOL Revision. Chapters 1 & 2 Hutty & Spence Divisions of a Cobol Program Identification Division Program-ID. Environment.

Chapter 7 & 8 Hutty & Spence

2-D Arrays

Definition01 box-of-chocolates.

02 layer-of-chocolates occurs 4 times indexed by layer-no.

03 chocolate occurs 20 times pic 99 indexed by choc-no.

Indexing as beforeReferencing Access lowest level

Chocolate (layer #, chocolate #)

Page 15: DT266/2 Information Systems COBOL Revision. Chapters 1 & 2 Hutty & Spence Divisions of a Cobol Program Identification Division Program-ID. Environment.

Chapter 7 & 8 Hutty & Spence

Arrays - InitialisingWithin the procedure division

Move Accept Use perform statement to control

Within working storage section Redefines clause01 Month-Days.

02 Jan-Days pic 99 value 31.02 Feb-Days pic 99 value 28.02 Mar-days pic 99 value 31.02 Dec-days pic 99 value 31.

01 Days-array redefines Month-Days pic 99 occurs 12 times.OR01 Days-of-year pic 9(36 ) value

312831303130313130313031.02 Month-Days redefines Days-of-year pic 99 occurs 12 times.

Page 16: DT266/2 Information Systems COBOL Revision. Chapters 1 & 2 Hutty & Spence Divisions of a Cobol Program Identification Division Program-ID. Environment.

Chapter 11, 12, 13, 14 Hutty & Spence

File Handling

File Introduced in Environment DivisionInput-Output SectionFile-Control

What happens ? Give logical name by which file will be

referred to in program Give physical name and location of file Describe file organisation and method used

to access it

Page 17: DT266/2 Information Systems COBOL Revision. Chapters 1 & 2 Hutty & Spence Divisions of a Cobol Program Identification Division Program-ID. Environment.

Chapter 11, 12, 13, 14 Hutty & Spence

File Handling

Describe the structure of the records in the file Data Division File Section. FD <file name> 01 <record name>

Each file must have a File Descriptor entry in the File SectionRecord must be level 01Do not embed validation in file descriptors

Page 18: DT266/2 Information Systems COBOL Revision. Chapters 1 & 2 Hutty & Spence Divisions of a Cobol Program Identification Division Program-ID. Environment.

Chapter 11, 12, 13, 14 Hutty & Spence

File Organisation

3 Types Sequential

Records stored one after another in order in which they are written

Indexed Sequential Each record is given a key that

determines its position Relative

Page 19: DT266/2 Information Systems COBOL Revision. Chapters 1 & 2 Hutty & Spence Divisions of a Cobol Program Identification Division Program-ID. Environment.

Chapter 11, 12, 13, 14 Hutty & Spence

File Access

Sequential Access Next record in sequence is read/written A file whose organisation is sequential can

only have sequential access

Random Access Record is read/written by specifying its KEY Relative or Indexed Sequential Files

Dynamic Access Combination of the above Relative or Indexed Sequential files

Page 20: DT266/2 Information Systems COBOL Revision. Chapters 1 & 2 Hutty & Spence Divisions of a Cobol Program Identification Division Program-ID. Environment.

Chapter 11, 12, 13, 14 Hutty & Spence

Sequential Files

Writing Open output

<filename> OR Open extend

<filename> Write <recordname> Close <filename>

Reading Open Input

<filename> Read <filename> Close <filename>

Checking for End of File Read <filename>

AT END NOT AR END

End-Read

Use a Level 88Cannot RewriteCan Delete

Page 21: DT266/2 Information Systems COBOL Revision. Chapters 1 & 2 Hutty & Spence Divisions of a Cobol Program Identification Division Program-ID. Environment.

Chapter 11, 12, 13, 14 Hutty & Spence

Indexed Sequential Files

Declaring Organisation Keys (primary &

alternate) Access method

Reading Open Input <filename> OR Open I-O <filename> Read <filename>

Invalid Key Not Invalid Key

End-Read Close <filename>

Positioning using a keySTART FILE-NAME KEY

NOT < KEY-NAMEINVALID KEY

INVALID KEY CLAUSE{NOT INVALID KEY

NOT INVALID KEY CLAUSE}

END-START

Page 22: DT266/2 Information Systems COBOL Revision. Chapters 1 & 2 Hutty & Spence Divisions of a Cobol Program Identification Division Program-ID. Environment.

Chapter 11, 12, 13, 14 Hutty & Spence

Indexed Sequential Files

Writing Open Output

<filename> OR Open extend

<filename> OR Open I-O

<filename> Write <recordname>

Invalid Key Not Invalid Key

End-write Close <filename>

ReWriting Open I-O <filename> Rewrite <recordname>

Invalid Key Not Invalid Key

End-Rewrite Close <filename>

Must have positioned at record first Deleting

Always ask DELETE <filename>

record

Page 23: DT266/2 Information Systems COBOL Revision. Chapters 1 & 2 Hutty & Spence Divisions of a Cobol Program Identification Division Program-ID. Environment.

Chapter 11, 12, 13, 14 Hutty & Spence

Relative Files

Records can be accessed directly, according to the number of the record within the fileEach record has a key, which is it’s relative address from the start of the fileThis is suitable for only a small number of records

Page 24: DT266/2 Information Systems COBOL Revision. Chapters 1 & 2 Hutty & Spence Divisions of a Cobol Program Identification Division Program-ID. Environment.

Chapter 17 Hutty & Spense

Report WriterReport Section

Reports written to external file Declare file to program File Definition

FD <filename>Report is <reportname>

RD Report Definition Must Define

PAGE LIMIT No. of lines to print on each page

HEADING Line at which heading will appear

FIRST DETAIL Line at which first detail will appear

LAST DETAIL Line at which last detail will appear

FOOTING Line at which footing will appear

Page 25: DT266/2 Information Systems COBOL Revision. Chapters 1 & 2 Hutty & Spence Divisions of a Cobol Program Identification Division Program-ID. Environment.

Chapter 17 Hutty & Spense

Report Writer

Each RD entry consists of one or more 01 level-number entries. Each 01 level-number entry identifies a report group A unit consisting of one or more print lines and is of a particular type.

Report Heading - Printed once at start of the report Report Footing - Printed once at the end of the report Page Heading - Printed at the start of every page Page Footing - Printed at the end of every page Detail - Basic report detail, the body of the report Control Heading - Printed each time some particular

value changes Control Footing - Printed each time some particular value

changes

Page 26: DT266/2 Information Systems COBOL Revision. Chapters 1 & 2 Hutty & Spence Divisions of a Cobol Program Identification Division Program-ID. Environment.

Chapter 17 Hutty & Spense

Report Writer

Defining Report Lines Must identify the type of line Have sub-ordinate items Must identify the line on which it is to be

printed in the report For each section of the line must identify

where the data to be included is positioned I.e. column

Must identify the source of the data to be included on that line

Page 27: DT266/2 Information Systems COBOL Revision. Chapters 1 & 2 Hutty & Spence Divisions of a Cobol Program Identification Division Program-ID. Environment.

Chapter 17 Hutty & Spense

Report WriterControlling Reports Define control in the report definition

RD STOCK-REPORT CONTROLS ARE FINAL, DEPARTMENT

Data to control report must appear in a detail or control line

FINAL is a report writer specific word Ensures last control footing of the report will be output

When detail line first generated report writer saves values of control data itemsEach time a detail line is generated report item checks to see if values have changed beginning with the first listed in the RDIf a control data item changes it also cause all other control breaks that are sub-ordinate to it

Page 28: DT266/2 Information Systems COBOL Revision. Chapters 1 & 2 Hutty & Spence Divisions of a Cobol Program Identification Division Program-ID. Environment.

Chapter 17 Hutty & Spense

Report Writer

Creating the report in Procedure Division OPEN File INITIATE the report GENERATE the lines of the report TERMINATE the report CLOSE File

Page 29: DT266/2 Information Systems COBOL Revision. Chapters 1 & 2 Hutty & Spence Divisions of a Cobol Program Identification Division Program-ID. Environment.

Chapter 15 Hutty & Spence

SortingSimple Sorting

File to be sorted is copied to special work file Records in this work file are sorted into the requested order Sorted work file is copied to an output file Declare Work-file in Environment DivisionSelect SortFile assign to dummy. Define structure of workfile in File Section

SD SORT-FILE.

01 S-CAR-RECORD.

Sort file in Procedure Division

SORT <WORKFILE>ON {ASCENDING/DESCENDING }KEY {IDENTIFIER}USING <INFILE>GIVING <OUTFILE>

Page 30: DT266/2 Information Systems COBOL Revision. Chapters 1 & 2 Hutty & Spence Divisions of a Cobol Program Identification Division Program-ID. Environment.

Chapter 15 Hutty & Spence

SortingSelective SortingSort as before, but Cut down on the fields in the output file Or include other fields in the output file Change the procedure division to sort using an input

procedure and output procedure instead of input and output files

Define 2 more sections, one for the input procedure and one for the output procedure

Release records into the sort and Return them from the sort

Keywords USING and GIVING not required instead have

INPUT PROCEDURE IS <paragraph>OUTPUT PROCEDURE IS <paragraph>

Page 31: DT266/2 Information Systems COBOL Revision. Chapters 1 & 2 Hutty & Spence Divisions of a Cobol Program Identification Division Program-ID. Environment.

Chapter 15 Hutty & Spence

Sorting

Write record to sort file

Read record from sort file