CHAPTER 8

DECISION MAKING USING THE IF AND EVALUATE STATEMENTS

CHAPTER OBJECTIVES

After completion of this chapter, the student should be able to:

1.         List the four logical control structures.

2.         Describe the format of the IF or conditional statement.

3.         Define the term imperative statement.

4.         Identify the two methods to terminate the IF statement.

5.         List the relational operators used with the IF statement.

6.         Define the term collating sequence.

7.         Describe the differences in the rules for the EBCDIC and ASCII collating sequences.

8.         Explain the purpose of the CONTINUE clauses and describe how it is used.

9.         Describe what a nested conditional is.

10.       Describe what a compound conditional is.

11.       Explain the purpose of the sign test and describe how it is used.

12.       Explain the purpose of the class test and describe how it is used.

13.              Explain what is meant by a negated conditional.

14.       List the criteria for determining whether a year is a leap year and write COBOL code to implement the leap-year test.

15.       Explain what a condition-name is and describe how it is used.

16.       Explain the purpose of the EVALUATE statement and describe how it is used.

LECTURE OUTLINE

I.          Selection Using a Simple IF Statement

A.        A Review of Logical Control Structures

1.         Logical control structures enable the computer to make decisions that affect the order in which instructions are executed.

2.         The logical control structures are:

a.         Sequence

b.         Selection (IF-THEN-ELSE)

c.         Iteration (PERFORM)

d.         Case (EVALUATE)

B.         Basic Conditional Statements

1.         A conditional statement is one that performs operations depending on the existence of some condition.

2.         Conditional statements in COBOL generally begin with the word IF and are called IF-THEN-ELSE or selection structures.

3.         The basic format for the IF statement is:

IF condition-1

[THEN]

imperative-statement-1 ...

[ELSE

imperative-statement-2...]

[END-IF]

4.         An imperative statement is a statement that performs an operation regardless of any existing conditions.

5.         The words THEN and END-IF are optional.

6.         Condition-1 may be a simple relational condition, which has the following form:

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

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

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

7.         If the condition tested exists, all statements up to the ELSE are executed if there is an ELSE clause; if not all statements up to the END-IF or period are executed.

8.         If the condition tested does not exist, the statements after the ELSE clause (if coded) are executed.

9.         The END-IF scope terminator can be used to terminate the IF statement.  A period after END-IF is optional

10.       The ELSE clause is optional and is required only when special processing must be done if the condition does not exist.

11.       More than one operation may be specified in either the THEN or the ELSE clauses.

12.       Indentation within the IF-THEN-ELSE instruction is used to make the program easier to read and debug.

13.       The following symbols for simple relational conditions are valid with a COBOL statement.  Either the symbols or the words may be used:

a.         IS LESS THAN                                               <

b.         IS GREATER THAN                                       >

c.         IS EQUAL TO                                                =

d.         IS LESS THAN OR EQUAL TO                    <=

e.         IS GREATER THAN OR EQUAL TO            >=

f.          <= and >= are not available in COBOL 74

14.       Conditional statements must compare fields with the same data types to obtain proper results.

15.       Numeric fields should not contain blanks.

C.        Planning Conditional Statements with Pseudocode

Refer to the text for pseudocode examples involving:

1.         A simple conditional without an ELSE clause.

2.         A simple conditional in which multiple statements are executed when the condition is true.

D.        How Comparisons Are Performed

1.         Numeric comparisons are performed algebraically.  This means that the comparisons are based on the numeric values of the fields rather than on their PICTURE clauses.

2.         Low-order (rightmost) blanks do not affect the equivalence of nonnumeric fields; only significant (non-blank) positions are compared, from left to right.

E.         ASCII and EBCDIC Collating Sequences

1.         Collating sequence refers to the specific order, known as the character hierarchy, in which characters are represented by a computer.

2.         When performing an alphanumeric comparison, the collating sequence of the particular computer will affect the results.

3.         On ASCII computers, uppercase letters are less than lowercase letters, whereas the reverse is true with EBCDIC computers.

4.         On EBCDIC machines, letters are less than numbers; on ASCII machines, numbers are less than letters.

F.         Ending Conditional Sentences With a Period or an END-IF Scope Terminator

1.         If an END-IF is not used in an IF statement, the placement of periods can affect the logic.

