Wednesday, February 17, 2010

6. Decision Making

A conditional statement is one that performs operations depending on the existence of some condition. In COBOL, such statements generally begin with the word IF and are called IF-THEN-ELSE or selection structures.
An imperative statement , as opposed to a conditional statement, is one that performs an operation regardless of any existing condition.
A condition may test for a specific relation. A simple condition may be a single relational test of the following form :

Eg 6.1:
IF AMT1 IS EQUAL TO AMT2
DIVIDE QTY INTO TOTAL
ELSE
ADD UNIT-PRICE TO FINAL-TOTAL.

The following symbols for simple relational conditions are valid within a COBOL statement :
<> IS GREATER THAN
= IS EQUAL TO

The NEXT SENTENCE or 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 NEXT SENTENCE will enable you (1) to avoid performing any operation if a condition exists and (2) to execute instructions only if the ELSE condition is met.

Eg 6.2:
IF AMT1 = AMT2
NEXT SENTENCE
ELSE
ADD 1 TO TOTAL.

Compound conditional
The compound conditional offers even greater flexibility for selection and enables the IF statement to be used for more complex problems. The compound conditionals are as follows :

OR
Performs an operation or a series of operations if any one of several conditions exists.


AND
If a statement or statements are to be executed only when all of several conditions are met.

Negating conditionals
NOT
All simple relation, class or sign tests may be coded using a negated conditional (NOT).

Eg 6.3:
IF AMT1 IS NOT EQUAL TO AMT2
PERFORM 200-NOT-EQUAL-RTN.

Hierarchy rules for compound conditionals
1. NOT is evaluated first.
2. Conditions surrounding the word AND are evaluated first.
3. Conditions surrounding the word OR are evaluated last.
4. 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.
5. To override Rules 1-3, use parentheses around conditions you want to be evaluated first.

Sign test
We can test whether a field is POSITIVE, NEGATIVE, or ZERO with a sign test.
If a numeric field contains an amount less than zero, it is considered negative. If it has an amount greater than zero, then it is considered positive.

Eg 6.4:
IF AMT IS POSITIVE
PERFORM 200-CALC-RTN.

Class test
We can test for the type of data (ALPHABETIC or NUMERIC) using the class test.

Eg 6.5:
IF AMT-IN IS NUMERIC
PERFORM 300-CALC-RTN.




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 coded in the DATA DIVISION is a condition-name that denotes a possible value for an identifier. A condition-name is always coded on the 88 level and has only a VALUE clause associated with it. Since a condition-name is not the name of a field, it will not contain a PICTURE clause.

Eg 6.6:
05 MARITAL-STATUS PIC X.
88 SINGLE VALUE “S”.

Either of the following tests may be used in the PROCEDURE DIVISION .

Eg 6.6a:
IF MARITAL-STATUS = “S”
PERFORM 1000-SINGLE-ROUTINE.

or

Eg 6.6b:
IF SINGLE
PERFORM 1000-SINGLE-ROUTINE.

Back to COBOL Index

No comments: