CHAPTER 8

1 / 26

# CHAPTER 8 - PowerPoint PPT Presentation

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.

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

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

Decision Making Using theIF and EVALUATE Statements

From Stern & Stern

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

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

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 Selection
• Variation of simple IF structure: null ELSE statement.

IF acct-bal > 100.00

THEN

ENDIF

From Stern & Stern

Basic Conditional Statements

IF AMT1 IS EQUAL TO AMT2

MOVE NAME-IN TO NAME-OUT

MOVE DESCRIPTION-IN TO

DESCRIPTION-OUT

ELSE

END-IF.

From Stern & Stern

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

With COBOL 85 you should use the END-IF as a scope terminator to establish the specific boundaries:

IF PRICE1 IS LESS THAN PRICE2

MOVE 2 TO ITEM1

ELSE

END-IF

MOVE 0 TO ITEM2.

From Stern & Stern

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 Selection

IF student_attendance = part_time AND student_gender = female THEN

ENDIF

IF student_attendance = part_time OR student_gender = female THEN

ENDIF

From Stern & Stern

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

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

Allowed:

If TOTAL = 7 OR 8

END-IF

Implied operands can be difficult to debug.

Preferred:

IF TOTAL = 7 OR TOTAL = 8

END-IF

NOTE: I LIKE THIS

Implied Operands

From Stern & Stern

Nested Selection – Non-linear

IF student_attendance = part_time THEN

IF student_gender = female THEN

IF student_age > 21 THEN

ELSE

ENDIF

ELSE

ENDIF

ELSE

ENDIF

From Stern & Stern

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

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

Acceptable:

IF AMT1 = AMT2

CONTINUE

ELSE

END-IF

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

Preferred:

IF AMT1 IS NOT EQUAL TO AMT 2

END-IF

-or-

IF NOT (AMT1 = AMT2)

THEN

END-IF

The CONTINUE Clause

From Stern & Stern

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