2.         Use of the scope terminator END-IF is preferred.

G.        The CONTINUE Clause

1.         The COBOL expression CONTINUE is used:

a.         to avoid performing any operation if a condition exists.

b.         to execute instructions only when the ELSE condition is met.

2.         NEXT SENTENCE is used in place of CONTINUE in COBOL 74.

3.                  When used, CONTINUE (or NEXT SENTENCE) must be the only clause following a condition, since it indicates that no action is to be performed.

II.         Selection Using Other Options of the IF Statement

A.        Nested Conditional

1.         A nested conditional is a conditional in which an IF statement itself can contain additional IF clauses.

2.         In a nested conditional, it is advisable to have each IF clause paired with an ELSE clause since this makes the nesting of conditionals less confusing.

3.         A decision table may be used to map out complex logic that requires execution of different modules depending on the results of numerous tests.

4.         Any nested conditional may be written with a series of simple conditionals.

5.         The indenting of clauses should be consistently applied when coding nested conditionals.

B.         Compound Conditional

1.         A compound conditional allows the programmer to test for several conditions with one statement.

2.         A compound conditional using OR will be true if any one of the individual conditions exists.

3.         If two or more conditions in a compound conditional contain the same operand, then the operand only needs to be coded in the first conditional.  When omitted from subsequent conditionals, the operand is called an implied operand.

4.         NOT (GREATER THAN OR EQUAL TO) is equivalent to LESS THAN.

5.         A compound conditional using AND will be true only when all of the individual conditions are true.

6.         The hierarchy rules for compound conditionals are:

a.         Conditions surrounding the word AND are evaluated first.

b.         Conditions surrounding the word OR are evaluated last.

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

d.         Parentheses override the other hierarchy rules.  All conditions within parentheses are evaluated first.

C.        Sign and Class Tests

1.         A sign test may be used to determine whether a field is POSITIVE, NEGATIVE, or ZERO.

2.         Format for the sign test:

IF identifier is {POSITIVE/NEGATIVE/ZERO}

3.         To test a field for a sign implies that it may have a negative value.  The PICTURE clause should, therefore, contain an S.

4.         A class test is used to determine the type of data in a field.

5.         Format for the class test:

IF identifier is {ALPHABETIC/NUMERIC}

6.         The class test can be helpful in trapping data-entry errors.

7.         COBOL 85 has two tests to differentiate between uppercase and lowercase alphabetic characters:

a.    ALPHABETIC-UPPER

b.    ALPHABETIC-LOWER

D.        Negating Conditionals

1.         A negated conditional is an IF statement that tests for the absence of a condition by using the word NOT before the condition.

2.         All simple relational, sign, or class tests may be coded using a negated conditional.

3.         NOT NEGATIVE is not equivalent to POSITIVE.  Likewise, NOT POSITIVE is not equivalent to NEGATIVE. They are not equivalent because ZERO is neither positive nor negative.

4.         When simplifying negated compound conditionals, DeMorgan’s Law applies:

a.         IF NOT (condition-1 OR condition-2)

is equivalent to

IF NOT condition-1 AND NOT condition-2

b.         IF NOT (condition-1 AND condition-2)

is equivalent to

IF NOT condition-1 OR NOT condition-2

5.         The hierarchy rules for statements that include negated conditionals are:

a.         NOT is evaluated first.

b.         AND (from left to right) is evaluated next.

c.                   OR (from left to right) is evaluated last.

d.         Parentheses override the other hierarchy rules.  All conditions within parentheses are evaluated first.

III.               Using IF Statements to Determine Leap Years

The following criteria determine whether a year is a leap year.  A year is a leap year if:

1.                  It is divisible by four but does not end in 00, or

2.                  It ends in 00 and is divisible by 400

IV.       Condition-Names

1.         A condition-name is a user-defined word defined in the DATA DIVISION that gives a name to a specific value or values that an identifier can assume.

2.         A condition-name is always coded on the 88 level.

3.         The 88-level item is not the name of a field but the name of a condition.

4.         An 88-level item refers to the elementary item immediately preceding it.

5.         An 88-level item has a VALUE clause.

6.         An 88-level item does not contain a PICTURE clause.

7.         The format for an 88-level item is:

88 condition-name  VALUE literal

