Professional Documents
Culture Documents
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.
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 = 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 -
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.
Example:
Let’s study the working of a deeply nested conditional. The data in the program used
are :
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.
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 :
IS EQUAL TO =
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
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.
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.
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.
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?
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.
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