1 / 23

Chapter 3

Chapter 3. Decision Making. What is IF?. The primary method of changing the flow of a program is by making decisions using the IF verb. The following example demonstrates the IF verb: Example IF condition PERFORM DO-SOMETHING. What is IF?.

eryk
Download Presentation

Chapter 3

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 3 Decision Making

  2. What is IF? • The primary method of changing the flow of a program is by making decisions using the IF verb. The following example demonstrates the IF verb: • Example IF condition PERFORM DO-SOMETHING.

  3. What is IF? • When COBOL sees an IF, it makes a decision about the condition, and then either requests a PERFORM of DO-SOMETHING or skips that line of the program.

  4. What is IF? IDENTIFICATION DIVISION. PROGRAM-ID. YESNO01. DATA DIVISION. WORKING-STORAGE SECTION. 01 YES-OR-NO PIC X. PROCEDURE DIVISION. PERFORM GET-THE-ANSWER. PERFORM DISPLAY-THE-ANSWER. STOP RUN. GET-THE-ANSWER. DISPLAY "Is the answer Yes or No? (Y/N)". ACCEPT YES-OR-NO. DISPLAY-THE-ANSWER. IF YES-OR-NO IS EQUAL "Y“ DISPLAY "You answered Yes.". IF YES-OR-NO IS EQUAL "N“ DISPLAY "You answered No.“.

  5. Using IF to control multiple statements IF condition PERFORM DO-SOMETHING PERFORM DO-SOMETHING-ELSE. • An IF controls all statements under it until the sentence ends. • When an IF tests true, all statements up to the next period are executed.

  6. Using IF to control multiple statements DISPLAY-THE-ANSWER. IF YES-OR-NO IS EQUAL "Y“ PERFORM IT-IS-VALID DISPLAY "You answered Yes.". IF YES-OR-NO IS EQUAL "N“ PERFORM IT-IS-VALID DISPLAY "You answered No.".

  7. What Can You Test with IF? • The symbols used to compare two values are called comparison operators. • The short and long versions of these comparisons are all comparison operators. • IS NOT EQUAL, NOT =, =, IS EQUAL, NOT <, >, GREATER THAN, and NOT GREATER THAN are all examples of comparison operators.

  8. COBOL comparison operators.

  9. COBOL comparison operators.

  10. COBOL comparison operators. IF YES-OR-NO = "y“ MOVE "Y" TO YES-OR-NO. IF YES-OR-NO = "n“ MOVE "N" TO YES-OR-NO.

  11. COBOL comparison operators. IF YES-OR-NO = "Y“ PERFORM IT-IS-VALID DISPLAY "You answered Yes.". IF YES-OR-NO = "N“ PERFORM IT-IS-VALID.

  12. COBOL comparison operators. • For numeric values. Less than and greater than are both conditions that easily can be established when you are testing two numbers. • When a condition test is performed on alphanumeric variables, the tests usually compare the characters in the two alphanumeric values on the left and right sides of the comparison operator, in ASCII order.

  13. COBOL comparison operators. • In ASCII order, • A is less than B, AB is less than ABC, and the uppercase letters are less than the lowercase letters; so, ABC is less than abc. • digits 0 through 9, the digits are less than the characters, so 1BC is less than ABC. • Spaces are the lowest of all, so three spaces are less than 00A. • The complement of GREATER THAN is LESS THAN OR EQUAL

  14. IF Statement • Indent IF conditions carefully. • An IF controls all statements up to the period at the end of the sentence. • Correct indentation gives a good visual clue of which parts of the program are controlled by the IF. • DON'T use sloppy indenting on an IF.

  15. IF Statement DISPLAY "The words sorted in ASCII order are:". IF WORD-1 < WORD-2 DISPLAY WORD-1 DISPLAY WORD-2. IF WORD-1 NOT < WORD-2 DISPLAY WORD-2 DISPLAY WORD-1.

  16. IF Statement DISPLAY "The words sorted in ASCII order are:". IF WORD-1 < WORD-2 DISPLAY WORD-1 DISPLAY WORD-2. IF WORD-1 NOT < WORD-2 DISPLAY WORD-2 DISPLAY WORD-1.

  17. Testing Multiple Conditions • An IF test also can be used to test more than one condition. • Conditions can be combined by using AND, OR, or combinations of both. • E.g. IF MENU-PICK < 1 OR MENU-PICK > 3 DISPLAY "Invalid selection". • Note: If the entered MENU-PICK is less than 1 or greater than 3, it is invalid. • The OR combines the two tests within one IF. An OR test is true if either of the tests is true.

  18. Testing Multiple Conditions • An AND test is true only if both conditions being tested are true. • The valid range of entries for the statement below is 011 through 099. Excluding 10 and 100. IF THE-NUMBER > 10 AND THE-NUMBER < 100 DISPLAY "The number is in range.".

  19. IF-ELSE • When an IF test fails, none of the statements controlled by the IF test are executed. The program continues to the next sentence and skips all the logic. • If you are testing a condition and you want to do one set of commands if the condition or conditions are true and another set if they are false, it is easier to use ELSE.

  20. IF-ELSE Example IFA < B PERFORM ACTION-A PERFORM ACTION-B ELSE PERFORM ACTION-C PERFORM ACTION-D. period

  21. IF-ELSE • When the IF condition is true, all statements up to the ELSE are executed. • Otherwise, all statements from the ELSE to the closing period are executed. • The period is placed at the end of the last statement in the ELSE.

  22. Quiz • In the following paragraph DECIDE-WHAT-TO-DO, which lines are executed when THE-NUMBER equals 7? DECIDE-WHAT-TO-DO. IF THE-NUMBER = 7 OR THE-NUMBER < 4 PERFORM ACTION-1 PERFORM ACTION-2 ELSE PERFORM ACTION-3. • Which lines are executed when THE-NUMBER equals 6? • Which lines are executed when THE-NUMBER equals 2? • Which lines are executed when THE-NUMBER equals 4?

  23. IF-ELSE • Reading Chapter 4 of text.

More Related