54
11-1 Structured COBOL Programming Nancy Stern Hofstra University Robert A. Stern Nassau Community College James P. Ley University of Wisconsin- Stout John Wiley & Sons, Inc. PowerPoint Winifred J. Rex Presentation Bowling Green State University 10th edition

COBOL DATA VALIDATION

Embed Size (px)

DESCRIPTION

VISIT http:\\lakshmideepak.blogspot.com

Citation preview

Page 1: COBOL DATA VALIDATION

11-1

Structured COBOL Programming

Nancy Stern Hofstra University

Robert A. Stern Nassau Community College

James P. Ley University of Wisconsin-Stout

John Wiley & Sons, Inc.

PowerPoint Winifred J. Rex Presentation Bowling Green State University

10th edition

Page 2: COBOL DATA VALIDATION

11-2

Data Validation

Chapter 11

Page 3: COBOL DATA VALIDATION

11-3

Chapter Objectives

To familiarize you with

• Types of input errors that may occur

• Techniques used to validate input data

• Actions that can be taken when input errors are detected

Page 4: COBOL DATA VALIDATION

11-4

Chapter Contents

• Avoiding Logic Errors by Validating Input

• What to Do If Input Errors Occur

• Global Considerations in COBOL

• When Data Should Be Validated

• Understanding Program Interrupts

• Other Methods for Improving Program Performance

Page 5: COBOL DATA VALIDATION

11-5

Program Errors - Syntax

• Due to violations of language rules

• Detected by compiler

Page 6: COBOL DATA VALIDATION

11-6

Program Errors - Logic

• Result from using incorrect instructions or incorrect sequence of instructions

• Also include run-time errors

• Not detected during compilation

• Found by running program with test data and comparing outcome to expected results

Page 7: COBOL DATA VALIDATION

11-7

Avoiding Logic Errors

• Develop comprehensive test data

• Include all condition and types of data tested for in program

• Have someone other than programmer prepare test data to avoid bias

• Manually check computer-produced results for accuracy

Page 8: COBOL DATA VALIDATION

11-8

Debugging Tips

• For every IF statement, include test data that satisfies and does not satisfy condition

• For multi-page report include enough test data to print several pages

• Include test data that produces size errors if ON SIZE ERROR routines are used

Page 9: COBOL DATA VALIDATION

11-9

Debugging Tips

• Used DISPLAY statements during test runs to isolate logic errors

• If program produces disk file, always examine it for accuracy

• Check loops to see that instructions performed exact number of times required

Page 10: COBOL DATA VALIDATION

11-10

Why Input Must Be Validated

• Risk of data entry errors is high– Large volume of data entered– Human error keying in data

• Invalid input leads to inaccurate output– For example, salary reported incorrectly if

entered as 23000 instead of 32000

• Input error can cause program interrupt– For example, spaces entered for numeric

field used in arithmetic operation

Page 11: COBOL DATA VALIDATION

11-11

Data Validation Techniques

• Routines to identify various types of input errors

• Error modules to handle each error that occurs

Page 12: COBOL DATA VALIDATION

11-12

Test Fields for Correct Format

• Use NUMERIC class test to ensure field used in arithmetic operation has numeric value

If Amt-In Is Not NumericPerform 500-Err-RtnElse

Add Amt-In To WS-Total

End-If

Example

Page 13: COBOL DATA VALIDATION

11-13

Test Fields for Correct Format

• Use ALPHABETIC class test if field must be alphabetic

• Use sign test if numeric field is to have – Values greater than zero (POSITIVE)– Values less than zero (NEGATIVE)– Value equal to zero (ZERO)

• S must be included in PIC to store a negative number

• NOT POSITIVE is not same as NEGATIVE

Page 14: COBOL DATA VALIDATION

11-14

Checking for missing data

• Check key fields if they must contain data

If Soc-Sec-No = Spaces

Perform 900-Err-Rtn

End-If

Example

Page 15: COBOL DATA VALIDATION

11-15

INSPECT Statement

• Useful for validity checking as well as other purposes

• Two main functions– To count number of occurrences of given

character in field– To replace specific occurrences of given

character with another character

Page 16: COBOL DATA VALIDATION

11-16

INSPECT … TALLYING

• To count number of times a given character occurs

INSPECT identifier-1 TALLYING

identifier-2 FOR ALL identifier-3

LEADING literal-1 ...

CHARACTERS

Format

Page 17: COBOL DATA VALIDATION

11-17

INSPECT … TALLYING

• identifier-1– Field to be "inspected"

• identifier-2– Field where count stored– Not automatically set to zero by INSPECT

• identifier-3 or literal-1– Character to be counted– ZERO, SPACE, 8, 'S' are valid entries for

literal-1

Page 18: COBOL DATA VALIDATION

11-18

Options with FOR Clause

• ALL - every occurrence of specified character in field counted

• LEADING - all occurrences of specified character preceding any other character tallied

• CHARACTERS - all characters within field tallied– Used to determine size of field

