Structured COBOL Programming

Nancy Stern Hofstra University Robert A. Stern

“Copyright @ 2000 John Wiley & Sons, In. All rights reserved. Reproduction or translation of this work beyond that permitted in Section 117 of the 1976 United States Copyright Act without the express permission of the copyright owner is unlawful. Request for further information should be addressed to the permissions Department , John Wily & Sons, Inc. The purchaser may make back-up copies for his/her own use only and not for distribution or resale. The Publisher assumes no responsibility for errors, omissions, or damages, caused by the use of these programs or from the use of the information contained herein.”

Nassau Community College
PowerPoint Presentation: Richard H. Baum, Ph.D.
DeVry Institute of Technology

9th Edition

CHAPTER 11 Data Validation Techniques

Structured COBOL Programming, Stern & Stern, 9th Edition

OBJECTIVES
• To familiarize you with:

1. The types of input errors that may occur. 2. The techniques used to validate input data.
3. The actions that can be taken when input errors are detected.
Structured COBOL Programming, Stern & Stern, 9th Edition

CONTENTS
• Avoiding Logic Errors by Validating Input
– Why Input to a Business System Must Be Validated
– Some Consequences of Invalid Input – Data Validation Techniques – Self-Test – Using the EVALUATE Verb for Data Validation (COBOL 85) – Other Methods for Validating Data
Structured COBOL Programming, Stern & Stern, 9th Edition

CONTENTS
• What to Do If Input Errors Occur
– Print an Error Record Containing the Key Field, the Contents of the Erroneous Field, and an Error Message – Stop the Run
– Partially Process or Bypass Erroneous Records – Stop the Run if the Number of Errors Exceeds a Predetermined Limit

Structured COBOL Programming, Stern & Stern, 9th Edition

CONTENTS
– Use Switches

– Print Totals

• When Data Should Be Validated

• Understanding Program Interrupts

Structured COBOL Programming, Stern & Stern, 9th Edition

Avoiding Logic Errors By Validating Input

Structured COBOL Programming, Stern & Stern, 9th Edition

DEBUGGING TIPS FOR VALIDATING INPUT
1. For every IF statement in a program, be sure your test data includes multiple instances when the condition is met and multiple instances when the condition is not met. 2. If you use a line counter to determine when a new page is to print, be sure you include enough test data to print several pages.
Structured COBOL Programming, Stern & Stern, 9th Edition

DEBUGGING TIPS FOR VALIDATING INPUT
3. If you have ON SIZE ERROR routines, be sure your test data includes instances that would produce size errors. 4. If logic errors occur that are difficult to find, insert DISPLAY statements at various places in your program during test runs to see what intermediate results are being produced.
Structured COBOL Programming, Stern & Stern, 9th Edition

DEBUGGING TIPS
– Remember to eliminate these DISPLAY statements after the program has been fully debugged.

5. When producing disk output, always get a copy of the resulting file and check it for accuracy.
You can use a DISPLAY prior to the WRITE to view the output on the screen, or you can use an operating system command such as PRINT or TYPE to obtain a printout of the file after the program has been executed.
Structured COBOL Programming, Stern & Stern, 9th Edition

DEBUGGING TIPS
6. Pay particular attention to loop counts so that a series of instructions is performed the exact number of times that is required.
Often, looping is performed one time more or less than desired. This can happen if counters are set to 0 initially (when they should be set to 1) and if the test for terminating the loop is not consistent with the initial counter value.

Structured COBOL Programming, Stern & Stern, 9th Edition

Why Input to a Business System Must Be Validated
• Because input to a business system is often voluminous, the risks of data entry or input errors are great. • Steps must be taken to identify and correct these errors so they are not processed by the computer. • Data Validation Techniques:
1. Routines that identify the various types of input errors that may occur.
2. Error modules that print each specific error that has occurred.
Structured COBOL Programming, Stern & Stern, 9th Edition

Some Consequences of Invalid Input: Inaccurate Output
• If a data entry operator enters a salary field for a payroll record as 43265 instead of 41265, the result will be inaccurate output.
– It would be extremely difficult for a program itself to find such an error.

Structured COBOL Programming, Stern & Stern, 9th Edition

Some Consequences of Invalid Input: Logic Errors Resulting from Erroneous Input
• It is not unusual for programs that have been tested, debugged, and run regularly on a scheduled production basis to begin to produce errors.
• This situation will eventually arise if the programmer has not anticipated every conceivable type of input error.

Structured COBOL Programming, Stern & Stern, 9th Edition

