Chapter 8 l.jpg
This presentation is the property of its rightful owner.
Sponsored Links
1 / 26

CHAPTER 8 PowerPoint PPT Presentation


  • 63 Views
  • Uploaded on
  • Presentation posted in: General

CHAPTER 8. Decision Making Using the IF and EVALUATE Statements. OBJECTIVES. To familiarize you with The use of IF statements for selection. The variety of formats and options available with the conditional statement. The use of the EVALUATE statement with COBOL 85.

Download Presentation

CHAPTER 8

An Image/Link below is provided (as is) to download presentation

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 8 l.jpg

CHAPTER 8

Decision Making Using theIF and EVALUATE Statements

From Stern & Stern


Objectives l.jpg

OBJECTIVES

To familiarize you with

  • The use of IF statements for selection.

  • The variety of formats and options available with the conditional statement.

  • The use of the EVALUATE statement with COBOL 85.

  • The use of condition-names.

From Stern & Stern


The structure theorem l.jpg

The Structure Theorem

  • It is possible to write any computer program by using only 3 basic control structures that are EASILY represented in pseudocode:

    • sequence

    • selection

    • repetition-iteration

    • case – a special type of selection

From Stern & Stern


Selection control structure l.jpg

Selection Control Structure

  • The computer ‘makes a decision.’

  • NO! The computer evaluates a condition and chooses one of two paths -- then or else -- depending on whether the condition is true or false.

From Stern & Stern


Selection control structure5 l.jpg

IF a condition is trueTHEN

statement a

statement b

statement c

ELSE (the condition is false)

statement d

statement e

statement f

ENDIF

When true, statements a, b, c are executed in sequence and statements d, e, f are skipped over.

ELSE part is not required.

Any of the statements could be another IF-THEN-ELSE!

Selection Control Structure

From Stern & Stern


Selection control structure6 l.jpg

Selection Control Structure

  • Conditions for comparison are usually expressed with one (or more) relational operators

    • < IS LESS THAN

    • > IS GREATER THAN

    • = IS EQUAL TO

    • <= IS LESS THAN OR EQUAL TO

    • >= IS GREATER THAN OR EQUAL TO

    • <>NOT EQUAL TO

From Stern & Stern


Simple selection l.jpg

Simple Selection

A choice between 2 alternative paths

IF account_balance > 300.00

THEN

COMPUTE service_charge = 5.00

ELSE

MOVE 2.00 TO service_charge

ENDIF

From Stern & Stern


Simple selection8 l.jpg

Simple Selection

  • Variation of simple IF structure: null ELSE statement.

    IF acct-bal > 100.00

    THEN

    ADD fin-chrg TO acct-bal

    ENDIF

From Stern & Stern


Basic conditional statements l.jpg

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.

From Stern & Stern


Debugging tip l.jpg

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.

From Stern & Stern


Ending conditional sentences with a period or an end if scope terminator l.jpg

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

With COBOL 85 you should use the END-IF 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

MOVE 0 TO ITEM2.

From Stern & Stern


Combined selection l.jpg

Combined Selection

  • Use Logical Operators AND or OR to evaluate multiple conditions.

  • Both conditions must be true if AND

  • Either condition may be true if OR

  • Use parentheses to avoid ambiguity, especially when using AND and OR in the same statement.

From Stern & Stern


Combined selection13 l.jpg

Combined Selection

IF student_attendance = part_time AND student_gender = female THEN

add 1 to fem_part_time_count

ENDIF

IF student_attendance = part_time OR student_gender = female THEN

add 1 to fem_part_time_count

ENDIF

From Stern & Stern


Combined selection14 l.jpg

Combined Selection

IF record_code = ’23’ OR update_code = delete AND acct_bal = zero THEN

Delete customer record

ENFIF

IF (record_code = ’23’ OR update_code = delete) AND acct_bal = zero THEN

Delete customer record

ENDIF

From Stern & Stern


Compound conditional l.jpg

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.

From Stern & Stern


And in a compound conditional l.jpg

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:

From Stern & Stern


Hierarchy rules for compound conditionals l.jpg

HIERARCHY RULES FOR COMPOUND CONDITIONALS

1. Conditions surrounding the word AND are evaluated first.

2. Conditions surrounding the word OR are evaluated last.

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.

From Stern & Stern


Implied operands l.jpg

Allowed:

If TOTAL = 7 OR 8

ADD 1 TO COUNT

END-IF

Implied operands can be difficult to debug.

Preferred:

IF TOTAL = 7 OR TOTAL = 8

ADD 1 to COUNT

END-IF

NOTE: I LIKE THIS

Implied Operands

From Stern & Stern


Nested selection non linear l.jpg

Nested Selection – Non-linear

IF student_attendance = part_time THEN

IF student_gender = female THEN

IF student_age > 21 THEN

Add 1 to mature_fem_pt_students

ELSE

Add 1 to young_fem_pt_students

ENDIF

ELSE

Add 1 to male_pt_students

ENDIF

ELSE

Add 1 to full_time_students

ENDIF

From Stern & Stern


Nested selection linear l.jpg

IF tax-code IS EQUAL TO 1

THEN

COMPUTE tax_rate = .03

ELSE

IF tax_code = 2

THEN

MOVE .043 TO tax_rate

ELSE

IF tax_code = 3

THEN

MOVE .07 TO tax_rate

ELSE

MOVE .10 TO tax_rate

ENDIF

ENDIF

ENDIF

EVALUATE tax-code

WHEN 1

MOVE .03 TO tax-rate

WHEN 2

MOVE .043 to tax-rate

WHEN 3

MOVE .07 to tax-rate

WHEN OTHER

MOVE .10 to tax-rate

END-EVALUATE

Nested Selection - Linear

From Stern & Stern


The continue clause l.jpg

The CONTINUE 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.

From Stern & Stern


The continue clause22 l.jpg

Acceptable:

IF AMT1 = AMT2

CONTINUE

ELSE

ADD 1 TO TOTAL

END-IF

The first path should be TRUE; skip the ELSE part.

Preferred:

IF AMT1 IS NOT EQUAL TO AMT 2

ADD 1 TO TOTAL

END-IF

-or-

IF NOT (AMT1 = AMT2)

THEN

ADD 1 TO TOTAL

END-IF

The CONTINUE Clause

From Stern & Stern


Condition names l.jpg

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 PICTURE clause.

From Stern & Stern


Condition names24 l.jpg

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.

From Stern & Stern


Condition names25 l.jpg

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

From Stern & Stern


Condition names26 l.jpg

CONDITION-NAMES

01 STUDENT-CODE PIC 9.

88 UNDERGRAD VALUE 1 THRU 4.

88 GRAD VALUE 3 THRU 6.

88 POST-BAC VALUE 7 8.

  • TEMP VALUE 9.

    IF UNDERGRAD

    PERFORM 300-FINANCIAL-AID

    ELSE

    PERFORM 350-SPECIAL-FIN-AID

    END-IF

From Stern & Stern


  • Login