Computer Programs: Computer Programs Are Divided Into Two Types: Operating Systems Programs

Total Page:16

File Type:pdf, Size:1020Kb

Computer Programs: Computer Programs Are Divided Into Two Types: Operating Systems Programs

CPAN 320 COBOL Lecture 9

Decision Making

Basic conditional statements: A conditional statement will perform a certain operation if a given condition is met. The Basic format of IF statement is:

IF CONDITION-1 [THEN] IMPERATIVE STATEMENT-1 … [ELSE IMPERATIVE STATEMENT-2 ….] [END-IF]

THEN and END-IF are COBOL 85 options.

As we studied before, an imperative statement performs an operation regardless of any existing condition, and it is not a condition by itself. The following are simple relational conditions that may be used with IF statement:  IS EQUAL TO  IS LESS THAN  IS GREATER THAN Ex 1: Following is a simple IF statement without ELSE option:

IF ITEM-1 IS EQUAL TO ITEM-2 THEN MOVE ‘EQUAL ITEMS WAS FOUND’ TO WS-MESSAGE END-IF

Ex 2: Following is a simple IF statement with ELSE option:

IF AMNT-1 IS LESS THAN AMNT-2 THEN SUBTRACT AMNT-1 FROM AMNT-2 GIVING AMNT-3 ELSE SUBTRACT AMNT-2 FROM AMNT-1 GIVING AMNT-3 END-IF

1 Ex 3: More than one imperative statements can be executed within the same IF statement:

IF AMNT-1 IS LESS THAN AMNT-2 THEN SUBTRACT AMNT-1 FROM AMNT-2 GIVING AMNT-3 MOVE AMNT-3 TO OUT-FIELD ELSE SUBTRACT AMNT-2 FROM AMNT-1 GIVING AMNT-3 MOVE AMNT-3 TO OUT-FIELD END-IF

In COBOL 85 when we use a scope terminator (such as END-IF,END-READ, etc), a period at the end of the line is optional except for the last statement in a paragraph. COBOL 74 users should omit the END-IF and terminate the IF sentence by placing a period on the previous line.

Ex 4: Using a period instead of END-IF option:

IF PRICE-1 > PRICE-LIMIT ADD PRICE-1 TO AMNT GIVING TOTAL SUBTRUCT 1 FROM UNITS-IN ELSE MOVE ZERO TO TOTAL. MOVE TOTAL TO TOT-OUT.

Note: The last statement is not part of ELSE option, it will be executed all the time.

Field used in comparison: Fields used in any conditional statement must be of the same data types in order to obtain proper results. Numeric fields are compared algebraically. For example the following values are all considered equal in COBOL: 12, 12.0, +12.0, 0012 When we compare non-numeric fields the following are considered equivalent: ABC , ABC b , ABCbb The rightmost blanks do not affect the comparison. However leftmost blanks affect the comparison. For example: bABC and ABC are not equivalent.

2 Using Relational Operators in place of Verbs:

We can use the following simple relational operators symbols within COBOL code instead of verbs:

Operator symbol Meaning < IS LESS THAN > IS GREATER THAN = IS EQUAL TO <= (COBOL 85 only) IS LESS THAN OR EQUAL TO >= (COBOL 85 only) IS GREATER THAN OR EQUAL TO Most COBOL compilers require a blank on each side of the relational operators symbols.

Ex 5:

IF INV-IN < DEM-IN PERFOM ADD-SUPPLY END-IF

Ex 6:

IF AMNT-IN <= ZERO DISPLAY ‘ NEGATIVE VALUE’ END-IF

Ex 7: To code the preceding example using COBOL 74 we write:

IF AMNT-IN < ZERO OR AMNT-IN = ZERO DISPLAY ‘NEGATIVE VALUE’.

CONTINUE or NEXT Sentence:

COBOL 85 expression CONTINUE or COBOL 74 NEXT SENTENCE enable to: 1- Avoid performing any operation if a condition is met 2- Execute instructions only if ELSE condition is met.

3 Ex 8: COBOL 85

IF AMNT-1 = AMNT-2 CONTINUE ELSE ADD 1 TO TOTAL END-IF

In this example if AMNT-1 equals to AMNT-2 then nothing is done, otherwise 1 will be added to TOTAL.

Ex 9: COBOL 74 The same above example using COBOL 74

IF AMNT-1 = AMNT-2 NEXT SENTENCE ELSE ADD 1 TO TOTAL.

Ex 10: In the following example we add 1 to counter if TOTAL-1 is equals to TOTAL- 2 ,otherwise nothing is done.

IF TOTAL-1 = TO TATAL-2 ADD 1 TO COUNTER ELSE CONTINUE END-IF

Nested conditional: A nested conditional is a condition in which an IF statement can contain additional IF statements, for example:

IF CONDITION-1 STATEMENT-1 ELSE IF CONDITION-2 STATEMENT-2 ELSE STATEMENT-3 [END-IF] [END-IF]

4 Ex 11:

IF AMNT > ZERO IF UNITS-IN > ZERO PERFORM ORDER-1 ELSE PERFORM ADD-SUPPLY END-IF ELSE PERFORM START-OVER ENDIF

In this example if AMNT is positive then another condition is tested for UNITS- IN field. If AMNT-IN is not positive then START-OVER procedure is executed.

Compound conditional using (OR, AND):

