610 likes | 1.11k Views
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.
E N D
CHAPTER 8Decision Making Using theIF and EVALUATEStatements 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
Selection • 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
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]* Strutured COBOL Programming, Stern & Stern, 9th Edition
Basic Conditional Statements 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
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 • 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
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. 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 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
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 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
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 THEN MOVE AMT-IN TO AMT-OUTEND-IF 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 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
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 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
ASCII and EBCDIC Collating Sequences 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
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. • If you do mix case be sure to use TOUpper etc. Strutured COBOL Programming, Stern & Stern, 9th Edition
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
SELF-TEST 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
SELF-TEST 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
SELF-TEST 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
SELF-TEST 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
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 SENTENCE (unless your compiler has an enhancement that permits it) but can always be used with CONTINUE. Strutured COBOL Programming, Stern & Stern, 9th Edition
Selection Using Other Options of the IF Statement Strutured COBOL Programming, Stern & Stern, 9th Edition
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
NESTED IFs • 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
NESTED IFs • 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
NESTED IFs • 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
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 conditions with one statement. Strutured COBOL Programming, Stern & Stern, 9th Edition
Compound ConditionalOR • 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
OR Strutured COBOL Programming, Stern & Stern, 9th Edition
Compound ConditionalOR 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 ADD AMT1 TO TOTAL 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
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. Strutured COBOL Programming, Stern & Stern, 9th Edition
AND Strutured COBOL Programming, Stern & Stern, 9th Edition
Compound ConditionalAND 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 ADD AMT1 TO TOTAL 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
Compound Conditional: Format IF condition-1 {OR} {AND} condition-2 [THEN] {statement-1 . . .} {CONTINUE} {ELSE statement-2 ... } [END-IF] Strutured COBOL Programming, Stern & Stern, 9th Edition
HIERARCHY RULES FOR COMPOUND CONDITIONALS 1. Conditions surrounding the word ANDare 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 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 AND and OR in the Same Statement • 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
SOLUTION 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 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 ) or it contains alphanumeric data, meaning any possible characters. Strutured COBOL Programming, Stern & Stern, 9th Edition
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 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
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