1 / 61

CHAPTER 8 Decision Making Using the IF and EVALUATE Statements - PowerPoint PPT Presentation

CHAPTER 8 Decision Making Using the IF and EVALUATE Statements. 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. Selection.

I am the owner, or an agent authorized to act on behalf of the owner, of the copyrighted work described.

PowerPoint Slideshow about 'CHAPTER 8 Decision Making Using the IF and EVALUATE Statements' - johana

Download Policy: Content on the Website is provided to you AS IS for your information and personal use and may not be sold / licensed / shared on other websites without getting consent from its author.While downloading, if for some reason you are not able to download a presentation, the publisher may have deleted the file from their server.

- - - - - - - - - - - - - - - - - - - - - - - - - - E N D - - - - - - - - - - - - - - - - - - - - - - - - - -
Presentation Transcript
CHAPTER 8Decision Making Using theIF and EVALUATEStatements

Strutured COBOL Programming, Stern & Stern, 9th Edition

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

• Selection Using

• Simple Conditional

• Nested Conditional

• Compound Conditional

• Sign and Class Tests

• Negating Conditionals

• Evaluate

• Condition Names (Named Conditions)

Strutured COBOL Programming, Stern & Stern, 9th Edition

LOGICAL CONTROL STRUCTURES

1. Sequence

2. Selection (IF-THEN-ELSE)

3. Iteration (PERFORM)

4. Case (EVALUATE)

Strutured COBOL Programming, Stern & Stern, 9th Edition

Format for IF statements:

IF condition-1 [THEN]*

imperative statement-1 . . .

[ELSE

imperative statement-2 . . . ]

[END-IF]*

Strutured COBOL Programming, Stern & Stern, 9th Edition

Simple Relational Conditions

1. IF identifier-1 IS EQUAL TO identifier-2

2. IF identifier-1 IS LESS THAN identifier-2

3. IF identifier-1 IS GREATER THAN identifier-2

Strutured COBOL Programming, Stern & Stern, 9th Edition

• Illustration of a simple conditional:

IF AMT1 IS EQUAL TO AMT2

DIVIDE QTY INTO TOTAL

ELSE

END-IF

Strutured COBOL Programming, Stern & Stern, 9th Edition

• 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

MOVE 'NO TRANSACTIONS THIS

MONTH' TO OUT-AREA

End-IF

WRITE PRINT-REC AFTER ADVANCING 2 LINES

Strutured COBOL Programming, Stern & Stern, 9th Edition

• 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

IF AMT1 IS EQUAL TO AMT2

MOVE NAME-IN TO NAME-OUT

MOVE DESCRIPTION-IN TO

DESCRIPTION-OUT

ELSE

END-IF.

Strutured COBOL Programming, Stern & Stern, 9th Edition

• 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

• 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.

Strutured COBOL Programming, Stern & Stern, 9th Edition

Using Relational Operators in Place of Words

The following symbols for simple relational conditions are valid within a COBOL statement:

RELATIONAL OPERATORS

SymbolMeaning

< IS LESS THAN

> IS GREATER THAN

= IS EQUAL TO

<= IS LESS THAN OR EQUAL TO

>= IS GREATER THAN OR EQUAL TO

Strutured COBOL Programming, Stern & Stern, 9th Edition

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 END-IFCODE-IN should be a nonnumeric field, since it is compared to a nonnumeric literal.

• IF CTR1 = CTR2 THEN ADD AMT1 TO TOTALEND-IF

Strutured COBOL Programming, Stern & Stern, 9th Edition

• 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 THEN MOVE AMT-IN TO AMT-OUTEND-IF

Strutured COBOL Programming, Stern & Stern, 9th Edition

Numeric Fields Should Not Contain Blanks

• Suppose we code IF AMT-IN IS EQUAL TO 10 ADD 1 TO COUNTER END-IF

• 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.

• 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

• 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

• The two types of internal codes that are most commonly used for representing data are:

EBCDIC is found on IBM and IBM-compatible 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

COLLATING SEQUENCES

EBCDICASCII

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

High 0-9 a-z

Strutured COBOL Programming, Stern & Stern, 9th Edition

• 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.

• If you do mix case be sure to use TOUpper etc.

Strutured COBOL Programming, Stern & Stern, 9th Edition

• 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

• 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

What is wrong with the following statements (1-6) ?

1. IF A IS LESS THAN B GO TO CONTINUE ELSE ADD 1 TO XX END-IF

Solution: You cannot say:

GO TO CONTINUE

Strutured COBOL Programming, Stern & Stern, 9th Edition

3. IF A EQUALS B MOVE 1 TO A END-IF

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

Strutured COBOL Programming, Stern & Stern, 9th Edition

4. IF A IS LESS THEN B MOVE 2 TO CODE1 END-IF

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

5. IF C = D MOVE 0 TO COUNTER. ELSE MOVE 100 TO COUNTER END-IF

Solution: There should be no period after MOVE 0 TO COUNTER.

Strutured COBOL Programming, Stern & Stern, 9th Edition

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 SENTENCE (unless your compiler has an enhancement that permits it) but can always be used with CONTINUE.

Strutured COBOL Programming, Stern & Stern, 9th Edition

Strutured COBOL Programming, Stern & Stern, 9th Edition

NESTED IFs

IF condition-1 IF condition – 2 … imperatives ELSE … imperatives END-IFELSE … imperativesEND-IF

IF condition-1 IF condition – 2 … imperatives ELSE … imperatives END-IFEND-IF

NESTED IFs

Strutured COBOL Programming, Stern & Stern, 9th Edition