With compound conditional, several conditions can be tested with one statement. When we have two or more conditions and at least one must be met we use OR operator to separate them. AND operator is used to separate two or more conditions when all must be met. The following rules apply for using (AND) and (OR):  If both (OR) and (AND) are used in the same compound conditional then conditions surrounding the word AND are evaluated first then conditions surrounding the word OR are evaluated last.  Several AND or Several OR can be used in the same compound conditional.  To override the sequence of evaluation, parentheses should be used.

Ex 12:

IF AMNT-1 = AMNT-2 OR AMNT-2 > AMNT-3 PERFORM TOTAL-CALCULATION END-IF

Ex 13:

IF AMNT-1 = AMNT-2 AND AMNT-3 >ZERO PERFORM TOTAL-CALCULATION END-IF

Sign Test: It is used to test whether a field is POSITIVE, NEGATIVE or ZERO.

5 Ex 14: IF AMNT IS POSITIVE PERFORM CALCULATION END-IF

Class test: It is used to test the type of a data in a field by coding: IF identifier-1 IS NUMERIC Or IF identifier-1 IS ALPHABETIC

COBOL 85 offers two extra class tests that are: ALPHABETIC-UPPER and ALPHABETIC-LOWER

Negating conditional: All simple relational, class and sign tests may be coded using a negated conditional (NOT).

Ex 15:

IF AMNT IS NOT EQUAL TO AMNT-2 PERFORM DISPLAY-TOTAL ELSE PERFORM ORDER-DETAILS END-IF

Condition-names: A condition name is a user-defined word established in the DATA DIVISION. It 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 of an identifier. It has the following format:

88 condition-name VALUE literal.

Ex 16: We can code in the DATA DIVISION

05 MARITAL-STATUS PIC X. 88 SINGLE VALUE ‘S’

In the PROCEDURER DIVISION we can code:

IF SINGLE PERFORM SINGLE-RTN END-IF

Instead of

6 IF MARITAL-STATUS IS EQUAL TO ‘S’ PERFORM SINGLE-RTN END-IF

Ex 17: 05 MARITAL-STATUS PIC X . 88 SINGLE VALUE ‘S’ 88 DEVORCED VALUE ‘D’. 88 MARRID VALUE ‘M’.

Ex 18:

05 STATUS PIC 9. 88 STATUS-VALU 1 THRU 5.

Ex 19:

05 CODE PIC X. 88 CODE-VAL ‘A’ ‘B’ ‘F’.

Ex 20: 05 CODE PIC X(5). 88 STYLE VALUE ALL ‘A’.

EVALUATE Statement (COBOL 85): EVALUATE verb is used to test a series of conditions. It implements the case structure. This statement has the following format: EVALUATE {identifier-1 or expression-1 } WHEN condition-1 imperative statement-1 … [WHEN OTHER imperative statement-2] [END-EVALUATE]

Ex 21: EVALUATE MENU-SELECTION WHEN 1 PERFORM ENTER-DATA WHEN 2 PERFORM PRINT-REPORT WHEN OTHER PERFORM INCORRECT-SELECTION END-EVALUATE

Instead of IF MENU-SELECTION IS EQUAL TO 1 PERFORM ENTER-DATA END-IF IF MENU-SELECTION IS EQUAL TO 2 PERFORM PRINT-REPORT END-IF IF MENU-SELECTION IS NOT EQUAL TO 1 AND NOT EQUAL TO 2 PERFORM ENTER-DATA END-IF

7 Ex 22:

EVALUATE TRUE WHEN MENU-SELECTION = 1 PERFORM ENTER-DATA WHEN MENU-SELECTION = 2 PERFORM PRINT-REPORT WHEN OTHER PERFORM INCORRECT-SELECTION END-EVALUATE

Ex 23:

DATA DIVISION 5 LEVEL PIC 9. 88 FIRST VALUE 1. 88 SECOND VALUE 2. 88 THIRD VALUE 3.

In the PROCEDURE DIVISION we can code:

EVALUATE LEVEL WHEN FIRST PERFORM FIRST-PROCEDURE WHEN SECOND PERFORM SECOND-PROCEDURE WHEN THIRD PERFORM THIRD-PROCEDURE WHEN OTHER PERFORM ERROR-PROCEDURE END-EVALUATE

8 Accessing segment of a field using COCOL 85: It is possible to reference a portion of an elementary field with COBOL 85. Assume that the field WS-PHONE-NO is defined in the WORKING- STORAGE SECTION as follow:

WS-PHONE-NO PIC X(10) VALUE ‘4161112222’ To test the area code part of this field we can code the following in the PROCEDURE DIVISION:

IF WS-PHONE-NO (1:3) = ’416’ DISPLAY ‘ TORONTO’ END-IF

The result of this IF statement is ‘TORONTO’. The first integer in the parentheses represents the starting position of the field segment; the second integer represents the field segment length.

Ex 24:

IDENTIFICATION DIVISION. PROGRAM-ID. YearSegment. DATA DIVISION. WORKING-STORAGE SECTION. 01 YEAR-IN-4 PIC 9999. 01 YEAR-IN-2 PIC 99. PROCEDURE DIVISION. MOVE 1900 TO YEAR-IN-4 MOVE YEAR-IN-4(3:2) TO YEAR-IN-2 DISPLAY YEAR-IN-2 STOP RUN.

Reference: textbook Ch 8.

9

Recommended publications