Monday, September 26, 2011

COBOL: Beware if you use logical operators NOT & AND in the same IF condition

This post explains the confusion caused by the usage of logical operators AND, OR in the same IF condition.


Lets say we have following set of values

TYPE-IND = "M"
REIMB-IND = "Q"
and ADJ-AMT-BILLED > SCHEDULE-AMT

and we have below criteria.

IF NOT ( TYPE-IND = 'B' )
AND NOT ((REIMB-IND = 'Q') AND (ADJ-AMT-BILLED < SCHEDULE-AMT))
    ADD WS-SCHED-SAV-TO-MEMBER TO MEMBER-LIAB
END-IF

What is the result of the above IF condition ?

Ans: The IF condition is satisifed.

Why this is happening

In the above code

i. NOT(TYPE-IND = 'B') is satisfied since TYPE-IND = 'M'
i.e, NOT(FALSE) = TRUE

ii. NOT ((REIMB-IND = 'Q') AND (ADJ-AMT-BILLED < SCHEDULE-AMT))
Result of this condition is NOT(TRUE AND FALSE) = NOT (FALSE) = TRUE

Conclusion :

NOT(Condition1 and condition2) is = Not(Condition1) Or Not(Condition2)


No comments:

Post a Comment

Note: Only a member of this blog may post a comment.