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

presentation player overlay
1 / 61
About This Presentation
Transcript and Presenter's Notes

Title: CHAPTER 8 Decision Making Using the IF and EVALUATE Statements


1
CHAPTER 8Decision Making Using theIF and
EVALUATEStatements
2
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.

3
Selection
  • Selection Using
  • Simple Conditional
  • Nested Conditional
  • Compound Conditional
  • Sign and Class Tests
  • Negating Conditionals
  • Evaluate
  • Condition Names (Named Conditions)

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

5
Basic Conditional Statements
  • Format for IF statements
  • IF condition-1 THEN
  • imperative statement-1 . . .
  • ELSE
  • imperative statement-2 . . .
  • END-IF

6
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

7
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

8
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

9
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

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

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

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

13
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
  • lt IS LESS THAN
  • gt IS GREATER THAN
  • IS EQUAL TO
  • lt IS LESS THAN OR EQUAL TO
  • gt IS GREATER THAN OR EQUAL TO

14
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-IF CODE-IN should be
    a nonnumeric field, since it is compared to a
    nonnumeric literal.
  • IF CTR1 CTR2 THEN ADD AMT1 TO
    TOTALEND-IF

15
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

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

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

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

19
ASCII and EBCDIC Collating Sequences
  • COLLATING SEQUENCES
  • EBCDIC ASCII
  • Low Spaces Spaces
    Special characters Special
    characters a-z 0-9
    A-Z A-Z
  • High 0-9 a-z

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

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

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

23
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
24
SELF-TEST
  • 3. IF A EQUALS B
    MOVE 1
    TO A
    END-IF

Solution This should be IF A IS EQUAL TO B ....
25
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.
26
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.
27
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.
28
Selection Using Other Options of the IF Statement
29
NESTED IFs
30
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

31
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

32
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

33
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

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

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

36
OR
37
Compound ConditionalOR
  • Examples
  • 1. IF AMT1 AMT2 OR AMT2 gt AMT3 (IF AMT1
    AMT2 OR gt AMT3)
  • PERFORM 500-TOTAL-RTN
  • END-IF.
  • 2. IF AMT1 lt 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.

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

39
AND
40
Compound ConditionalAND
  • Examples
  • 1. IF AMT1 AMT2 AND AMT2 gt AMT3 (IF AMT1
    AMT2 AND gt AMT3)
  • PERFORM 500-TOTAL-RTN
  • END-IF.
  • 2. IF AMT1 lt 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.

41
Compound Conditional Format
  • IF condition-1 OR AND condition-2 THEN
  • statement-1 . . .
  • CONTINUE
  • ELSE
  • statement-2 ...
  • END-IF

42
HIERARCHY RULES FOR COMPOUND
CONDITIONALS
  • 1. Conditions surrounding the word AND are
    evaluated first.
  • 2. Conditions surrounding the word OR are
    evaluated last.

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

44
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

45
SOLUTION
FALSE TRUE IF (C
D and E F) or A B THEN PERFORM
600-PARA-1END-IFFALSE or TRUE
true
46
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.

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

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

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

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

51
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

52
Sign and Class Tests
  • The three alphabetic class tests for COBOL 85
    are
  • Reserved Word Meaning
  • ALPHABETIC A-Z, a-z, and
    blank
  • ALPHABETIC-UPPER A-Z and blank
  • ALPHABETIC-LOWER a-z and blank

53
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

54
CONDITION-NAMES
  • 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.

55
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 PIC XX.
  • 88 STATUS-OK VALUE '12'.IF
    STATUS-OK is equivalent toIF CODE-IN
    12

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

57
The COBOL 85 EVALUATE Statement Using the Case
Structure as an Alternative to Selection
58
The COBOL 85 EVALUATE Statement
  • Format
  • EVALUATE identifier-1
  • expression-1
  • WHEN condition-1 imperative-
    statement-1 . . .
  • WHEN OTHER imperative-statement-2
  • END-EVALUATE

59
EVALUATE
  • EVALUATE STATE-CODE 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

60
EVALUATE (format 2)
  • EVALUATE TRUE WHEN GENDER M WHEN
    MARRIED S WHEN AGE gt 21 WHEN
    OTHEREND-EVALUATE
  • alternative to nested IFs or a series of IF
    statements.

61
QUESTIONS?
Write a Comment
User Comments (0)
About PowerShow.com