8.         A condition-name must be unique.

9.         The VALUE clause must be a literal consistent with the data type of the field preceding the condition-name.

10.       Condition names make programs easier to read, debug, and modify.

11.       A field may contain as many 88-level items as are needed.

12.       88-level items can specify multiple values.

13.       VALUE ALL may be used to specify repeated entries for a literal.

V.        The EVALUATE Statement: Using the Case Structure as an

Alternative to Selection

1.         The EVALUATE verb is used to implement the case structure.

2.         The basic EVALUATE format:

EVALUATE {identifier-1/expression-1}

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

[WHEN OTHER imperative-statement-2]

[END-EVALUATE].

3.         If an identifier is used after the word EVALUATE, then condition-1 will be a value that the identifier may assume.

4.         If the expression TRUE is coded after the word EVALUATE, then condition-1 can be any condition or condition-name.

5.         After a condition in a WHEN is met and the corresponding imperative statements are executed, execution proceeds to the statement following END-EVALUATE.

SOLUTIONS TO REVIEW QUESTIONS

I.          True-False Questions

1.         F          Statements surrounding the word AND are evaluated first.

2.         T

3.         F          Zero is neither negative nor positive. An alphanumeric field can be

neither numeric nor alphabetic.

4.         F          In EBCDIC, numbers are greater than uppercase letters. In ASCII

however, numbers are less than uppercase letters.

5.         F          Numeric items are compared algebraically (020 = 20).  Trailing spaces do

not affect the evaluation of a nonnumeric field. (Expansion of the

smaller field according to rules of numeric and alphanumeric fields

results in fields of equal size for the comparison.)

6.         T

7.         T

8.         T

9.         F          Parentheses may be used to alter the hierarchy of operations

10.       T

II.        General Questions

1.         AMT1 is equal to AMT2

2.         AMT1 is equal to AMT2

3.         AMT1 is equal to AMT2

4.         AMT1 is equal to AMT2

5.         IF SALARY <= 84900

COMPUTE FICA ROUNDED = SALARY * 0.0765

ELSE

COMPUTE FICA ROUNDED = 84900 * 0.0765

+ (SALARY - 84900) * 0.0145

END-IF

6.         MOVE A TO HOLD-IT

IF B > HOLD-IT

MOVE B TO HOLD-IT

END-IF

IF C > HOLD-IT

MOVE C TO HOLD-IT

END-IF

IF D > HOLD-IT

MOVE D TO HOLD-IT

END-IF

7.         The statements are not equivalent.  In (a), 600-PRINT-RTN will always be performed.  In (b), 600-PRINT-RTN will only be performed if A = B.

8.         The statements are not equivalent.  In (a), 700-RTN-Y will be performed if A is zero.  In (b), 600-RTN-X will be performed if A is zero.

9.         The statements are not equivalent.  In (a), if DISCOUNT is greater than TOTAL the 500-ERR-RTN will be performed but not the subtraction.  In (b), if DISCOUNT is greater than TOTAL, the 500-ERR-RTN is performed, and the subtraction is also performed.

10.       Correct.

11.       The word IF should not be repeated.

12.       000-MAIN-MODULE.

PERFORM UNTIL ARE-THERE-MORE-RECORDS = ’NO ’

AT END

MOVE ’NO ’ TO ARE-THERE-MORE-RECORDS

NOT AT END

PERFORM 200-PROCESS-SHIPMENT

END-PERFORM.

200-PROCESS-SHIPMENT.

EVALUATE SHIPPING-WEIGHT < 30000

WHEN TRUE      MOVE 0.35 TO RATE

WHEN OTHER     MOVE 0.30 TO RATE

END-EVALUATE

COMPUTE SHIPPING-CHARGES =

RATE * SHIPPING-WEIGHT / 100.

13.       IF WEEKEND = ’YES’

IF ORCHESTRA = ’YES’

MOVE 48 TO TICKET-PRICE

ELSE

MOVE 36 TO TICKET-PRICE

END-IF

ELSE

IF ORCHESTRA = ’YES’

MOVE 44 TO TICKET-PRICE

ELSE

MOVE 24 TO TICKET-PRICE

END-IF

END-IF

DISPLAY ’TICKET PRICE = ’, TICKET-PRICE

