1 The Procedure Division Chapter 4. 2 Main Two Sections File Section –Used to define files and record formats –Field names within records Working Storage.

Slides:



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

2-1 Chapter 2.  Coding Requirements of IDENTIFICATION DIVISION  Sections of ENVIRONMENT DIVISION  Assigning Files to Devices in ENVIRONMENT DIVISION.
The IDENTIFICATION and ENVIRONMENT DIVISIONS Chapter 2.
5-1 COBOL for the 21 st Century Nancy Stern Hofstra University Robert A. Stern Nassau Community College James P. Ley University of Wisconsin-Stout (Emeritus)
Programming Logic and Design Fourth Edition, Introductory
Starting Out with C++: Early Objects 5/e © 2006 Pearson Education. All Rights Reserved Starting Out with C++: Early Objects 5 th Edition Chapter 5 Looping.
COBOL for the 21 st Century Stern, Stern, Ley Chapter 1 INTRODUCTION TO STRUCTURED PROGRAM DESIGN IN COBOL.
CHAPTER 6 FILE PROCESSING. 2 Introduction  The most convenient way to process involving large data sets is to store them into a file for later processing.
Pseudocode.
COBOL for the 21st Century
C++ Programming: Program Design Including Data Structures, Fourth Edition Chapter 5: Control Structures II (Repetition)
COMP An Introduction to Computer Programming : University of the West Indies COMP6015 An Introduction to Computer Programming Lecture 02.
Structured COBOL Programming, Stern & Stern, 9th edition
4-1 Coding Complete COBOL Programs: The PROCEDURE DIVISION Chapter 4.
4-1 COBOL for the 21 st Century Nancy Stern Hofstra University Robert A. Stern Nassau Community College James P. Ley University of Wisconsin-Stout (Emeritus)
12-1 Structured COBOL Programming Nancy Stern Hofstra University Robert A. Stern Nassau Community College James P. Ley University of Wisconsin-Stout John.
4-1 COBOL for the 21 st Century Nancy Stern Hofstra University Robert A. Stern Nassau Community College James P. Ley University of Wisconsin-Stout (Emeritus)
1 Chapter 4. To familiarize you with methods used to 1. Access input and output files 2. Read data from an input file 3. Perform simple move operations.
Chapter To familiarize you with  Why COBOL is a popular business-oriented language.  Programming practices and techniques  History of COBOL.
Outlines Chapter 3 –Chapter 3 – Loops & Revision –Loops while do … while – revision 1.
Introduction to COBOL. COBOL  COBOL is an acronym which stands for Common Business Oriented Language.  The name indicates the target area of COBOL applications.
History COBOL (Common Business Oriented Language) was one of the earliest high-level programming languages. COBOL was first proposed in 1959 by the Conference.
Designing and Debugging Batch and Interactive COBOL Programs Chapter 5.
Lecture 31 Numeric Edited Alphabetic (A) AlphaNumeric (X) Numeric (9, V, S) Numeric Edited (9, Z, comma, decimal point, minus sign) –Z = zero suppressed.
The DATA DIVISION Chapter 3. COBOL Data Organization Field - group of characters forming a meaningful unit or basic fact –Characters in a name or digits.
3-1 Chapter 3. To familiarize you with  Ways in which data is organized in COBOL  Rules for forming data-names  Defining input and output files in.
1 Interactive vs Batch Programs Cobol suited for developing both types of programs Interactive programs Accept input data from keyboard Input data processed.
Printing on power systems Program/ Command Data Report Layout (Printer File) Job Output Queue *FILE Spooled File.
Chapter 7 File I/O 1. File, Record & Field 2 The file is just a chunk of disk space set aside for data and given a name. The computer has no idea what.
Indexed and Relative File Processing
Statement Syntax1 THE SELECT STATEMENT Purpose: designates a file and points to its physical location Syntax Definition : SELECT file-name-1 ASSIGN TO.
CPSC3111/CISM3111 COBOL Structured COBOL Programming Text: murach’s structured COBOL Authors: Murach, Prince, Menendez.
Chapter 2 Pseudocode. Objectives To introduce common words, keywords and meaningful names when writing pseudocode To define the three basic control structures.
Pseudocode. Simple Program Design, Fourth Edition Chapter 2 2 Objectives In this chapter you will be able to: Introduce common words, keywords, and meaningful.
9-1 COBOL for the 21 st Century Nancy Stern Hofstra University Robert A. Stern Nassau Community College James P. Ley University of Wisconsin-Stout (Emeritus)
9-1 Iteration: Beyond the Basic PERFORM Chapter 9.
Structured COBOL Programming Nancy Stern Hofstra University Robert A. Stern Nassau Community College James P. Ley University of Wisconsin-Stout.
1 The Data Division. 2 Main Two Sections File Section –Used to define files and record formats –Field names within records Working Storage Section –All.
1 IDENTIFICATION and ENVIRONMENT DIVISIONS. 2 Objectives Basic Structure of COBOL Programs General Coding and Formatting Rules Identification and the.
5-1 Structured COBOL Programming Nancy Stern Hofstra University Robert A. Stern Nassau Community College James P. Ley University of Wisconsin-Stout John.
Structured COBOL Programming, Stern & Stern, 9th Edition CHAPTER 2 Cobol Language Fundamentals.
1 Chapter 5 – The Procedure Division File handling statements –OPEN statement Initiates processing for a file Input Output Each file opened must have been.
1 Chapter 9. To familiarize you with  Simple PERFORM  How PERFORM statements are used for iteration  Options available with PERFORM 2.
PowerPoint Presentation: Richard H. Baum, Ph.D. DeVry Institute of Technology 9th Edition Structured COBOL Programming Nancy Stern Hofstra University Robert.
1.  Introduction  The Benefits of the Report Writer Module ◦ For Detail and Summary Printing ◦ For Control Break Processing ◦ For Printing Headings.
COBOL for the 21 st Century Nancy Stern Hofstra University Robert A. Stern Nassau Community College James P. Ley University of Wisconsin-Stout (Emeritus)
C++ Programming: From Problem Analysis to Program Design, Fourth Edition Chapter 5: Control Structures II (Repetition)
Analysis of SAMPLE1.CBL Please check speaker notes for additional information!
Alternate Version of STARTING OUT WITH C++ 4 th Edition Chapter 5 Looping.
11- 1 Chapter 11.  Avoiding Logic Errors by Validating Input  What to Do If Input Errors Occur  Global Considerations in COBOL  When Data Should Be.
Chapter 4 PROCEDURE DIVISION. Paragraphs PROCEDURE DIVISION divided into paragraphs Each is independent module or routine Made up of series of instructions.
The PERFORM. The PERFORM Verb  Iteration is an important programming construct. We use iteration when we need to repeat the same instructions over and.
Week 2/3 - 2nd Lecture Intro to COBOL Programming Defining Files and Processing Data.
371 Structured COBOL Programming Nancy Stern Hofstra University Robert A. Stern Nassau Community College James P. Ley University of Wisconsin-Stout John.
Array Applications. Objectives Design an algorithm to load values into a table. Design an algorithm that searches a table using a sequential search. Design.
OPERATORS IN C CHAPTER 3. Expressions can be built up from literals, variables and operators. The operators define how the variables and literals in the.
Sorting in COBOL M. M. Pickard.
Designing and Debugging Batch and Interactive COBOL Programs
Any Questions?.
Chapter 3 The DATA DIVISION.
An Introduction to Structured Program Design in COBOL
Structured COBOL Programming
Chapter 14 Sorting and Merging.
Iteration: Beyond the Basic PERFORM
Iteration: Beyond the Basic PERFORM
Coding Concepts (Basics)
Agenda Collating sequence / Sorting data
Computing in COBOL: The Arithmetic Verbs and Intrinsic Functions
CHAPTER 17 The Report Writer Module
Chapter 5: Control Structures II (Repetition)
Presentation transcript:

