You are on page 1of 8

Wednesday, 06 January 2010

© Mainframes360.com – Quasar Chunawala


Visit us at : http://www.mainframes360.com

Decision Making in COBOL


Q. How does the IF Condition in COBOL work? What’s the syntax for it..?

The IF-condition is used for decision-making in COBOL. The basic IF-condition has
following structure -

IF <test-condition> THEN
Statement-1
Statement-2
...
END-IF
Statement-X

The entire IF END-IF construct along with all its statements, Statement-1, Statement-2
constitute a single unit, a BLOCK. Upon entry into the IF Block, the test-condition is
checked. The statements inside the IF Block constitute the body of the IF Block. Thus,
the test-condition guards entry into the IF Body.

The <test-condition> expression is evaluated. If the test-condition expression equals


= true, then the Statement-1, Statement-2,... are executed, i.e. the statements in the
body of the IF-Block are executed. Once, the IF-Block has been completed, the next
successive statement following the IF-Block, say Statement-X will be executed.

When test-condition expression equals = false, the control jumps directly to


Statement-X, after the IF-Block.
Wednesday, 06 January 2010
© Mainframes360.com – Quasar Chunawala
Visit us at : http://www.mainframes360.com

Example:
01 AIR-FARE PIC 9(5).
01 BAGGAGE-WEIGHT PIC 9(5).
01 MAXIMUM-ALLOWED PIC 9(5) VALUE 20.
01 DIFFERENCE PIC 9(5).
01 EXCESS-CHARGE PIC 9(5).
...
...
MOVE 1000 TO AIR-FARE
IF BAGGAGE-WEIGHT > MAXIMUM-ALLOWED THEN
COMPUTE DIFFERENCE = BAGGAGE-WEIGHT – MAXIMUM-ALLOWED
COMPUTE EXCESS-CHARGE = DIFFERENCE * 300;
COMPUTE AIR-FARE = AIR-FARE + EXCESS-CHARGE
END-IF
DISPLAY ‘AIR-FARE : ‘ AIR-FARE

Explanation:
In a flight, the maximum weight of luggage allowed = 20 kgs. The passengers have to
pay a Total fare = Basic Fare + Any extra charges for excess baggage above 20 kgs. How
to code this logic in COBOL?

First, the MOVE statement stores the value 1000 in AIR-FARE variable. Now, if the
baggage-weight exceeds 20 kgs., then you got to pay extra for the excess baggage. The
IF condition checks, whether the weight is greater than 20.

Say, the baggage-weight = 24 kgs. Then, baggage-weight > 20, so difference = 24 – 20 =


4 kgs. The excess-charge would be 300 multiplied 4 times = Rs. 1200. So, the air-fare
= 1000 + 1200 = 2200. Finally, the DISPLAY statement displays AIR-FARE=2200.

Say, the baggage-weight = 18 kgs. Then, baggage-weight < 20, so the test-condition
fails. Thus, the control directly to the DISPLAY statement after the IF-block, and the
Air-Fare is displayed as Rs. 1000.
Wednesday, 06 January 2010
© Mainframes360.com – Quasar Chunawala
Visit us at : http://www.mainframes360.com

Upon running the above COBOL Program, you get the following output -

Q. How do you use the IF-THEN


THEN ELSE construct in COBOL?
Wednesday, 06 January 2010
© Mainframes360.com – Quasar Chunawala
Visit us at : http://www.mainframes360.com

The IF-THEN
THEN ELSE construct in COBOL, has the following
follow syntax :

IF <test-condition> THEN
Statement-1
Statement-2
...
ELSE
Statement-3
Statement-4
...
END-IF
Statement-X

The IF-THEN
THEN ELSE construct is simple – when the test-condition
condition = true, the IF-part
IF is
performed. When the test-condition
condition = false, the ELSE-part
ELSE part is performed. Thus, the IF
part and ELSE part are mutually exclusive. Either one of them is performed, based on
the truth or falsity of the condition.

The statement-1/statement-22 could be a simple COBOL Statement, or it could be another


IF Else block. One IF-ELSE
ELSE Block can sit right inside another IF or ELSE. This is
called as Nested Conditional.

Example:
Let’s study the working of a deeply nested conditional. The data in the program used
are :

The logic used to calculate the RATE-OF-TAX


RATE is as follows -

Salary upto 20k Salary>20k

Salaried Employee 08 percent 10 percent

Self-employed Professional 20 percent 20 percent

This decision-table
table is used to determine the rate of tax. You can implement this logic
COBOL Code as follows -
Wednesday, 06 January 2010
© Mainframes360.com – Quasar Chunawala
Visit us at : http://www.mainframes360.com

The data presented tells you, the Salary is 18000. The program starts with
wit SET
SALARIED TO TRUE, which turns on the SALARIED switch. Thus, the control goes to the
next statement, IF SALARIED. When this condition is evaluated(as SALARIED switch is
turned on), it returns true. So, control enters into the the IF Block. The control
reaches IF SALARY > 2000.
. Salary equals 18k, so
(SALARY > 20000) = false, the control goes to the ELSE part. The statement MOVE 08 to
Rate is performed, which stores the value 08 in Rate. The Inner IF Block terminates.
The control jumps to the DISPLAY statement,
statement, that displays the RATE = 08 percent.

Q. What are the Relational/Comparision Conditions?

To compare two quantities, which is larger one, which is smaller amongst them, are the
two quantities equal in magnitude, you would use Relational Conditions
nditions. The different
relational operators available in COBOL, are tabulated below :

Relational Operator Symbol

IS LESS THAN <

IS EQUAL TO =

IS GREATER THAN >

IS LESS THAN OR EQUAL TO <=

