Nancy Stern Hofstra University Robert A. Stern: Nassau Community College

You might also like

Download as ppt, pdf, or txt
Download as ppt, pdf, or txt
You are on page 1of 79

Structured COBOL Programming

Nancy Stern Hofstra University Robert A. Stern


Copyright @ 2000 John Wiley & Sons, In. All rights reserved. Reproduction or transl ation 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 unl awful . 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 Publ isher assumes no responsibil ity 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

Strutured COBOL Programming, Stern & Stern, 9th Edition

CHAPTER 8 Decision Making Using the IF and EVALUATE Statements

Strutured COBOL Programming, Stern & Stern, 9th Edition

OBJECTIVES
To familiarize you with 1. The use of IF statements for selection. 2. The variety of formats and options available with the conditional statement. 3. The use of the EVALUATE statement with COBOL 85.
Strutured COBOL Programming, Stern & Stern, 9th Edition

CONTENTS
Selection Using a Simple IF Statement
A Review of Logical Control Structures Basic Conditional Statements Planning Conditional Statements with Pseudocode and Flowcharts How Comparisons Are Performed ASCII and EBCDIC Collating Sequences Ending Conditional Sentences with a Period or an END-IF Scope Terminator (COBOL 85) The CONTINUE or NEXT SENTENCE Clause
Strutured COBOL Programming, Stern & Stern, 9th Edition

CONTENTS
Selection Using Other Options of the IF Statement
Nested Conditional Compound Conditional Sign and Class Tests Negating Conditionals

Strutured COBOL Programming, Stern & Stern, 9th Edition

CONTENTS
Using IF Statements to Determine Leap Years Condition-Names The COBOL 85 EVALUATE Statement: Using the Case Structure as an Alternative to Selection

Strutured COBOL Programming, Stern & Stern, 9th Edition

Selection Using A Simple IF Statement

Strutured COBOL Programming, Stern & Stern, 9th Edition

A REVIEW of LOGICAL CONTROL STRUCTURES


LOGICAL CONTROL STRUCTURES 1. Sequence 2. Selection (IF-THEN-ELSE) 3. Iteration (PERFORM) 4. Case (EVALUATE)

Strutured COBOL Programming, Stern & Stern, 9th Edition

Basic Conditional Statements


Format for IF statements:
IF condition-1 [THEN]* imperative statement-1 . . . [ELSE imperative statement-2 . . . ] [END-IF]* *The words THEN and END-IF are COBOL Programming, Stern & Stern, 9th Edition 85 options. Strutured COBOL

Basic Conditional Statements


Simple Relational Conditions 1. IF identifier-1 IS EQUAL TO identifier-2 2. IF identifier-1 IS LESS THAN identifier2 3. IF identifier-1 IS GREATER THAN identifier-2

Strutured COBOL Programming, Stern & Stern, 9th Edition

Basic Conditional Statements


Illustration of a simple conditional: IF AMT1 IS EQUAL TO AMT2 DIVIDE QTY INTO TOTAL ELSE ADD UNIT-PRICE TO FINAL-TOTAL END-IF
Strutured COBOL Programming, Stern & Stern, 9th Edition

Basic Conditional Statements


There are two possible results of the test performed by the preceding statement: 1. AMT1 is equal to AMT2 or 2. AMT1 is not equal to AMT2

Strutured COBOL Programming, Stern & Stern, 9th Edition

Basic Conditional Statements


Explanation: 1. If AMT1 is equal to AMT2, the DIVIDE operation is performed and the second part of the statement, beginning with the ELSE clause, is ignored. 2. If the two fields are not equal, then the DIVIDE operation is not executed.
Only the ELSE portion of the statement, the ADD operation, is executed.

Strutured COBOL Programming, Stern & Stern, 9th Edition

Basic Conditional Statements


In either case, the program continues executing with the next sentence. Thus, by using the word IF, we test the initial condition and perform the instruction specified. By using ELSE, we can perform an operation if the initial condition is not met or is ``false.''
Strutured COBOL Programming, Stern & Stern, 9th Edition

Interpreting Instruction Formats