Page 19: COBOL DATA VALIDATION

11-19

INSPECT … TALLYING

Move Zeros To Ct1, Ct2, Ct3

Inspect X1 Tallying Ct1 For All Spaces

Inspect X2 Tallying Ct2 For Characters

Inspect X3 Tallying Ct3 For Leading Zeros

Fields Results X1 = bb82b Ct1 = 3

X2 = AB32C Ct2 = 5

X3 = 00060 Ct3 = 3

Examples

Page 20: COBOL DATA VALIDATION

11-20

BEFORE/AFTER Clause

• Optional clause after FOR options to count only characters before or after some initial value

BEFORE INITIAL identifier-4

AFTER literal-2

Format

Page 21: COBOL DATA VALIDATION

11-21

BEFORE/AFTER Clause

Move Zeros To Ct4, Ct5

Inspect X4 Tallying Ct4 For All Zeros Before Initial 9

Inspect X5 Tallying Ct5 For Characters After Initial 6

Items Results X4 = 05090 Ct4 = 2

X5 = 06762 Ct5 = 3

Examples

Page 22: COBOL DATA VALIDATION

11-22

INSPECT … REPLACING

• To replace specified occurrences of a given character with another

INSPECT identifier-1 REPLACING

CHARACTERS

ALL identifier-2 BY identifier-3

LEADING literal-1 literal-2 ...

FIRST

Format

Page 23: COBOL DATA VALIDATION

11-23

INSPECT … REPLACING

• Literals must be single characters or figurative constants consistent with type of field being inspected

• ALL, LEADING, CHARACTERS have same meaning as previously noted

• FIRST means first occurrence of literal-1 will be replaced by literal-2

• BEFORE/AFTER clause can be used

Page 24: COBOL DATA VALIDATION

11-24

INSPECT … REPLACING

Inspect Date-In Replacing All '-' By '/'

Inspect SSNo Replacing All Spaces By '-'

Field Before After

Date-In 10-17-02 10/17/02

SSNo 123 45 6789 123-45-6789

Examples

Page 25: COBOL DATA VALIDATION

11-25

INSPECT … REPLACING

Inspect X1 Replacing Leading 'A' By 'Z'

Inspect X2 Replacing First 'R' By 'Q'

Field Before After

X1 AAABBA ZZZBBA

X2 RRSST QRSST

Examples

Page 26: COBOL DATA VALIDATION

11-26

Testing for Reasonableness

• Use after verifying that numeric fields contain numeric data

• Range test - check that field is within established lower and upper bounds

• Limit test - check that field does not exceed defined upper limit

Page 27: COBOL DATA VALIDATION

11-27

Checking Coded Fields

• Code often stored in field to shorten record and minimize typing

• For example, 'H' or 'S' may represent pay type of 'Hourly' or 'Salaried'

• Use condition names to check validity of coded fields

Page 28: COBOL DATA VALIDATION

11-28

Checking Coded Fields

05 Pay-Code Pic X.

88 Hourly Value 'H'.

88 Salaried Value 'S'.

If Hourly Or Salaried Then

Perform Pay-Calc-Rtn

Else

Perform Pay-Code-Err-Rtn

End-If

Example

Data Division entries

Procedure Division statements

Page 29: COBOL DATA VALIDATION

11-29

Typical Validity Checks

• Class test - determine if field contains appropriate type of data (NUMERIC, ALPHABETIC)

• Determine if data is missing by comparing field to SPACES

• Replace spaces in numeric fields with ZEROS using INSPECT statement

Page 30: COBOL DATA VALIDATION

11-30

Typical Validity Checks

• Range test - determine if field in established range

• Limit test - determine if field exceeds established limit

• Determine if field contains valid codes or values using condition-names to document and test fields

Page 31: COBOL DATA VALIDATION

11-31

EVALUATE Statement

• Perform class tests first to ensure field is proper type

• Use EVALUATE for further validation tests

• Three common forms

Page 32: COBOL DATA VALIDATION

11-32

1) EVALUATE identifier

EVALUATE identifier

WHEN value(s) PERFORM ……

Evaluate Pay-Code

When 'H' Perform 300-Hourly-Rtn

When 'S' Perform 400-Salaried-Rtn

End-Evaluate

Example

Page 33: COBOL DATA VALIDATION

11-33

1) EVALUATE identifier

• May also use THRU clause to check range of values

Evaluate AgeWhen 0 Thru 19

Perform 400-Minor-RtnWhen 20 Thru 99

Perform 500-Adult-RtnEnd-Evaluate

Example

Page 34: COBOL DATA VALIDATION

11-34

2) EVALUATE TRUE

EVALUATE TRUEWHEN condition PERFORM …

Evaluate TrueWhen Age >= 0 And <= 19

Perform 400-Minor-RtnWhen Age >= 20 And <= 99

Perform 500-Adult-RtnEnd-Evaluate

Example

Page 35: COBOL DATA VALIDATION

11-35