1 The Procedure Division Chapter 4

2 Main Two Sections File Section –Used to define files and record formats –Field names within records Working Storage Section –All accumulators, counters, subscripts, flags, tables, etc. –All constant records, such as print header records and other constant or near-constant records. But first:

3 PROCEDURE DIVISION. Contains all the instructions that the computer will execute. Other divisions: identification, environment, data (all i/o areas (file section) and Other data areas (working-storage section) remaining chapters - mostly procedure division... FORMAT OF PROCEDURE DIVISION.. Procedure division divided into paragraphs (modules/routines/functions). Designed to perform a specific function. Set of operations ===> one function.. A area = paragraph names only. all instructions - b area. RULES FOR PARAGRAPH NAMES:. Must be unique names. Should be descriptive. See our "standards" <<<======yes yes!!. Numbering of modules - four digits

4 e.g initialize; 2000-read,... From structured design: Statements and sentences:. Verbs start statements.. Statements followed by period = sentence. Paragraphs consist of sentences Sequences of instructions. Statements are executed in order (sequence) unless a "transfer of control" type statement is executed, such as a perform, goto. Well-written programs - written "top-down". Code main module first.. Lower level modules next.. Progress to lower levels (more detailed)

5 STRUCTURE OF PROCEDURE DIVISION: PROCEDURE DIVISION MAIN INITIALIZE-TIME-DATE INITIALIZE-READS..... ETC. PARAGRAPH NAMES COINCIDE WITH MODULES IN STRUCTURE CHART MAIN + FOUR SUBORDINATE FUNCTIONS ==> FIVE PARAGRAPHS.

6 PROCEDURE DIVISION STATEMENTS - MAIN MODULE OPEN STATEMENT SYNTAX: OPEN INPUT filename1… OUTPUT. UPPERCASE = COBOL RESERVED WORD. UNDERLINE = REQUIRED { } MUST SELECT SOMETHING INSIDE... MEANS ANY NUMBER OF PRECEDING ITEMS. MUST SAY: OPEN “INPUT” OR “OUTPUT” EACH MUST BE FOLLOWED BY ANY NUMBER OF FILE NAMES... (BUT AT LEAST ONE...) CAN SAY OPEN INPUT FILENAME-1, FILENAME-2. OR OPEN INPUT FILENAME-1. OPEN INPUT FILENAME-2.

7 IF OPEN INPUT WITH LABELS (TYPICALLY TAPES OR DISKS) THE HEADER LABEL (IN FRONT OF FILE) IS CHECKED TO SEE IF THIS IS THE CORRECT FILE. IF OPEN OUTPUT WITH LABELS DEVICE IS ACCESSED AND HEADER LABEL IS WRITTEN TO THE FRONT OF THE FILE TO BE CREATED. THEREFORE, LABELS ARE CREATED OR CHECKED. A FILE MUST BE (OPENED) BEFORE IT CAN BE PROCESSED!. EVERY FILE NAMED IN A SELECT STATEMENT WILL HAVE AN FD. THIS IMPLIES THAT EACH FILE WILL BE OPENED FOR EITHER INPUT OR OUTPUT. OPEN DESIGNATES THE FILE AS “INPUT” OR “OUTPUT” OPEN CAUSES THE DEVICE (TAPE OR DISK) TO BE ACCESSED TO FIND AND LOCATE FILE.

8 The Read Statement Transfers data (a record) from input device to the input storage area. (The 01 - process area) Format:(older versions of Cobol) READ filename AT END STATEMENT(S). OR READ filename(COBOL 85) AT END STATEMENT STATEMENT... END-READ. Fourth time we have seen the filename. Transfers one record into the process area: the 01. Read also checks length of record inputted against record description. The At End ==> Did I hit EOF or unsuccessful read? If so,do what follows the at end entry:

9 In COBOL-85, we have a newer READ statement: READ filename AT END STATEMENTS [ NOT AT END STATEMENTS] [END-READ] EITHER MUST END IN PERIOD OR END-READ. EXAMPLE: Here are two READ statements…. 1. READ SALES-FILE AT END PERFORM 2000-BEGIN-PROCESS PERFORM 2100-CONTINUE. < (period) OR (BOOK [ BUT I CHANGED EOF FLAG....] 2. IF AMT = ZERO READ IN-FILE AT END MOVE 1 TO F-EOF END-READ END-IF NOTE THE SCOPE TERMINATORS. GIVEN THE CHOICE, USE OF THE SCOPE TERMINATORS IS STRONGLY ENCOURAGED.

10 THE PERFORM... UNTIL FORMAT: (THE ‘TRANSFER OF CONTROL’ PERFORM:) PERFORM procedure-name-1 UNTIL condition OR COBOL 85: (THE ‘IN-LINE’ PERFORM) PERFORM UNTIL condition STATEMENTS END-PERFORM The Perform is critical for implementing structured design and programming techniques FIRST FORMAT:. The Perform is thee mechanism by which control is transferred from a higher level module to a lower level module.. Perform transfers control to a procedure and repeatedly executes the procedure (module, paragraph...) ‘until’ the cited condition becomes true.. Then, control returns to the next sequential instruction following the perform. (We will spend considerable time on this verb.)

11 THE PERFORM... UNTIL Example: (Traditional Perform) 100-SOME-PARAGRAPH. … PERFORM 200-CALCULATE-TOTALS UNTIL X > 25 … 200-CALCULATE-TOTALS. 300-DO-MORE. EXPLAIN…

12 “IN-LINE PERFORM” (COBOL 85 AND 2002 VERSIONS) PERFORM UNTIL condition.... END-PERFORM. HERE, USE OF THE SCOPE TERMINATOR IS ESSENTIAL. EXAMPLE: MOVE INPUTFIELD-A TO OUTPUTFIELD-B PERFORM UNTIL F-EOF = 1 READ EMPLOYEE-FILE AT END... NOT AT END... END-READ MOVE fields... WRITE some printline... END-PERFORM. Can readily see the “scope” of the Perform via the scope terminators. Called “in line” Perform because there is no explicit branching to a paragraph and returning.

13 THE NOTION OF THE "PRETEST" AND POST-TEST": Consider: PERFORM 2000-CALCULATE UNTIL EOF = 1. Condition: (EOF = 1) is tested before loop is ever executed. (Pretest). Then, after each iteration, the condition is tested repeatedly. (Post-test) Here, EOF is assumed to be set initially to 0 (or at least not 1) 05 F-EOF PIC 9 VALUE 0. OR 05 F-EOF PIC XXX VALUE "YES".

14 More on Pretest and Post-test Let's consider the notions of 1.. THE PERFORM... UNTIL AND THE READ STATEMENT (WITH BAD CODING APPROACH... ) 2. THE PERFORM... UNTIL AND THE READ STATEMENT (GOOD CODING TECHNIQUE) Poorly-written code: 0000-MAIN-MODULE. OPEN INPUT.... PERFORM 2000-CALC-ROUTINE UNTIL EOF= CALC-ROUTINE. READ INPUT-FILE AT END MOVE 1 TO F-EOF END-READ MOVE... COMPUTE…. Are there potential problems?? 2100-NEXT-PARA. Discuss pre-test and post-test…

15 BETTER WAY TO GO SCOPE TERMINATORS; 2..PRIMING READ 0000-MAIN-MODULE. OPEN INPUT READ INPUT-FILE <---- PRIMING READ (ONE TIME) AT END MOVE 1 TO F-EOF END-READ PERFORM 2000-CALC-ROUTINE <--- PRETEST; POST TEST UNTIL EOF=1 END-PERFORM <--- NICE SCOPE TERMINATOR PERFORM 4000-SUMMARY-ROUTINE CLOSE.... STOP RUN CALC-ROUTINE. MOVE READ INPUT-FILE<--- THE "MAIN" READING OF THE FILE AT END MOVE 1 TO F-EOF<---- MERELY SETS A SWITCH END-READ. DOES NOT STOP LOOP ITERATION LAST STATEMENT IN PARAGRAPH 2100-NEXT-PARA.

16 END-OF-JOB PROCESSING The Close and Stop Run statements. Typically have two statements part of every EOJ routine: Close files and Stop Run. CLOSE : Releases files and "deactivates" the devices (Releases devices for other assignments....) Format: Close filename1, filename2,..... Creates trailer labels (and more on old technologies).. No "input" or "output" cited... Use separate Closes if appropriate to close files in different parts of a larger program. STOP RUN. Last executable statement of program. In Cobol 85 and 2002, the Stop Run will close any opened files, but explicitly Close them anyway.

17. THE SIMPLIFIED MOVE. SYNTAX: MOVE identifier-1 TO identifier-2. Moves data from first field to second. First field remains unchanged EXAMPLES: MOVE ER-NAME-IN TO PR-NAME-OUT MOVE ER-AMT-OF-CREDIT-IN TO PR-AMT-OF-CREDIT-OUT Assumption: fields are of same type (size is also a consideration - later...)

18 THE WRITE STATEMENT WE READ FILES AND WRITE RECORDS READ a filename. WRITE a record name THE WRITE SPECIFIES WHICH FORMAT IS TO BE WRITTEN We can have multiple output formats (detail records, column headers, report headers, page trailers, etc. We use the FD name (file name) when we Read We use a record name when we Write.

19 We use the 01 name (record name) when we write. Now: let's look at multiple record format - for output. FD PRINTFILE REPORT-HDR COLUMN-HDR DETAIL REC.... All are output record formats What are “report headers” and “column headers?” What are “footer” records (“trailer” records) What are “detail” records? Explain.... ____________________________________________________________ INVENTORY REPORT STOCK NUMBERQTY-ON-HAND REORDER POINT UNIT COST WAREHOUSE PAGE NO: 14

20 Where are they written from?

21 So, we must WRITE all records from the single output process area, that is, the 01 area – FOR EACH FILE. Because of the availability of the Value clause, however, we can set up these "constant records" in working-storage, and move them to the print area and ‘write’ them as needed. (Set up records already ‘built’ and ready to be printed…) Otherwise, we would have to build up these constant records each time we wish to write the report trailers and the report header(s)... (since we have but a single output area from which Writing will occur. Do, however, use the file section’s 01 for the detail record (variable data). Why? Explain? Important to note... Looking ahead: Two classes of verbs: 1... Arithmetic, and 2… Conditional Statements Writing Records

22 BASIC FORMATS: ADD identifier-1 TO identifier-2 … literal-1 SUBTRACT identifier-1 FROM identifier-2… literal-1 MULTIPLY identifier-1 BY identifier-2 literal-1 DIVIDE identifier-1 INTO identifier-2 literal-1 BASIC IF FORMAT: IF (condition) (statement-1) … [ELSE (statement-2) … END-IF]

23 Examples Add 1 to Counter Add over-time-pay to regular-pay Subtract 1 from Total Subtract discount-amt from sub-total Multiply amt by 1.05 Divide temp into field-total If a > b Move ….. Write ….. Else Move… Add… End-if IF C = D Write printrec from hdr-1 after advancing Page End-if.