ELSE Is Optional. The ELSE clause in the instruction format is bracketed with [ ], which means that it is optional. If some operation is required only if a condition exists and nothing different need be done if the condition does not exist, the entire ELSE clause may be omitted.
Strutured COBOL Programming, Stern & Stern, 9th Edition

Basic Conditional Statements


Example of an IF Statement Without an ELSE Clause:
MOVE NAME-IN TO NAME-OUT MOVE AMOUNT-IN TO AMOUNT-OUT IF AMOUNT-IN IS EQUAL TO ZEROS THIS END-IF
Strutured COBOL Programming, Stern & Stern, WRITE PRINT-REC 9th Edition

MOVE 'NO TRANSACTIONS MONTH' TO OUT-AREA

Basic Conditional Statements


In the previous example, the message 'NO TRANSACTIONS THIS MONTH' is printed only if AMOUNTIN is zero. If AMOUNT-IN is not zero, we continue with the next sentence without performing any operation. The ELSE clause is unnecessary in this instance.
Strutured COBOL Programming, Stern & Stern, 9th Edition

Basic Conditional Statements


More Than One Operation Can Be Performed When a Condition Exists:
The instruction format includes dots or ellipses (...) indicating that more than one operation may be executed for each condition.

The following performs two MOVE operations if AMT1 is equal to AMT2, and two ADD operations if AMT1 is not equal to AMT2:
Strutured COBOL Programming, Stern & Stern, 9th Edition

Basic Conditional Statements


IF AMT1 IS EQUAL TO AMT2 MOVE NAME-IN TO NAME-OUT MOVE DESCRIPTION-IN TO DESCRIPTION-OUT ELSE ADD AMT1 TO TOTAL1 ADD AMT2 TO TOTAL2 END-IF.
Strutured COBOL Programming, Stern & Stern, 9th Edition

DEBUGGING TIP
Omitting the scope terminator is permitted for all versions of COBOL as long as the IF sentence ends with a period. However, we recommend that you use scope terminators with COBOL 85 and omit periods except for the last statement in a paragraph.
Strutured COBOL Programming, Stern & Stern, 9th Edition

Coding Guidelines: Indenting


Indent statements within the IF instruction to make programs easier to read and debug. The following is the coding style for conditionals: IF condition THEN imperative statement ... ELSE imperative statement ... END-IF. COBOL Programming, Stern & Stern, 9th Edition Strutured

Coding Guidelines
Indenting The technique of indenting and coding each statement on a separate line makes reading the program easier, but it does not affect compilation or execution. When errors do occur, it is much easier to isolate and correct them when each line contains a single statement (versus all statements run together on a single line).
Strutured COBOL Programming, Stern & Stern, 9th Edition

Basic Conditional Statements


Using Relational Operators in Place of Words The following symbols for simple relational conditions are valid within a COBOL statement: RELATIONAL OPERATORS Symbol Meaning < IS LESS THAN > IS GREATER THAN = IS EQUAL TO <= IS LESS THAN OR EQUAL TO >= IS GREATER THAN OR EQUAL TOStrutured COBOL Programming, Stern & Stern, 9th Edition

Basic Conditional Statements


Do Not Mix Field Types in a Comparison Conditional statements must use fields with the same data types to obtain proper results. In the statement, IF CODE-IN = 123 MOVE NAME-IN TO NAME- OUT, CODE-IN should be a nonnumeric field, since it is compared to a nonnumeric literal.

Strutured COBOL Programming, Stern & Stern, 9th Edition

Basic Conditional Statements


As in MOVE operations, the literal should have the same format as the data item.
If CODE-OUT has a PICTURE of 9's, the following would be appropriate:

IF CODE-OUT = 123 MOVE AMT-IN TO AMT-OUT.

Strutured COBOL Programming, Stern & Stern, 9th Edition

Basic Conditional Statements


Similarly, to ensure correct results, fields that are compared to one another should have the same data types, whether numeric or nonnumeric. Thus, in the statement, IF CTR1 = CTR2 ADD AMT1 TO TOTAL, both CTR1 and CTR2 should be either numeric or nonnumeric.

Strutured COBOL Programming, Stern & Stern, 9th Edition

Basic Conditional Statements


Numeric Fields Should Not Contain Blanks Suppose we code IF AMT-IN IS EQUAL TO 10 ADD 1 TO COUNTER. If AMT-IN were a field defined as numeric, but actually contained all blanks, the instruction would result in a data exception error, which causes a program interrupt.