14a.     000-MAIN-MODULE.

PERFORM UNTIL ARE-THERE-MORE-RECORDS = ’NO ’

AT END

MOVE ’NO ’ TO ARE-THERE-MORE-RECORDS

NOT AT END

PERFORM 200-PROCESS-ACCOUNT

END-PERFORM

PERFORM 300-CALCULATE-PERCENT.

200-PROCESS-ACCOUNT.

IF BALANCE-IN > 100000

END-IF.

300-CALCULATE-PERCENT.

IF ACCOUNT-CTR > 0

COMPUTE HIGH-BALANCE-PCT ROUNDED =

100 * HIGH-BALANCE-CTR / ACCOUNT-CTR

ELSE

MOVE 0 TO HIGH-BALANCE-PCT

END-IF.

14b.     In module 200-PROCESS-ACCOUNT, replace the IF statement with:

EVALUATE BALANCE-IN > 100000

WHEN TRUE       ADD 1 TO HIGH-BALANCE-CTR

END-EVALUATE.

15.       MOVE ZERO TO COUNTER.

PERFORM 400-TABLE-RTN 10 TIMES.

.

.

.

400-TABLE-RTN.

MOVE COUNTER TO POS1-OUT

MULTIPLY COUNTER BY 2 GIVING POS2-OUT

MULTIPLY COUNTER BY 3 GIVING POS3-OUT

MULTIPLY COUNTER BY 4 GIVING POS4-OUT

MULTIPLY COUNTER BY 5 GIVING POS5-OUT

MULTIPLY COUNTER BY 6 GIVING POS6-OUT

MULTIPLY COUNTER BY 7 GIVING POS7-OUT

MULTIPLY COUNTER BY 8 GIVING POS8-OUT

MULTIPLY COUNTER BY 9 GIVING POS9-OUT

MULTIPLY COUNTER BY 10 GIVING POS10-OUT

WRITE OUTPUT-REC FROM TABLE-LINE

16.       900-FINISH will be performed in cases b, e, g, and h.

17.       DIVIDE 2 INTO FLDA REMAINDER ANSWER

MOVE ’EVEN’ TO RESULT-OUT

ELSE

MOVE ’ODD’ TO RESULT-OUT

END-IF.

18.       WORKING-STORAGE SECTION field definitions:

01  DATE-FIELDS.

05  BIRTH-DATE.

10  BIRTH-YEAR          PIC 9(4).

10  BIRTH-MONTH         PIC 9(2).

10  BIRTH-DAY           PIC 9(2).

05  TODAYS-DATE.

10  TODAYS-YEAR         PIC 9(4).

10  TODAYS-MONTH        PIC 9(2).

10  TODAYS-DAY          PIC 9(2).

10                      PIC X(13).

PROCEDURE DIVISION code:

100-DETERMINE-WHETHER-UNDER-25.

DISPLAY 'ENTER A BIRTH DATE (YYYYMMDD): '

ACCEPT BIRTH-DATE

MOVE FUNCTION CURRENT-DATE TO TODAYS-DATE

EVALUATE TRUE

WHEN TODAYS-YEAR - BIRTH-YEAR < 25

DISPLAY 'THIS PERSON IS UNDER 25'

WHEN TODAYS-YEAR - BIRTH-YEAR = 25

PERFORM 200-COMPARE-MONTHS

WHEN TODAYS-YEAR - BIRTH-YEAR > 25

DISPLAY 'THIS PERSON IS AT LEAST 25'

END-EVALUATE.

200-COMPARE-MONTHS.

EVALUATE TRUE

WHEN BIRTH-MONTH < TODAYS-MONTH

DISPLAY 'THIS PERSON IS AT LEAST 25'

WHEN BIRTH-MONTH = TODAYS-MONTH

PERFORM 300-COMPARE-DAYS

WHEN BIRTH-MONTH > TODAYS-MONTH

DISPLAY 'THIS PERSON IS UNDER 25'

END-EVALUATE.

300-COMPARE-DAYS.

EVALUATE TRUE

WHEN BIRTH-DAY < TODAYS-DAY

DISPLAY 'THIS PERSON IS AT LEAST 25'

WHEN BIRTH-DAY = TODAYS-DAY

DISPLAY 'HAPPY BIRTHDAY!'