• Using these hierarchy rules to evaluate

Given: A = 2, B = 2, C = 3, D = 4

IF C = D THEN IF A = B THEN PERFORM 600-PARA-1 ELSE PERFORM 500-PARA-2

END-IFELSE PERFORM 400-PARA-3END-IF

Strutured COBOL Programming, Stern & Stern, 9th Edition

• Using these hierarchy rules to evaluate

Given: A = 2, B = 2, C = 3, D = 4

IF A = B THEN IF C = D THEN PERFORM 600-PARA-1 ELSE PERFORM 500-PARA-2

END-IFELSE PERFORM 400-PARA-3END-IF

Strutured COBOL Programming, Stern & Stern, 9th Edition

• Using these hierarchy rules to evaluate

Given: A = 2, B = 2, C = 3, D = 4

IF C = D THEN IF A = B THEN PERFORM 600-PARA-1 ELSE PERFORM 500-PARA-2

END-IFEND-IF

Strutured COBOL Programming, Stern & Stern, 9th Edition

• 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 conditions with one statement.

Strutured COBOL Programming, Stern & Stern, 9th Edition

• By using OR in a compound conditional, if any of the conditions specified is true will cause execution of the statement(s).

Strutured COBOL Programming, Stern & Stern, 9th Edition

Strutured COBOL Programming, Stern & Stern, 9th Edition

Examples:

1. IF AMT1 = AMT2 OR AMT2 > AMT3 (IF AMT1 = AMT2 OR > AMT3)

PERFORM 500-TOTAL-RTN

END-IF.

2. IF AMT1 < AMT3 OR AMT1 = AMT4

ELSE

PERFORM 600-ERR-RTN

END-IF.

• 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

• 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.

Strutured COBOL Programming, Stern & Stern, 9th Edition

Strutured COBOL Programming, Stern & Stern, 9th Edition

Examples:

1. IF AMT1 = AMT2 AND AMT2 > AMT3 (IF AMT1 = AMT2 AND > AMT3)

PERFORM 500-TOTAL-RTN

END-IF.

2. IF AMT1 < AMT3 AND AMT1 = AMT4

ELSE

PERFORM 600-ERR-RTN

END-IF.

• If either of the conditions is not met, the computer executes either the ELSE clause, if coded, or the next sentence.

• Any number of conditions separated by ANDs may be coded in a single statement.

Strutured COBOL Programming, Stern & Stern, 9th Edition

IF condition-1 {OR} {AND} condition-2 [THEN]

{statement-1 . . .}

{CONTINUE}

{ELSE

statement-2 ... }

[END-IF]

Strutured COBOL Programming, Stern & Stern, 9th Edition

1. Conditions surrounding the word ANDare evaluated first.

2. Conditions surrounding the word OR are evaluated last.

Strutured COBOL Programming, Stern & Stern, 9th Edition

3. When there are severalAND 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 these hierarchy rules to evaluate

Given: A = 2, B = 2, C = 3, D = 4, E = 5, and F = 6

IF C = D AND E = F or A = B THEN PERFORM 600-PARA-1

END-IF

Strutured COBOL Programming, Stern & Stern, 9th Edition

FALSETRUE IF (C = D and E = F) or A = B THEN PERFORM 600-PARA-1END-IFFALSE or TRUE = true

Strutured COBOL Programming, Stern & Stern, 9th Edition

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

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 ) or it contains alphanumeric data, meaning any possible characters.

Strutured COBOL Programming, Stern & Stern, 9th Edition

• 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

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 data-entry 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

The following test may be used to minimize such errors:

IF AMT-IN IS NUMERIC

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

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

• The three alphabetic class tests for COBOL 85 are:

Reserved WordMeaning

ALPHABETIC A-Z, a-z, and blank

ALPHABETIC-UPPER A-Z and blank

ALPHABETIC-LOWER a-z and blank

Strutured COBOL Programming, Stern & Stern, 9th Edition

• 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

• A condition-name (named-condition) 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 PICTURE clause.

Strutured COBOL Programming, Stern & Stern, 9th Edition

• 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 PIC XX.

88 STATUS-OK VALUE '12'.IF STATUS-OK is equivalent toIF CODE-IN = ’12’

Strutured COBOL Programming, Stern & Stern, 9th Edition

• 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 an Alternative to Selection

Format:

EVALUATE {identifier-1}

{expression-1}

WHEN condition-1 imperative- statement-1 . . .

[WHENOTHER imperative-statement-2]

[END-EVALUATE]

Strutured COBOL Programming, Stern & Stern, 9th Edition

EVALUATE STATE-CODE an Alternative to Selection WHEN = ‘IL’ … imperatives WHEN = ‘IA’ … imperatives WHEN = ‘WI’ … imperatives WHEN OTHER … imperatives END-EVALUATE

IF STATE-CODE = ‘IL’ … imperativesEND-IF IF STATE-CODE = ‘IA’ … imperativesEND-IF IF STATE-CODE = ‘WI’ … imperativesEND-IF

EVALUATE

Strutured COBOL Programming, Stern & Stern, 9th Edition

EVALUATE (format 2) an Alternative to Selection

• EVALUATE TRUE WHEN GENDER = ‘M’ WHEN MARRIED = ‘S’ WHEN AGE > 21 WHEN OTHEREND-EVALUATE

alternative to nested IFs or a series of IF statements.

Strutured COBOL Programming, Stern & Stern, 9th Edition

QUESTIONS? an Alternative to Selection

Strutured COBOL Programming, Stern & Stern, 9th Edition