Data Validation Techniques Testing Fields to Ensure a Correct Format
The Class Test
• Before actually processing input data, a program should first ensure that all input fields have the correct format. If an input field has a PIC of 9's, the programmer should make certain that the field does, in fact, have numeric data.

Structured COBOL Programming, Stern & Stern, 9th Edition

Data Validation Techniques Testing Fields to Ensure a Correct Format
Format for Class Test
IF identifier-1 IS {NUMERIC} {ALPHABETIC} THEN* statement-1 . . . [ELSE statement-2 . . .] END-IF*

*Optional with COBOL 85.

Structured COBOL Programming, Stern & Stern, 9th Edition

Data Validation Techniques Testing Fields to Ensure a Correct Format
The Sign Test
• If a numeric field is to have either positive or negative values, we may include a sign test to validate input data.

Structured COBOL Programming, Stern & Stern, 9th Edition

Data Validation Techniques Testing Fields to Ensure a Correct Format
• Format for Sign Test
IF identifier-1 IS {ZERO} {NEGATIVE} {POSITIVE}

THEN* statement-1 . . .

[ELSE statement-2 . . .] END-IF*

*Optional with COBOL 85.

Structured COBOL Programming, Stern & Stern, 9th Edition

Data Validation Techniques Checking for Missing Data
• One main source of error occurs when input fields are missing data. • If key fields must contain data, they should be checked before processing continues: IF SOC-SEC-NO = SPACES PERFORM 900-ERR-RTN

Structured COBOL Programming, Stern & Stern, 9th Edition

Data Validation Techniques Checking for Missing Data
• Alternatively, we could use a class test to determine if SOC-SEC-NO contains nonnumeric data: IF SOC-SEC-NO IS NOT NUMERIC PERFORM 900-ERR-RTN

Structured COBOL Programming, Stern & Stern, 9th Edition

Data Validation Techniques The INSPECT Statement
Tallying and Replacing Specific Characters with Other Characters to Minimize Errors Applications of the INSPECT Statement:

1. To count the number of occurrences of a given character in a field.
2. To replace specific occurrences of a given character with another character.
Structured COBOL Programming, Stern & Stern, 9th Edition

Data Validation Techniques The INSPECT STATEMENT
Format 1:

• INSPECT identifier-1 TALLYING {identifier-2 FOR {{ALL} {LEADING} {CHARACTERS}} {identifier-3} {literal-1}} [{BEFORE} {AFTER} INITIAL {identifier-4} {literal-2}]...
Structured COBOL Programming, Stern & Stern, 9th Edition

Data Validation Techniques The INSPECT STATEMENT
Format 2:

INSPECT identifier-1 REPLACING {{CHARACTERS} {{ALL} {LEADING} {FIRST}} {identifier-2} {literal-1}} BY {identifier-3} {literal-2} [{BEFORE} {AFTER} INITIAL {identifier-4} {literal-3}]}..
Structured COBOL Programming, Stern & Stern, 9th Edition

SELF-TEST

Structured COBOL Programming, Stern & Stern, 9th Edition

SELF-TEST
1. The two major functions of the INSPECT statement are ____ and ____ .

Solution: to replace certain characters with other characters; to count the number of occurrences of a given character in a field
Structured COBOL Programming, Stern & Stern, 9th Edition

SELF-TEST
2. (T or F) Literals in an INSPECT statement must be single characters or figurative constants.

Solution: T
Structured COBOL Programming, Stern & Stern, 9th Edition

SELF-TEST
For the following statements, fill in the missing columns:
FLDX

Statement Before/After Value of CTR 3. INSPECT FLDX TALLYING 10050 CTR1 FOR ALL ZEROS 4. INSPECT FLDX REPLACING 10050 ALL ZEROS BY SPACES 5. INSPECT FLDX TALLYING 00057 CTR1 FOR LEADING ZEROS 6. INSPECT FLDX TALLYING 00579 CTR1 FOR CHARACTERS BEFORE INITIAL '9'
Structured COBOL Programming, Stern & Stern, 9th Edition

Solution
For the following statements, fill in the missing columns:
FLDX

Statement Before/After Value of CTR 3. INSPECT FLDX TALLYING 10050 3 CTR1 FOR ALL ZEROS 4. INSPECT FLDX REPLACING 10050 not used ALL ZEROS BY SPACES 5. INSPECT FLDX TALLYING 00057 3 CTR1 FOR LEADING ZEROS 6. INSPECT FLDX TALLYING 00579 4 CTR1 FOR CHARACTERS BEFORE INITIAL '9'
Structured COBOL Programming, Stern & Stern, 9th Edition

