chapter 8
Download
Skip this Video
Download Presentation
CHAPTER 8

Loading in 2 Seconds...

play fullscreen
1 / 26

CHAPTER 8 - PowerPoint PPT Presentation


  • 87 Views
  • Uploaded on

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.

loader
I am the owner, or an agent authorized to act on behalf of the owner, of the copyrighted work described.
capcha
Download Presentation

PowerPoint Slideshow about 'CHAPTER 8' - sezja


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

Decision Making Using theIF and EVALUATE Statements

From Stern & Stern

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

ad