WHEN BIRTH-DAY > TODAYS-DAY

DISPLAY 'THIS PERSON IS UNDER 25'

END-EVALUATE.

19.       WORKING-STORAGE SECTION field definitions:

01  DATE-FIELDS.

05  DATE-OF-LAST-PAYMENT            PIC 9(8).

05  INTEGER-DATE-OF-LAST-PAYMENT    PIC 9(8).

05  DATE-PAYMENT-IS-DUE             PIC 9(8).

05  INTEGER-DATE-PAYMENT-IS-DUE     PIC 9(8).

05  SYSTEM-DATE-AND-TIME.

10  SYSTEM-CURRENT-DATE         PIC 9(8).

10                              PIC X(13).

PROCEDURE DIVISION code:

DISPLAY 'ENTER THE DATE OF LAST PAYMENT (YYYYMMDD): '

ACCEPT DATE-OF-LAST-PAYMENT

MOVE FUNCTION INTEGER-OF-DATE (DATE-OF-LAST-PAYMENT)

TO INTEGER-DATE-OF-LAST-PAYMENT

GIVING INTEGER-DATE-PAYMENT-IS-DUE

MOVE FUNCTION DATE-OF-INTEGER (INTEGER-DATE-PAYMENT-IS-DUE)

TO DATE-PAYMENT-IS-DUE

MOVE FUNCTION CURRENT-DATE TO SYSTEM-DATE-AND-TIME

IF DATE-PAYMENT-IS-DUE < SYSTEM-CURRENT-DATE

DISPLAY 'THE PAYMENT IS OVERDUE'

ELSE

IF DATE-PAYMENT-IS-DUE = SYSTEM-CURRENT-DATE

DISPLAY 'THE PAYMENT IS DUE TODAY'

ELSE

DISPLAY 'THE PAYMENT IS NOT YET DUE'

END-IF

END-IF.

20.       WORKING-STORAGE SECTION field definitions:

01  DATE-FIELDS.

05  DATE-OF-PURCHASE                PIC 9(8).

05  INTEGER-DATE-OF-PURCHASE        PIC 9(8).

05  DUE-DATE                        PIC 9(8).

05  INTEGER-DUE-DATE                PIC 9(8).

PROCEDURE DIVISION code:

DISPLAY 'ENTER THE DATE OF PURCHASE (YYYYMMDD): '

ACCEPT DATE-OF-PURCHASE

MOVE FUNCTION INTEGER-OF-DATE (DATE-OF-PURCHASE)

TO INTEGER-DATE-OF-PURCHASE

ADD 30 TO INTEGER-DATE-OF-PURCHASE GIVING INTEGER-DUE-DATE

MOVE FUNCTION DATE-OF-INTEGER (INTEGER-DUE-DATE)

TO DUE-DATE

DISPLAY 'THE PAYMENT IS DUE ON ' DUE-DATE.

III.       Internet/Critical Thinking Questions

##### Search Engine:  yahoo.com>Computers & Internet

Keywords:                   “Julian date”

Contents:                      What Is a Julian date (a definition).  Includes links to other web sites.

##### Search Engine:  yahoo.com>Computers & Internet

Keywords:                   “Julian date”

Contents:                      Date Algorithms

Search Engine:  yahoo.com>Computers & Internet

Keywords:                   “Julian date”

Contents:                      Julian date definition and converter

##### Search Engine:  hotbot.com

Keywords:                   COBOL + “intrinsic functions”

URL:                            http://www.cis.ohio-state.edu/~sgomori/314/intrinsic.html

Contents:                      Discussion of Date Functions and others.  Has link for Julian dates.

SOLUTIONS TO DEBUGGING EXERCISES

1.         A record is written if AMT1 is not equal to 5400.

2.         The period after ADD AMT3 TO TOTAL will cause a syntax error.

3.         a.         An alphanumeric field cannot be tested with a sign test.

b.         There is no syntax error, although it is not appropriate to compare an alphanumeric field to a numeric literal.  800-RTN-X will always execute.  If A = 6, it is not equal to 7 and thus the statement is true. If A = 7, it is not equal to 6 and thus the statement is true.

c.         A syntax error will not occur.  A run-time error, resulting in a program interrupt would occur if the initial contents of B was not numeric.