Testing for Reasonableness
Range Tests • One way to validate data is to make certain that fields pass a range test;
– that is, the value contained in a particular field should fall within pre-established guidelines.

Limit Tests • When a field is not to exceed a given value we can perform a limit test, which is simply a range test without a lower bound.
Structured COBOL Programming, Stern & Stern, 9th Edition

Condition-Names: Checking Coded Fields for Valid Contents
• Coded fields are frequently used in input records to minimize keystrokes for data entry operators and to keep the input record format shorter and therefore less prone to errors.
– Thus, a field used to indicate an individual's marital status is not likely to be keyed as “SINGLE’’, “MARRIED”, “DIVORCED”.

• Rather, MARITAL-STATUS might be a one-position field that will be coded with a 1 to denote single, 2 for married etc.
Structured COBOL Programming, Stern & Stern, 9th Edition

Condition-Names: Checking Coded Fields for Valid Contents
• To make the coded field more easily understood, we may use instead 'M' for married, 'S' for single, and 'D' for divorced.

• Programs should make certain that the contents of coded fields are valid.
• Condition-names are frequently used to facilitate the coding of error control procedures in a program.
Structured COBOL Programming, Stern & Stern, 9th Edition

Sequence Checking
• Frequently, input records are entered in sequence by some control or key field. • A Social Security number may be a key field for a payroll file, a customer number may be a key field for an accounts receivable file, and so on. • A key field may also be a control field if it is used to signal a control break.
Structured COBOL Programming, Stern & Stern, 9th Edition

Sequence Checking
• If the keyed input data is to be in sequence, the actual order in which records are entered should be sequenced checked.

• Sometimes records are to be in ascending or descending sequence.

Structured COBOL Programming, Stern & Stern, 9th Edition

TYPICAL VALIDITY CHECKS
1. Determine if numeric data fields do, in fact, contain numeric data. The class test is as follows: IF identifier IS NUMERIC . . . 2. Determine if alphabetic data fields do contain alphabetic data. The class test is as follows: IF identifier IS ALPHABETIC . . .
Structured COBOL Programming, Stern & Stern, 9th Edition

TYPICAL VALIDITY CHECKS
3. Determine if data is missing. This can be accomplished with the following test: IF identifier IS EQUAL TO SPACES . . .

4. Use the INSPECT statement to replace all spaces with zeros in numeric fields.

Structured COBOL Programming, Stern & Stern, 9th Edition

TYPICAL VALIDITY CHECKS
• After a field has been verified to see if it contains the appropriate type of data you may need to further validate data as follows: 5. Determine if the value of a field falls within an established range;
– this is called a range test.

6. Determine if the value in a field does not exceed an established limit;
this is called a limit test.
Structured COBOL Programming, Stern & Stern, 9th Edition

TYPICAL VALIDITY CHECKS
7. Determine if specified fields contain valid codes or values.
– Use condition-names to help document such routines.

8. Determine if input records are in sequence, either ascending or descending, based on the control or key field.
Structured COBOL Programming, Stern & Stern, 9th Edition

Using the EVALUATE Verb for Data Validation (COBOL 85)
The EVALUATE statement is commonly used with COBOL 85 for data validation:
EVALUATE MODEL-CAR WHEN 1 WHEN 2 WHEN 3 PERFORM 200-COUPE-RTN PERFORM 300-SEDAN-RTN PERFORM 400-CONVERTIBLE-RTN PERFORM 800-ERROR-RTN

WHEN OTHER END-EVALUATE

Structured COBOL Programming, Stern & Stern, 9th Edition

The EVALUATE statement
• The three most common formats are: 1. EVALUATE identifier WHEN value(s) PERFORM . . . 2. EVALUATE TRUE WHEN condition PERFORM ... 3. EVALUATE condition WHEN TRUE PERFORM . . . WHEN FALSE PERFORM . . .
– TRUE and FALSE are COBOL reserved words that mean ``if the condition is met'' and ``if the condition is not met,'' respectively.
Structured COBOL Programming, Stern & Stern, 9th Edition

Other Methods for Validating Data: Use of Control Listings for Manual Validation of Input
• Computer errors commonly result from erroneous input, but they can also result from an intentional attempt to sabotage or defraud a company.
• One major method for minimizing the risk of undetected errors is to print a control listing or audit trail that includes:
Structured COBOL Programming, Stern & Stern, 9th Edition

Other Methods for Validating Data: Use of Control Listings for Manual Validation of Input
1. the identifying data or key fields in each input record
2. any errors encountered 3. totals of amounts accumulated for groups of input records processed.

Structured COBOL Programming, Stern & Stern, 9th Edition

Other Methods for Validating Data: Verification as a Means of Validating Input
Interactive Processing • One way to minimize keying errors when input is entered interactively is to verify that data has been keyed in properly using a verification procedure. • After all data for a record has been keyed, we can DISPLAY all the fields entered for that record along with a message that says 'IS DATA CORRECT (Y/N)?'.
Structured COBOL Programming, Stern & Stern, 9th Edition

Other Methods for Validating Data: Verification as a Means of Validating Input
Interactive Processing
• We use the ACCEPT verb to input the response. – If it is a 'Y' we continue, – If it is an 'N' we give the user an opportunity to reenter the data.

Structured COBOL Programming, Stern & Stern, 9th Edition

Verification as a Means of Validating Input
Interactive Processing

• Another way to verify data is to have an operator key each field in, transmit it to the computer, and have the computer ``echo'' that field back immediately to verify that what was transmitted is correct.

Structured COBOL Programming, Stern & Stern, 9th Edition

Verification as a Means of Validating Input
Interactive Processing

• A third way to minimize keying errors is by using a rekeying or verification procedure, which checks to see that the data originally keyed is the same as the data being keyed the second time.
– If it is not, then the operator who is verifying the data must find each error and correct it.

Structured COBOL Programming, Stern & Stern, 9th Edition

WHAT TO DO IF INPUT ERRORS OCCUR

Structured COBOL Programming, Stern & Stern, 9th Edition

WHAT TO DO IF INPUT ERRORS OCCUR
• Various types of procedures may be employed when input errors are detected. • The analyst, programmer, software developer, and user all work closely together to establish the most productive course of action to be taken. • We consider several procedures, any one of which may be used in a program.
Structured COBOL Programming, Stern & Stern, 9th Edition

Print an Error Record Containing the Key Field, the Contents of the Erroneous Field, and an Error Message
• Errors should always be clearly displayed with an appropriate message.
– The key field that identifies each erroneous record should also be included.

– In addition, a count should be maintained of the number of occurrences of each type of error.

• Typically, the user would be responsible for correcting all errors that have occurred.
Structured COBOL Programming, Stern & Stern, 9th Edition

Stop the Run

• If a major error occurs, it may be best simply to stop the run.
– This procedure is followed when data integrity is the primary consideration and errors must be kept to an absolute minimum. – Usually, a designated user would need to correct the error and arrange for the job to be restarted.

• If your program is to terminate because of an error, remember to:
– close all files before stopping, and – display or print a message explaining why the job is being stopped.
Structured COBOL Programming, Stern & Stern, 9th Edition

Partially Process or Bypass Erroneous Records
• Once an error is detected, the program could either:
(1) proceed to the next record, bypassing the erroneous record entirely (2) process some portion of the erroneous record.

• Sometimes, for example, an erroneously entered numeric field is replaced with zeros in the output area; sometimes the erroneous input is simply ignored.
Structured COBOL Programming, Stern & Stern, 9th Edition

Stop the Run if the Number of Errors Exceeds a Predetermined Limit
• Often we wish to continue processing even if errors occur, but if such errors become excessive, we can stop the run.

Structured COBOL Programming, Stern & Stern, 9th Edition

Use Switches
• Suppose we perform multiple validity tests on each record, and, after all tests we wish to process valid records only.
– That is, process records without any errors.

• We may use a switch for this purpose.
– We create a field called ERR- SWITCH initialized at 'N' for no errors. If any error occurs, we move 'Y' to ERR-SWITCH in each error routine to indicate that 'YES' an error has occurred. – After all validity tests, we test ERR-SWITCH. If it contains a 'Y', then an error has occurred and we proceed accordingly. If ERR-SWITCH is an 'N', then no error has occurred.
Structured COBOL Programming, Stern & Stern, 9th Edition

Print Totals
• Print a Count of All Records and a Count of All Errors
– Programs should provide a count of records processed as well as a count of errors that have occurred. – To determine the number of records processed, we ADD 1 TO WS-TOTAL-RECORDS each time we read a record and print the contents of WS-TOTAL-RECORDS at the end of the job.

Structured COBOL Programming, Stern & Stern, 9th Edition

Print Totals
• Print a Batch Total
– If large groups of input records are processed we might include batch totals.
• We would print a count of all records within specific groups or batches of records.

