DT266/2 Information Systems COBOL Revision
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
Basic Statements Move Add Subtract Multiply Giving Divide Perform
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
Chapter 2 Hutty & Spence Data & Conditions Conditions, 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
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-MONTH VALUE 1 THROUGH FEBVALUE DAYVALUE 4,6,9,11. EVALUATE TRUE WHEN 30-DAY WHEN FEB WHEN OTHER END-EVALUATE
Validation Range Checks Level 88s Selection checks If Alphabetic checks Special names Complex checks Combine checks and, or, not Especially important when working with files No record should be written to a file unless the data is valid Reason for accepting into working-storage not directly into a file
Chapter 3, 4, 5, 6 Hutty & Spence Control Constructs - Selection IF statement IF …. THEN ELSE ENDIF EVALUATE statement EVALUATE WHEN A> B PERFORM A WHEN A<B PERFORM B WHEN OTHER PERFORM C END-EVALUATE
Chapter 3, 4, 5, 6 Hutty & Spence Control Constructs - Iteration PERFORM statement LEADING EDGE While Loop Test Before TRAILING EDGE Repeat Loop Test After VARYING For Loop
Chapter 3, 4, 5, 6 Hutty & Spence Control Constructs – Iteration LEADING EDGE Display “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 EDGE Perform 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
Chapter 3, 4, 5, 6 Hutty & Spence Control Constructs - Iteration FOR LOOP Perform Display-Array- Value varying WS- ARRAY-INDEX from 1 by 1 Execute statements a set number of times. FOR LOOP Perform Display-Array- Value varying WS- ARRAY-INDEX from 1 by 1 until ARRAY- INDEX > 10 Execute statements a set number of times.
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
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. Indexing Set month-no to 1. Set month-no up by 1. Referencing Display Month-Name (Month #)
Chapter 7 & 8 Hutty & Spence 2-D Arrays Definition 01 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 before Referencing Access lowest level Chocolate (layer #, chocolate #)
Chapter 7 & 8 Hutty & Spence Arrays - Initialising Within the procedure division Move Accept Use perform statement to control Within working storage section Redefines clause 01 Month-Days. 02 Jan-Dayspic 99value Feb-Dayspic 99value Mar-dayspic 99value Dec-dayspic 99value Days-arrayredefines Month-Days pic 99 occurs 12 times. OR 01 Days-of-yearpic 9(36) value Month-Daysredefines Days-of-year pic 99 occurs 12 times.
Chapter 11, 12, 13, 14 Hutty & Spence File Handling File Introduced in Environment Division Input-Output Section File-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
Chapter 11, 12, 13, 14 Hutty & Spence File Handling Describe the structure of the records in the file Data Division File Section. FD 01 Each file must have a File Descriptor entry in the File Section Record must be level 01 Do not embed validation in file descriptors
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
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
Chapter 11, 12, 13, 14 Hutty & Spence Sequential Files Writing Open output OR Open extend Write Close Reading Open Input Read Close Checking for End of File Read AT END NOT AR END End-Read Use a Level 88 Cannot Rewrite Can Delete
Chapter 11, 12, 13, 14 Hutty & Spence Indexed Sequential Files Declaring Organisation Keys (primary & alternate) Access method Reading Open Input OR Open I-O Read Invalid Key Not Invalid Key End-Read Close Positioning using a key START FILE-NAME KEY NOT < KEY-NAME INVALID KEY INVALID KEY CLAUSE {NOT INVALID KEY NOT INVALID KEY CLAUSE} END-START
Chapter 11, 12, 13, 14 Hutty & Spence Indexed Sequential Files Writing Open Output OR Open extend OR Open I-O Write Invalid Key Not Invalid Key End-write Close ReWriting Open I-O Rewrite Invalid Key Not Invalid Key End-Rewrite Close Must have positioned at record first Deleting Always ask DELETE record
Chapter 11, 12, 13, 14 Hutty & Spence Relative Files Records can be accessed directly, according to the number of the record within the file Each record has a key, which is it’s relative address from the start of the file This is suitable for only a small number of records
Chapter 17 Hutty & Spense Report Writer Report Section Reports written to external file Declare file to program File Definition FD Report is 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
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
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
Chapter 17 Hutty & Spense Report Writer Controlling 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 items Each time a detail line is generated report item checks to see if values have changed beginning with the first listed in the RD If a control data item changes it also cause all other control breaks that are sub-ordinate to it
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
Chapter 15 Hutty & Spence Sorting Simple 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 Division Select SortFile assign to dummy. Define structure of workfile in File Section SD SORT-FILE. 01 S-CAR-RECORD. Sort file in Procedure Division SORT ON {ASCENDING/DESCENDING } KEY {IDENTIFIER} USING GIVING
Chapter 15 Hutty & Spence Sorting Selective Sorting Sort 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 OUTPUT PROCEDURE IS
Chapter 15 Hutty & Spence Sorting Write record to sort file Read record from sort file