Strutured COBOL Programming, Stern & Stern, 9th Edition

Basic Conditional Statements


Numeric Fields Should Not Contain Blanks This error will occur because blanks are not valid numeric characters. Be certain, then, that if a field is defined as numeric, it actually contains numbers.

Strutured COBOL Programming, Stern & Stern, 9th Edition

ASCII and EBCDIC Collating Sequences


When performing an alphanumeric comparison, the hierarchy of the comparison, called the collating sequence, depends on the computer being used.

Strutured COBOL Programming, Stern & Stern, 9th Edition

ASCII and EBCDIC Collating Sequences


The two types of internal codes that are most commonly used for representing data are: EBCDIC is found on IBM and IBMcompatible mainframes. ASCII is used on most micros and many minis and mainframes. The collating sequences for these differ somewhat.
Strutured COBOL Programming, Stern & Stern, 9th Edition

ASCII and EBCDIC Collating Sequences


COLLATING SEQUENCES EBCDIC Low ASCII

Spaces Spaces Special characters Special characters a-z 0-9 A-Z A-Z 0-9 a-z

High

Strutured COBOL Programming, Stern & Stern, 9th Edition

DEBUGGING TIP
Do not mix upper- and lowercase letters when entering data in fields. This reduces the risk that comparisons might give problematic results. As a convention, we recommend you use uppercase letters in all input fields as well as in instructions. Use lowercase letters only for comments.
Strutured COBOL Programming, Stern & Stern, 9th Edition

Ending Conditional Sentences with a Period or an END-IF Scope Terminator


With COBOL 85 you should use the ENDIF as a scope terminator to establish the specific boundaries:

IF PRICE1 IS LESS THAN PRICE2


ADD PRICE1 TO TOTAL MOVE 2 TO ITEM1 ELSE ADD PRICE2 TO TOTAL END-IF MOVECOBOLTO ITEM2. 9th Edition 0 Programming, Stern & Stern, Strutured

The CONTINUE or NEXT SENTENCE Clause


There are times when you might want to execute a series of steps only if a certain condition does not exist. The COBOL expression CONTINUE (COBOL 85) or NEXT SENTENCE (COBOL 74) will enable you: (1) to avoid performing any operation if a condition exists (2) to execute instructions only if the ELSE condition &is met. Strutured COBOL Programming, Stern Stern, 9th Edition

COBOL 2000+ CHANGES


Both CONTINUE and NEXT SENTENCE can be used interchangeably in the new standard. That is, NEXT SENTENCE will be permitted even if an END-IF scope terminator is used.

Strutured COBOL Programming, Stern & Stern, 9th Edition

QUESTIONS?

Strutured COBOL Programming, Stern & Stern, 9th Edition

SELF-TEST
What is wrong with the following statements (1-6) ? 1. IF A IS LESS THAN B ELSE ADD 1 TO END-IF
Strutured COBOL Programming, Stern & Stern, 9th Edition

GO TO CONTINUE

XX

SELF-TEST
2. IF A IS EQUAL TO '127' TO B ADD A END-IF

Solution: Since A is compared to a nonnumeric literal, it should be an alphanumeric field. But A is added to another field, which implies that it is numeric. Hence a data type mismatch exists. Although this may, in fact, produce the correct results (depending on the contents of A), it is inadvisable to make a comparison where one field or literal is Strutured COBOL Programming, nonnumeric andStern & Stern, 9th Edition is numeric. the other

SELF-TEST
3. IF A EQUALS B END-IF MOVE

1 TO A

Solution: This should be: IF A IS EQUAL TO B ....


Strutured COBOL Programming, Stern & Stern, 9th Edition

SELF-TEST
4. IF A IS LESS THEN B MOVE END-IF

2 TO CODE1

Solution: When the words GREATER and LESS are used, the COBOL word that follows is THAN and not THEN.
Strutured COBOL Programming, Stern & Stern, 9th Edition

SELF-TEST
5. IF C = D ELSE MOVE END-IF

MOVE 0 TO COUNTER.

100 TO COUNTER