IS GREATER THAN OR EQUAL TO >=

A relational condition would have the following form:


Wednesday, 06 January 2010
© Mainframes360.com – Quasar Chunawala
Visit us at : http://www.mainframes360.com

data-item-1 relational-condition data-item-2

The data-items could be variables or literal constant values. Moreover, in relational


conditions you can use the complete relational operator spelled out in words, or as an
alternative the symbolic representation for it. So, for example you may write 2 < 3,
or you may as well write 2 IS LESS THAN 3.

What happens, when you compare two data-items, which are non-numeric. In this case,
the result of comparision depends on the collating sequence.

Q. Well in COBOL, how do you combine 2 or more conditions? Like, you wanna check if
AGE >= 20 and AGE =< 35(Age lies in the range 20 to 35)?

In COBOL, you can write a simple condition or a Complex Condition. Two or more simple
conditions connected by Logical Operators give a Complex condition. For example, you
can use the Logical operator AND to combine two conditions. For example, to check
whether Age is in the range 20-35, you write

IF (AGE >= 20) AND (AGE <= 35)


<condition-1> <condition-2>

The Logical Operators in COBOL, are briefly discussed below :


1. IF BALANCE IS NEGATIVE AND DAYS-OVERDUE > 30
PERFORM 1100-SEND-OVERDUE-NOTICE

In the above example, the PERFORM statement is executed, only when both the conditions
balance is less than 0, as well as, DAYS-OVERDUE exceeds 30 are satisfied. The AND
operator returns true, only if both the conditions are true.

2. IF PHONE-NUMBER IS NOT NUMERIC OR NAME-IS-MISSING


PERFORM INVALID-DATA

In the above example, the PERFORM Statement is executed, when either the phone-number
field is non-numeric or the name field is missing. Thus, the OR Operator returns true,
even if one of the conditions is true. If both the conditions are true, well and good,
but even if one of them is true, OR returns true.

3. IF WEIGHT-1 NOT = WEIGHT-2


PERFORM INEQUAL-WEIGHTS

In the above example, the PERFORM Statement is executed, when the first weight does
NOT equal the second weight. The NOT Operator is used to negate any condition.

Q. What are Class Conditions in COBOL?

Class Conditions help determine, what type of data is present in a variable. Thus, it
checks the contents stored in a variable, is it alphabetic, numeric or alpha-
numeric... Suppose, you want to check, whether the name is alphabetic, or age of the
person is numeric, then you should use Class Conditions. The general format for Class
conditions is given below -
Wednesday, 06 January 2010
© Mainframes360.com – Quasar Chunawala
Visit us at : http://www.mainframes360.com

Data is Alphabetic implies that it contains only characters from A-Z and/or blanks.
Alphabetic data cannot contain numerals(0,1,2,...,9). On the other, when you say that
data is numeric in COBOL, it can contain digits from 0-9, with/without a sign.

ALPHABETIC-LOWER and ALPHABETIC-UPPER are the same as alphabetic, except that they
also take the case into account; whether it is lower-case or upper-case. So, if the
variable WS-TEXT contains ‘HELLO’, and you test IF WS-TEXT IS ALPHABETIC-UPPER, it
returns true.

Q. Hey pal, how do you check if the data input in a COBOL Program is alphanumeric?
Alphabetic is cool, numeric is awesome, but alphanumeric?

Let’s assume, you want to check, if the ADDRESS-FIELD is alpha-numeric. To do this,


take it one at a time, (i) First check, if ADDRESS-FIELD is alphabetic. (ii)Next
check, if ADDRESS-FIELD is numeric.

IF ADDRESS-FIELD IS NOT ALPHABETIC THEN


IF ADDRESS-FIELD IS NOT NUMERIC THEN
PERFORM INVALID-ADDRESS
END-IF
END-IF

Q. What are Condition-name conditions?

Suppose the variable WS-GENDER stores the values ‘M’ for Male or ‘F’ for female. You
may want to associate the values ‘M’ or ‘F’ with some meaningful names such as MALE or
FEMALE. To do this, you use 88-Level Condition names. Consider the following :

--1----+----2----+----3----+----4----+----5----+----6----+----7--
DATA DIVISION.
WORKING-STORAGE SECTION.
01 GENDER PIC X.
88 MALE VALUE 'M'.
88 FEMALE VALUE 'F'.
PROCEDURE DIVISION.
SET MALE TO TRUE
IF MALE THEN
DISPLAY 'GENDER IS MALE'
ELSE
DISPLAY 'GENDER IS FEMALE'
END-IF
STOP RUN.

In the above code snippet, I have assigned the values ‘M’ and ‘F’, the symbolic names
MALE and FEMALE respectively using 88-level entries. 88-Level entries act as flags. To
turn the flag on, you use the SET Statement. SET MALE TO TRUE, turns MALE flag on, and
Wednesday, 06 January 2010
© Mainframes360.com – Quasar Chunawala
Visit us at : http://www.mainframes360.com

‘M’ is stored in GENDER variable. To check if the gender is male, you may write IF
GENDER = ‘M’ or you could use the condition name - IF MALE; both are equivalent.

Q. What are Sign-conditions?

Sign conditions are used to find if a numeric quantity is positive(>0) or


negative(<0). Thus, they are used to check for the sign of a numeric variable.

The general syntax for Sign condition is given below -

The quantity which is being test could be stored in a Variable, or it could also be
the result of an expression. A variable is POSITIVE, if it is greater than > 0. On the
other hand, the variable is NEGATIVE, if it is less than 0.

Example:
IF BALANCE IS NEGATIVE
PEFORM SEND-CREDIT-OVERLIMIT-NOTICE
END-IF

You might also like