|
Conditional Expressions
Conditional Expressions
Statements in COBOL program executed one after another.
In programming, many times, Based on some condition, program needs to run one
or more sets of statements. If condition is true, one set of statements get
excuted. If condition is false another set of statements will get executed.
Condition(s) are used in IF, EVALUATE, PERFORM and SEARCH statements.
A condition can be any one of the folllowing :
Simple conditions
(1) Class condition
(2) Condition-name condition
(3) Relation condition
(4) Sign condition
(5) Negated simple condition
(6) Combined conditions
(1) Class condition
class condition determines whether the identifier contains numeric value OR
alphanumeric value. Below is the format of class condition
[IS] [NOT] < NUMERIC / ALPHABETIC / ALPHABETIC-LOWER / ALPHABETIC-UPPER >
This condition is very useful to minimize the program errors. there are
many chances that program may receive corrupted data. To handle these kind of
data errors, we can use this condition.
If we want to check whether the identifier contains the numeric data or not,
before doing arthmetic operation on it. use the Class codition as below
using IF statement.
IF WS-AMT IS NUMERIC
ADD WS-AMT TO WS-BANLANCE
ELSE
MOVE ZEROS TO WS-AMT
PERFORM ERROR-PARA.
END-IF.
In above code , we are using WS-AMT , only when it cotain valid numeric data
otherwise we are moving ZEROS to that identifier and calling error para.
other options.
ALPHABETIC - is used to check identifier contains all alphabetic letters or not
( i.e., A-Z, a-z and blank )
ALPHABETIC-LOWER - To check identifier contains only lower alphabetic letters or not.
i.e., a-z and blank.
ALPHABETIC-UPPER - To check identifier contains only upper alphabetic letters or not.
i.e., A-Z and blank.
(2) condition-name condition. (important)
condition-name is a identifier defined with level number 88. It has only VALUE
clause, it does not contain any picture clause, but it must always be associated
to a data name called the conditional variable.
Format
88 condition-name literal-1 [ literal-2 ]
[, literal-3 [ literal-4] ... ]
Example -
01 WS-AGE PIC 99. <--- conditional variable
88 INFANT VALUE 0.
88 BABY VALUE 1,2.
88 CHILD VALUE 3 THRU 12.
88 TEEN-AGER VALUE 13 THRU 19.
In the above example we have defined 4 condition-name's associated with one data
name WS-NAME. if WS-AGE contains value 0 (ZERO). INFANT will become true , since
we have defined this condition-name with value 0. If WS-AGE contains values 1 OR
2, Second condition name BABY will become true,since we have defined BABY
condition-name with value 1 and 2. Same rule applied to other condition-names.
How we can use this in PROCEDURE DIVISION?
See below example... IF INFANT is true, statement(s) after IF statement will be executed.
i.e., WS-AGE contains value 0 (ZER0), then INFANT will become true.
IF INFANT
MOVE 20 TO WS-SIGNAL
PERFORM 200-INFANT-PARA
END-IF.
Same code can be written as below....
IF WS-AGE = 0
MOVE 20 TO WS-SIGNAL
PERFORM 200-INFANT-PARA
END-IF
(3) Relation Condition
Relatioh condition compares two operands either of which can be an idenftifier,
literal, arithmetic expression.
Format 1
Operand 1 Operand 2
The Relational operator specifies the type of comparison to be made as shown in
below table.
Relational operator | Can be written as | Meaning of operator |
IS GREATER THAN | IS > | Greater than |
IS NOT GREATER THAN | IS NOT > | Not greater than |
IS LESS THAN | IS < | Less than |
IS NOT LESS THAN | IS NOT < | Not less than |
IS EQUAL TO | IS = | Equal to |
IS NOT EQUAL TO | IS NOT = | Not equal to |
IS GREATER THAN OR EQUAL TO | IS >= | Is greater than or equal to |
IS LESS THAN OR EQUAL TO | IS <= | Is less than or equal to |
Numeric operand comparison
- If two operands can be compared regardless of sizes and USAGE.
Non-Numeric operand comparison
- Comparison starts from left most character in both the fields ( operand-1 and operand-2)
and it proceeds to right.
- Comparison happens based on the EBCDIC/ASCII collating sequence of character set OR
the character set specified in object-computer paragraph.
- If two fields are not equal sizes, comparison happens as though the shorter field extended
to right with spaces to make operands equal in sizes.
Numeric and Non-Numeric comparison
- When Numeric and Non-numeric comparison happens, both operands USAGE must be same.
- Non-numeric comparison rules apply
(4) Sign Condition
The sign condition determines whether the given numeric value is grater than the ZERO (or)
less than the ZERO (or) equal to ZERO.
Format 1
operand-1 [IS] [NOT]
- Operand-1 must be defined as a numeric identifier.
- IS POSITIVE condition will become true, if the operand-1 value grater than ZERO
- IS NEGATIVE condition will become true, if the operand-1 value less than ZERO
- IS ZERO condition will become true, if the operand-1 contains ZERO
(5) Negated simple condition
We can negate the simple condition by using keyword NOT. If the condition is true,
by using NOT with the condition, overall boolean value of that condition will become
false. If the simple condition is flase, by using NOT with the condition, overall
boolean value of that condition will become true.
Ex. IF A = B
COMPUTE C = A ** 2
END-IF
if A & B contains the same value, COMPUTE statement will get executed.
if we use NOT with the condition, COMPUTE wont get executed, when A & B
contains the same value.
IF NOT A = B
COMPUTE C = A ** 2
END-IF
(6) Combined conditions
Combined conditions contains two or more conditions connected using logical operators AND
or OR.
Format
condition-1 condition-2 [ condition-3 ]...
Below table gives the results of using logical operators AND & OR between two conditions
COND1 & COND2.
COND1 COND2 COND1 AND COND2 COND1 OR COND2
True True True True
True False False True
False True False True
False False False False
Ex. IF (WS-A = WS-B) AND
(WS-C = WS-D)
COMPUTE WS-FINAL-AMT = WS-A + WS-C
END-IF
In above IF condition, if both conditions are true, then COMPUTE statement inside IF will get executed.
|
|