Strutured COBOL Programming, Stern & Stern, 9th Edition Solution: There should be no period after

SELF-TEST
6. IF C = D MOVE 0 TO COUNTER ELSE NEXT

SENTENCE.

Solution: ELSE NEXT SENTENCE, although not incorrect, is unnecessary. Note that END-IF cannot be used with NEXT
Strutured COBOL Programming, Stern & Stern, 9th Edition

Selection Using Other Options of the IF Statement

Strutured COBOL Programming, Stern & Stern, 9th Edition

Compound Conditional
We have seen that selection and iteration structures provide programs with a great deal of logical control capability. The compound conditional offers even greater flexibility for selection and enables the IF statement to be used for more complex problems. With the compound conditional, the programmer can test for several Strutured COBOL Programming, one 9th Edition conditions withStern & Stern, statement.

Compound Conditional OR in a Compound Conditional


To perform an operation or a series of operations if any one of several conditions exists, use a compound OR condition. This means that if any one of several conditions exists, the imperative statement(s) specified will be executed.

Strutured COBOL Programming, Stern & Stern, 9th Edition

Compound Conditional OR in a Compound Conditional


Examples: 1. IF AMT1 = AMT2 OR AMT2 > AMT3 PERFORM 500-TOTAL-RTN END-IF. 2. IF AMT1 < AMT3 OR AMT1 = AMT4 ADD AMT1 TO TOTAL ELSE PERFORM 600-ERR-RTN END-IF. Programming, Stern & Stern, 9th Edition Strutured COBOL

Compound Conditional
By using OR in a compound conditional, any of the conditions specified causes execution of the statement(s). If none of the conditions is met, the computer executes either the ELSE clause, if coded, or the next sentence. Any number of conditions separated by ORs may be coded in a single statement.
Strutured COBOL Programming, Stern & Stern, 9th Edition

AND in a Compound Conditional


If a statement or statements are to be executed only when all of several conditions are met, use the word AND in the compound conditional. Thus, either AND or OR (or both) can be used in a compound conditional:

Strutured COBOL Programming, Stern & Stern, 9th Edition

Compound Conditional AND in a Compound Conditional: Format


IF condition-1 {OR} {AND} condition-2 . . [THEN]* {statement-1 . . .} {NEXT SENTENCE} {CONTINUE*} {ELSE statement-2 ... [END-IF]*} {ELSE NEXT SENTENCE}
Strutured COBOL Programming, Stern & Stern, 9th Edition

HIERARCHY RULES FOR COMPOUND CONDITIONALS


1. Conditions surrounding the word AND are evaluated first. 2. Conditions surrounding the word OR are evaluated last.

Strutured COBOL Programming, Stern & Stern, 9th Edition

HIERARCHY RULES FOR COMPOUND CONDITIONALS


3. When there are several AND or OR connectors, the AND conditions are evaluated first, as they appear in the statement, from left to right. Then the OR conditions are evaluated, also from left to right. 4. To override Rules 1-3, use parentheses around conditions you want to be evaluated first.
Strutured COBOL Programming, Stern & Stern, 9th Edition

Using AND and OR in the Same Statement


Using these hierarchy rules and the preceding example, the conditions will be evaluated as follows: (a) IF C = D AND E = F or (b) A = B With A = 2, B = 2, C = 3, D = 4, E = 5, and F = 6, 600-PARA-1 will be executed because A = B.

Strutured COBOL Programming, Stern & Stern, 9th Edition

Sign and Class Tests


Sign Test We can test whether a field is POSITIVE, NEGATIVE, or ZERO with a sign test. Example IF AMT IS POSITIVE PERFORM 200-CALC-RTN END-IF. We can also test to see if AMT IS NEGATIVE or ZERO.
Strutured COBOL Programming, Stern & Stern, 9th Edition

Sign and Class Tests


Class Test We can test for the type of data in a field by coding IF identifier- 1 IS NUMERIC or IF identifier-1 IS ALPHABETIC. If the ELSE option is executed with the NUMERIC class test, then either the field contains alphabetic data (only letters and/or spaces) or it contains alphanumeric data, meaning any possible Strutured COBOL Programming, Stern & Stern, 9th Edition characters.

Sign and Class Tests


