## Slide #1.

CHAPTER 8 Decision Making Using the IF and EVALUATE Statements March 12, 2002 From Stern & Stern 1
More slides like this

## Slide #2.

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. March 12, 2002 From Stern & Stern 2
More slides like this

## Slide #3.

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 March 12, 2002 From Stern & Stern 3
More slides like this

## Slide #4.

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. March 12, 2002 From Stern & Stern 4
More slides like this

## Slide #5.

Selection Control Structure IF a condition is true THEN statement a statement b statement c ELSE (the condition is false) statement d statement e statement f ENDIF March 12, 2002 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 IFTHEN-ELSE! From Stern & Stern 5
More slides like this

## Slide #6.

Selection Control Structure Conditions for comparison are usually expressed with one (or more) relational operators < > = <= >= <> March 12, 2002 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 6
More slides like this

## Slide #7.

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 March 12, 2002 From Stern & Stern 7
More slides like this

## Slide #8.

Simple Selection Variation of simple IF structure: null ELSE statement. IF acct-bal > 100.00 THEN ADD fin-chrg TO acct-bal ENDIF March 12, 2002 From Stern & Stern 8
More slides like this

## Slide #9.

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. March 12, 2002 From Stern & Stern 9
More slides like this

## Slide #10.

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. March 12, 2002 From Stern & Stern 10
More slides like this

## Slide #11.

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. March 12, 2002 From Stern & Stern 11
More slides like this

## Slide #12.

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. March 12, 2002 From Stern & Stern 12
More slides like this

## Slide #13.

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 March 12, 2002 From Stern & Stern 13
More slides like this

## Slide #14.

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 March 12, 2002 From Stern & Stern 14
More slides like this

## Slide #15.

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. March 12, 2002 From Stern & Stern 15
More slides like this

## Slide #16.

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: March 12, 2002 From Stern & Stern 16
More slides like this

## Slide #17.

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 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. March 12, 2002 From Stern & Stern 17
More slides like this

## Slide #18.

Implied Operands Allowed: If TOTAL = 7 OR 8 ADD 1 TO COUNT Preferred: IF TOTAL = 7 OR TOTAL = 8 ADD 1 to COUNT END-IF END-IF Implied operands can be difficult to debug. March 12, 2002 NOTE: I LIKE THIS From Stern & Stern 18
More slides like this

## Slide #19.

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 March 12, 2002 From Stern & Stern 19
More slides like this

## Slide #20.

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 March 12, 2002 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 From Stern & Stern 20
More slides like this

## Slide #21.

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. March 12, 2002 From Stern & Stern 21
More slides like this

## Slide #22.

The CONTINUE Clause Acceptable: Preferred: IF AMT1 = AMT2 CONTINUE ELSE ADD 1 TO TOTAL END-IF IF AMT1 IS NOT EQUAL TO AMT 2 ADD 1 TO TOTAL END-IF -orIF NOT (AMT1 = AMT2) THEN ADD 1 TO TOTAL END-IF The first path should be TRUE; skip the ELSE part. March 12, 2002 From Stern & Stern 22
More slides like this

## Slide #23.

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. March 12, 2002 From Stern & Stern 23
More slides like this

## Slide #24.

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 WORKINGSTORAGE may have a condition-name associated with it. March 12, 2002 From Stern & Stern 24
More slides like this

## Slide #25.

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'. March 12, 2002 From Stern & Stern 25
More slides like this

## Slide #26.

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. 88 TEMP VALUE 9. IF UNDERGRAD PERFORM 300-FINANCIAL-AID ELSE PERFORM 350-SPECIAL-FIN-AID END-IF March 12, 2002 From Stern & Stern 26
More slides like this