– Each individual total is called a batch total.
• The manual batch totals should match the computer-produced ones. If not, the user can track down the record or records that were not processed in the specific batch.

Structured COBOL Programming, Stern & Stern, 9th Edition

When Data Should Be Validated
• All programs to be run on a regularly scheduled basis should include data validation techniques designed to minimize errors.

Structured COBOL Programming, Stern & Stern, 9th Edition

Understanding Program Interrupts
• During program testing, logic errors sometimes occur that cause a program to terminate. • Such a termination is called a program interrupt. • Each time a program interrupt occurs, the computer prints a brief message that specifies the type of error that caused it.
Structured COBOL Programming, Stern & Stern, 9th Edition

COMMON PROGRAM INTERRUPTS
DATA EXCEPTION
1. You may be performing an arithmetic operation on a field that contains blanks or other nonnumeric characters. 2. You may be attempting to use a numeric field in a comparison and it contains blanks or other nonnumeric characters. 3. You may have failed to initialize a subscript or index.
Structured COBOL Programming, Stern & Stern, 9th Edition

COMMON PROGRAM INTERRUPTS
DIVIDE EXCEPTION
– You may be attempting to divide by 0. (On some systems, an attempt to divide by 0 will not cause an interrupt but will produce unpredictable results.)

ADDRESSING ERROR
1. You may have placed (or left) an incorrect value in a subscript or index so that a table look-up exceeds the number of entries in the table. 2. You may have coded nested PERFORMs (or GO TOs) improperly. This error will also occur if there is an improper exit from a paragraph being performed.
Structured COBOL Programming, Stern & Stern, 9th Edition

COMMON PROGRAM INTERRUPTS
OPERATION ERROR

• You may be attempting to access a file with a READ or WRITE before opening it. SPECIFICATION ERROR
• You may be attempting to access either an input area after an AT END condition or an output area directly after a WRITE.
Structured COBOL Programming, Stern & Stern, 9th Edition

CHAPTER SLIDES END HERE
CHAPTER SUMMARY COMES NEXT

Structured COBOL Programming, Stern & Stern, 9th Edition

CHAPTER SUMMARY
A. Types of Program Errors 1. Syntax errors - correct them before executing the program. 2. Logic errors - use sufficient test data to find them. B. Validating Data to Minimize Errors

1. Error control procedures can minimize errors but not eliminate them entirely.
Structured COBOL Programming, Stern & Stern, 9th Edition

CHAPTER SUMMARY
2. Types of error control procedures
a. Range tests - to ensure that data falls within a pre-established range. b. Limit tests - to ensure that data does not exceed a pre-established limit. c. Format tests - to ensure, for example, that numeric fields do, in fact, contain numeric data. d. Tests for missing data - to ensure that all critical fields contain nonzero or nonblank data. e. Sequence checks - to ensure that data is in the correct sequence.
Structured COBOL Programming, Stern & Stern, 9th Edition

CHAPTER SUMMARY
3. INSPECT Statement
a. Used to replace invalid characters with valid ones.
b. Used to count the occurrences of invalid characters.

4. Use condition-names to specify given values that identifiers may assume.

5. Use the EVALUATE verb to test for conditions if you are using COBOL 85.
Structured COBOL Programming, Stern & Stern, 9th Edition

CHAPTER SUMMARY
6. Verify input data.
a. Verification can be performed by displaying all data entered and asking the data entry operator to verify that it is correct. b. Data keyed in may be rechecked through a rekeying or verification procedure. If the rekeying produces different entries than the initial data entry, the reason for each discrepancy must be determined and corrective action taken.
Structured COBOL Programming, Stern & Stern, 9th Edition

CHAPTER SUMMARY
D. How to Handle Input Errors

1. If critical errors occur, stop the run.
2. Fill erroneous fields with spaces or zeros. 3. Count the occurrences of errors and stop the run if the number of errors is considered excessive.

Structured COBOL Programming, Stern & Stern, 9th Edition

CHAPTER SUMMARY
4. Print control listings or audit trails to be checked by the user department.
a. A control listing contains the key field and other identifying data for every record created, updated, or changed. b. The control listing also indicates the total number of records processed and any errors encountered.

Structured COBOL Programming, Stern & Stern, 9th Edition

Sign up to vote on this title
UsefulNot useful

Master Your Semester with Scribd & The New York Times

Special offer: Get 4 months of Scribd and The New York Times for just $1.87 per week!

Master Your Semester with a Special Offer from Scribd & The New York Times