Suppose we code the following: IF AMT-IN IS NUMERIC PERFORM 300-CALC-RTN ELSE PERFORM 400-ERROR-RTN END-IF If the field contains 123AB, for example, the ELSE clause will be executed since the contents of the field are not strictly numeric.
Strutured COBOL Programming, Stern & Stern, 9th Edition

Sign and Class Tests


Using Class Tests for Validating Data A class test is a useful tool for minimizing program errors. Suppose we wish to add AMT-IN to TOTAL, where AMT-IN is an input field. Since input is always subject to dataentry errors, it is possible that the field might be entered erroneously with nonnumeric data or spaces.
In such a case, ADD AMT-IN TO TOTAL can cause the computer to abort the run.
Strutured COBOL Programming, Stern & Stern, 9th Edition

Sign and Class Tests


The following test may be used to minimize such errors: IF AMT-IN IS NUMERIC ADD AMT-IN TO TOTAL ELSE PERFORM 500-ERR-RTN END-IF
It is a good practice to validate the AMT-IN field, as in the preceding, before performing the arithmetic.
As noted, periods are optional when using END-IF unless you are at the end of a paragraph.
Strutured COBOL Programming, Stern & Stern, 9th Edition

Sign and Class Tests


ALPHABETIC class with COBOL 85 COBOL 85 has eliminated the ambiguity over uppercase/lowercase when making the ALPHABETIC class test. Either uppercase or lowercase, or any blank is considered ALPHABETIC. Moreover, two new class tests have been added: ALPHABETIC-UPPER AND ALPHABETIC-LOWER Strutured COBOL Programming, Stern & Stern, 9th Edition

Sign and Class Tests


The three alphabetic lass tests for COBOL 85 are: Reserved Word ALPHABETIC blank ALPHABETIC-UPPER ALPHABETIC-LOWER Meaning A-Z, a-z, and A-Z and blank a-z and blank

Strutured COBOL Programming, Stern & Stern, 9th Edition

Sign and Class Tests


The following is an example of the Alphabetic Class Test: IF NAME-IN IS ALPHABETIC-LOWER THEN PERFORM 600-LOWER-CASE-RTN END-IF
Strutured COBOL Programming, Stern & Stern, 9th Edition

Using IF statements to determine Leap Year


Most people believe that leap years occur every four years. The traditional method for calculating a leap year is to divide the year by four.
If the year is evenly divisible by four, (i.e., no remainder) then it is typically considered a leap year.

The above procedure is not entirely correct, however.


Strutured COBOL Programming, Stern & Stern, 9th Edition

Using IF statements to determine Leap Year


Leap years are those divisible by four-except for years ending in 00--only those years ending in 00 that are also divisible by 400 are leap years. This means that 1900 was not a leap year, but 2000 is a leap year. The reason for the anomaly is that leap years were created to make adjustments to dates.
Strutured COBOL Programming, Stern & Stern, 9th Edition

Using IF statements to determine Leap Year


It takes the earth 365 1/4 days to revolve around the sun--approximately. We adjust for the 1/4 day by having one extra day every leap year.

This anomaly has Y2K ramifications. If two-digit year dates are still being used, then a year of 00 will be considered 1900 unless some adjustment is made. With COBOL 85, we can determine if any year is a leap year by Edition using an IF Strutured COBOL Programming, Stern & Stern, 9th statement.

CONDITION-NAMES
A condition-name is a user-defined word established in the DATA DIVISION that gives a name to a specific value that an identifier can assume. An 88-level entry coded in the DATA DIVISION is a condition-name that denotes a possible value for an identifier, which then can be tested to be either True or False.
A condition-name is always coded on the 88 level and has only a VALUE clause associated with it and will not contain a PICTUREProgramming, Stern & Stern, 9th Edition Strutured COBOL clause.

CONDITION-NAMES
Format for 88-level items: 88 condition-name VALUE literal The condition-name must be unique and its VALUE must be a literal consistent with the data type of the field preceding it: 05 CODE-IN 88 STATUS-OK
Strutured COBOL Programming, Stern & Stern, 9th Edition

PIC XX. VALUE '12'.

CONDITION-NAMES
For readability, we indent each 88-level item to clarify its relationship to the data-name directly preceding it. Any elementary item on level numbers 01--49 in the FILE SECTION or in the WORKING-STORAGE may have a condition-name associated with it.

