1 / 26

CHAPTER 8

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.

sezja
Télécharger la présentation

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. Content is provided to you AS IS for your information and personal use only. Download presentation by click this link. While downloading, if for some reason you are not able to download a presentation, the publisher may have deleted the file from their server. During download, if you can't get a presentation, the file might be deleted by the publisher.

E N D

Presentation Transcript


  1. CHAPTER 8 Decision Making Using theIF and EVALUATE Statements From Stern & Stern

  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. From Stern & Stern

  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 From Stern & Stern

  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. From Stern & Stern

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

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

  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 From Stern & Stern

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

  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. From Stern & Stern

  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. From Stern & Stern

  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. From Stern & Stern

  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. From Stern & Stern

  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 From Stern & Stern

  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 From Stern & Stern

  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. From Stern & Stern

  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: From Stern & Stern

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

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

  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 From Stern & Stern

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

  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. From Stern & Stern

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

  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. From Stern & Stern

  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 WORKING-STORAGE may have a condition-name associated with it. From Stern & Stern

  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'. From Stern & Stern

  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. • TEMP VALUE 9. IF UNDERGRAD PERFORM 300-FINANCIAL-AID ELSE PERFORM 350-SPECIAL-FIN-AID END-IF From Stern & Stern

More Related