Custom Search

cobol decision Making. cobol program must be able to make decisions about data and to execute,arithmetic statements,cobol elements, ondition checking

Decision Making

A program must be able to make decisions about data and to execute different sections of code based on those decisions. Controlling the flow of programs by testing conditions with the IF statement lies at the heart of every program.

This lesson deals almost exclusively with the IF statement and the many options available with it--information critical to understanding programming in COBOL. Today, you learn about the following topics:

· What is IF?

· Using IF to control multiple statements.

· What can you test with an IF?

· Testing multiple conditions.

· Using IF-ELSE.

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:

IF condition

PERFORM DO-SOMETHING.

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.

The example in Listing 4.1 uses an IF to decide which message to display. In GET-THE-ANSWER, at line 002300, this program prompts the user to enter Y or N (Yes or No) and accepts a single character from the keyboard and places it in the variable YES-OR-NO. This is not a particularly good program because if the user enters a lowercase y or n, the program does nothing at all. The problem of handling the lowercase entry is addressed later in this chapter. The general problem of handling lowercase versus uppercase data entry is covered in Day 15, "Data Integrity." For now, just press the Caps Lock key on the left of your keyboard to force all data entry into uppercase.

TYPE: Listing 4.1. Testing values using IF.

000100 IDENTIFICATION DIVISION.

000200 PROGRAM-ID. YESNO01.

000300*--------------------------------------------------

000400* This program asks for a Y or N answer, and then

000500* displays whether the user chose yes or no.

000600*--------------------------------------------------

000700 ENVIRONMENT DIVISION.

000800 DATA DIVISION.

000900 WORKING-STORAGE SECTION.

001000

001100 01 YES-OR-NO PIC X.

001200

001300 PROCEDURE DIVISION.

001400 PROGRAM-BEGIN.

001500

001600 PERFORM GET-THE-ANSWER.

001700

001800 PERFORM DISPLAY-THE-ANSWER.

001900

002000 PROGRAM-DONE.

002100 STOP RUN.

002200

002300 GET-THE-ANSWER.

002400

002500 DISPLAY "Is the answer Yes or No? (Y/N)".

002600 ACCEPT YES-OR-NO.

002700

002800 DISPLAY-THE-ANSWER.

002900 IF YES-OR-NO IS EQUAL "Y"

003000 DISPLAY "You answered Yes.".

003100

003200 IF YES-OR-NO IS EQUAL "N"

003300 DISPLAY "You answered No.".

003400

This is the output of yesno01.cbl if you enter a Y:

OUTPUT:

Is the answer Yes or No? (Y/N)

Y

You answered Yes.

C>

C>

ANALYSIS: Edit, compile, and run this program; then try it, entering a few different answers. You will notice that it displays a message only if the entry is an uppercase Y or N. When you are comparing alphanumeric variables, the values are case-dependent, so y is not the same as Y, and n is not the same as N.


DO/DON'T:
DO
test for both uppercase and lowercase versions of an alphanumeric field, if either uppercase or lowercase values are valid.

DON'T ignore case differences in a variable if they are important in a program.


In DISPLAY-THE-ANSWER, at lines 002800 through 003300, one of two possible messages is displayed, based on whether the user entered a Y or an N.

At line 002900, the condition being tested is YES-OR-NO IS EQUAL "Y". IS EQUAL are COBOL reserved words used for testing whether two values are equal. The IF sentences in DISPLAY-THE-ANSWER at lines 002900 and 003200 are each two lines long; there is no period until the end of the second line.

When the criteria of a tested condition are met, the condition is considered to be true. When the criteria of a tested condition are not met, the condition is considered to be false. The DISPLAY statement at line 003000 is executed only when the condition being tested by the IF at line 002900 (YES-OR-NO IS EQUAL "Y") is true. When the IF at line 002900 is not true (any character but Y is entered), line 003000 is skipped. The DISPLAY statement at line 003300 is executed only when the condition being tested by the IF at line 003200 (YES-OR-NO IS EQUAL "N") is true. When the IF at line 003200 is not true (any character but N is entered), line 003300 is skipped. When a condition tested by an IF statement is not true, any statements controlled by the IF are not executed.

Depending on the user's input, there are three possible output results from this program:

· If YES-OR-NO contains an N when DISPLAY-THE-ANSWER is performed, the IF test at line 002900 is not true, and line 003000 is not executed. However, the IF test at line 003200 is true, and line 003300 is executed.

· If YES-OR-NO contains a Y when DISPLAY-THE-ANSWER is performed, the IF test at line 002900 is true, and line 003000 is executed. However, the IF test at line 003200 is false, and line 003300 is not executed.

· If YES-OR-NO does not contain a Y or an N when DISPLAY-THE-ANSWER is performed, the IF test at line 002900 is false, and line 003000 is not executed. The IF test at line 003200 also is false, and line 003300 is not executed. In this case, neither message is displayed.

Listing 4.2 adds the extra step of editing the user's answer to adjust for a lowercase y or n.

TYPE: Listing 4.2. Editing the answer.

000100 IDENTIFICATION DIVISION.

000200 PROGRAM-ID. YESNO02.

000300*--------------------------------------------------

000400* This program asks for a Y or N answer, and then

000500* displays whether the user chose yes or no.

000600* The edit logic allows for entry of Y, y, N, or n.

000700*--------------------------------------------------

000800 ENVIRONMENT DIVISION.

000900 DATA DIVISION.

001000 WORKING-STORAGE SECTION.

001100

001200 01 YES-OR-NO PIC X.

001300

001400 PROCEDURE DIVISION.

001500 PROGRAM-BEGIN.

001600

001700 PERFORM GET-THE-ANSWER.

001800

001900 PERFORM EDIT-THE-ANSWER.

002000

002100 PERFORM DISPLAY-THE-ANSWER.

002200

002300 PROGRAM-DONE.

002400 STOP RUN.

002500

002600 GET-THE-ANSWER.

002700

002800 DISPLAY "Is the answer Yes or No? (Y/N)".

002900 ACCEPT YES-OR-NO.

003000

003100 EDIT-THE-ANSWER.

003200

003300 IF YES-OR-NO IS EQUAL "y"

003400 MOVE "Y" TO YES-OR-NO.

003500

003600 IF YES-OR-NO IS EQUAL "n"

003700 MOVE "N" TO YES-OR-NO.

003800

003900 DISPLAY-THE-ANSWER.

004000 IF YES-OR-NO IS EQUAL "Y"

004100 DISPLAY "You answered Yes.".

004200

004300 IF YES-OR-NO IS EQUAL "N"

004400 DISPLAY "You answered No.".

004500

ANALYSIS: In EDIT-THE-ANSWER at line 003300, the program checks to see whether the user entered a y. If true, at line 003400 the program forces this to become a Y. In the same paragraph at lines 003600 and 003700, an n will be changed to an N.

The tests in DISPLAY-THE-ANSWER work correctly now, because the answer has been forced to uppercase Y or N by the EDIT-THE-ANSWER paragraph.

If you edit, compile, and run yesno02.cbl, you will find that uppercase and lowercase versions of y and n are now all valid entries. The program still displays no message if anything else is entered. (I will address this problem later in this chapter, in the section entitled IF-ELSE.)