2) EVALUATE TRUE

• Can also use with condition-names

Assume these condition names defined for Age field

05 Age Pic 99.88 Minor Values 0 Thru 19.88 Adult Values 20 Thru 99.

Example

Page 36: COBOL DATA VALIDATION

11-36

2) EVALUATE TRUE

Evaluate TrueWhen Minor

Perform 400-Minor-RtnWhen Adult

Perform 500-Adult-RtnEnd-Evaluate

Page 37: COBOL DATA VALIDATION

11-37

2) EVALUATE TRUE

• Note that using Age in place of True in this statement causes syntax error

• Must compare numeric field Age to another numeric field or numeric literal

• Compare TRUE to a condition or condition-name with value of TRUE or FALSE

Page 38: COBOL DATA VALIDATION

11-38

3) EVALUATE condition

EVALUATE conditionWHEN TRUE PERFORM …WHEN FALSE PERFORM …

Evaluate Age <= 19When True Perform 400-Minor-RtnWhen False Perform 500-Adult-Rtn

End-Evaluate

Example

Page 39: COBOL DATA VALIDATION

11-39

Actions If Input Errors Occur

1. Print error record displaying key field, field in error and error message

2. Stop the run to preserve data integrity

3. Partially process or bypass erroneous records

4. Stop the run if number of errors exceeds predetermined limit

Page 40: COBOL DATA VALIDATION

11-40

Actions If Input Errors Occur

5. Use switch or field to indicate when record contains error

– Initialize field to 'N' for no errors– Set field to 'Y' anytime an error occurs– Process record as valid only if switch field

still 'N' after all validation checks

Page 41: COBOL DATA VALIDATION

11-41

Actions If Input Errors Occur

6. Print count totals and compare to manual counts

– Print count of all records processed– Print count of all errors encountered– Print batch totals or count of all records

within specific groups or batches

Page 42: COBOL DATA VALIDATION

11-42

Global Considerations

• Meaning of comma and decimal point in numbers in United States is reversed in some other nations

• 4,123.45 in United States represented as 4.123,45 in other nations

• To change representation of numbers in COBOL use SPECIAL-NAMES paragraph

Page 43: COBOL DATA VALIDATION

11-43

SPECIAL-NAMES paragraph

Environment Division.

Configuration Section.

Special-Names.

Decimal-Point is Comma.

• Number 4123,45 stored in field with PIC 9999V99

• When moved to report-item with PIC 9.999,99 is displayed as 4.123,45

Page 44: COBOL DATA VALIDATION

11-44

COBOL 2002+ Changes

• Restrictions on INSPECT statement limiting AFTER/BEFORE items to one-character literals or fields in REPLACING clause will be eleiminated

• VALIDATE statement introduced to check format of data field

Page 45: COBOL DATA VALIDATION

11-45

Program Interrupts

• Termination of program caused by logic error

• List of common program interrupts and their causes follows

Page 46: COBOL DATA VALIDATION

11-46

Common Program Interrupts

• Data Exception– Performing one of these operations on field

containing blanks or other nonnumeric characters

• Arithmetic operation• Comparison

– Failing to initialize subscript or index

Page 47: COBOL DATA VALIDATION

11-47

Common Program Interrupts

• Divide Exception– Attempting to divide by zero

• Addressing Error– Referring to array or table entry with value

in subscript or index that exceeds number of entries in table

– Improperly coding nested PERFORMs or exiting from paragraph being performed

Page 48: COBOL DATA VALIDATION

11-48

Common Program Interrupts

• Operation Error– Attempting to access file with READ or

WRITE before opening it or after closing it

• Specification Error– Attempting to access input area after AT

END condition

Page 49: COBOL DATA VALIDATION

11-49

Common Program Interrupts

• Illegal Character in Numeric Field– May be caused by type mismatch between

actual data and PIC clause– Field size specified in PIC clause may not

match actual size of field in record, leading to invalid (nonnumeric) characters from another field being treated as part of numeric field

Page 50: COBOL DATA VALIDATION

11-50

Chapter Summary

• Two types of program errors– Syntax errors– Logic errors

• Error control procedures can minimize errors but not eliminate them entirely

Page 51: COBOL DATA VALIDATION

11-51

Chapter Summary

• Types of error control procedures– Range tests– Limit tests– Format tests– Tests for missing data– Sequence checks

Page 52: COBOL DATA VALIDATION

11-52

Chapter Summary

• Other methods to validate data– INSPECT statement– Condition-names– EVALUATE verb– Verifying input data

Page 53: COBOL DATA VALIDATION

11-53

Chapter Summary

• Handling input errors– Stop the run– Fill erroneous fields with spaces or zeros– Stop the run if number of errors excessive– Print control listings or audit trails to be

checked

Page 54: COBOL DATA VALIDATION

11-54

Copyright © 2003 John Wiley & Sons, Inc. 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 written permission of the copyright owner is unlawful. Request for further information should be addressed to the Permissions Department, John Wiley & 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.