Strutured COBOL Programming, Stern & Stern, 9th Edition

The COBOL 85 EVALUATE Statement: Using the Case Structure as an Alternative to Selection

Strutured COBOL Programming, Stern & Stern, 9th Edition

The COBOL 85 EVALUATE Statement


Format: EVALUATE {identifier-1} {expression-1} WHEN condition-1 imperativestatement-1 . . . 2] [WHEN OTHER imperative-statement-

[END-EVALUATE]Stern & Stern, 9th Edition Strutured COBOL Programming,

CHAPTER SLIDES END HERE


CHAPTER SUMMARY COMES NEXT

Strutured COBOL Programming, Stern & Stern, 9th Edition

CHAPTER SUMMARY
A. Simple Relational for Selection 1. Relations IS {EQUAL} {=} TO IS {LESS THAN} {<} IF identifier-1 IS {GREATER THAN} {>} identifier-2 IS {LESS THAN OR EQUAL TO} {<=}* IS {GREATER THAN OR EQUAL TO} {>=}* *COBOL 85 only.
Strutured COBOL Programming, Stern & Stern, 9th Edition

CHAPTER SUMMARY
2. If the condition exists, all statements are executed up to (a) the ELSE clause or (b) the END-IF (COBOL 85) or the period if there is no ELSE clause. 3. If the condition does not exist, the statements after the word ELSE, if coded, are executed, or (if there is no ELSE clause) processing continues after the END-IF or with the next sentence.
Strutured COBOL Programming, Stern & Stern, 9th Edition

CHAPTER SUMMARY
4. Comparisons are algebraic or logical: (1) Numeric: 12.0 = 12.00 = 12 = +12 (2) Nonnumeric: ABC = ABCb = ABCbb (b = blanks) 5. Collating sequences (EBCDIC and ASCII) are the same with regard to A--Z, 0--9, and a--z. They differ when upper- and lowercase letters are compared or when letters and digits are compared.
Strutured COBOL Programming, Stern & Stern, 9th Edition

CHAPTER SUMMARY
With ASCII, lowercase letters are greater than uppercase letters; With EBCDIC, lowercase letters are less than uppercase letters. With EBCDIC, letters are less than numbers. With ASCII, numbers are less than letters.
Strutured COBOL Programming, Stern & Stern, 9th Edition

CHAPTER SUMMARY
B. Other Types of IF Statements 1. Compound Condition a. Format IF condition-1 OR condition-2 . . . IF condition-1 AND condition-2 . . .

Strutured COBOL Programming, Stern & Stern, 9th Edition

CHAPTER SUMMARY
b. Hierarchy (1) If ORs and ANDs are used in the same sentence, ANDs are evaluated first from left to right, followed by ORs. (2) Parentheses can be used to override hierarchy rules.

Strutured COBOL Programming, Stern & Stern, 9th Edition

CHAPTER SUMMARY
2. Other Tests a. Sign test IF identifier-1 IS {POSITIVE} {NEGATIVE} {ZERO}
Identifier-1 must have an S in its PIC clause if it is to store data with a negative value.
Strutured COBOL Programming, Stern & Stern, 9th Edition

CHAPTER SUMMARY
b. Class Test IF identifier-1 IS {NUMERIC} {ALPHABETIC} c. Negated Conditionals (1) A test can be preceded with NOT to test the negative conditional. (2) IF NOT (A = B OR A = C) is the Strutured COBOL Programming, Stern & Stern, 9th IF A NOT = B AND Edition same as

CHAPTER SUMMARY
C. Condition-Names 1. Coded on the 88-level directly following the field to which it relates. For example: 05 CODE-IN 88 OK-CODE PIC X. VALUE '6'.

Strutured COBOL Programming, Stern & Stern, 9th Edition

CHAPTER SUMMARY
2. A condition-name specifies a condition in the PROCEDURE DIVISION. For example: IF OK-CODE PERFORM 200-OK-RTN END-IF D. The COBOL 85 EVALUATE statement is often used as an alternative to nested IFs or a series of IF statements.
Strutured COBOL Programming, Stern & Stern, 9th Edition

You might also like