Logical operations in COBOL combine multiple conditions using logical operators AND, OR, and NOT to create complex conditional expressions. These operations enable programs to make decisions based on multiple criteria, evaluate complex business rules, and control program flow based on various conditions. Understanding logical operations is fundamental to writing effective conditional logic in COBOL programs.
Logical operations combine conditions using logical operators:
These operators allow you to build complex expressions that evaluate to true or false, controlling program behavior based on multiple factors.
AND requires all conditions to be true for the overall expression to be true. If any condition is false, the entire expression is false.
| Condition 1 | Condition 2 | Result |
|---|---|---|
| True | True | True |
| True | False | False |
| False | True | False |
| False | False | False |
123456789101112131415161718192021222324252627282930WORKING-STORAGE SECTION. 01 CUSTOMER-AGE PIC 9(3). 01 ACCOUNT-BALANCE PIC 9(9)V99. 01 ACCOUNT-STATUS PIC X. 88 ACTIVE-ACCOUNT VALUE 'A'. 88 CLOSED-ACCOUNT VALUE 'C'. PROCEDURE DIVISION. AND-EXAMPLES. *> Both conditions must be true IF CUSTOMER-AGE >= 18 AND ACCOUNT-BALANCE > 0 DISPLAY 'Customer is adult with positive balance' END-IF *> Multiple AND conditions IF CUSTOMER-AGE >= 18 AND ACCOUNT-BALANCE > 1000 AND ACTIVE-ACCOUNT DISPLAY 'Eligible for premium account' END-IF *> AND with different condition types IF CUSTOMER-AGE > 65 AND ACCOUNT-BALANCE >= 5000 AND ACTIVE-ACCOUNT AND ACCOUNT-STATUS NOT = 'C' DISPLAY 'Senior customer with premium account' END-IF STOP RUN.
AND is useful when you need all criteria to be met, such as validation rules that require multiple conditions.
OR requires at least one condition to be true. The expression is true if any condition is true, and false only when all conditions are false.
| Condition 1 | Condition 2 | Result |
|---|---|---|
| True | True | True |
| True | False | True |
| False | True | True |
| False | False | False |
1234567891011121314151617181920212223242526272829WORKING-STORAGE SECTION. 01 PAYMENT-METHOD PIC X. 88 CREDIT-CARD VALUE 'C'. 88 DEBIT-CARD VALUE 'D'. 88 CASH VALUE 'H'. 01 TRANSACTION-AMOUNT PIC 9(7)V99. PROCEDURE DIVISION. OR-EXAMPLES. *> At least one condition must be true IF CREDIT-CARD OR DEBIT-CARD DISPLAY 'Card payment method' END-IF *> Multiple OR conditions IF TRANSACTION-AMOUNT < 10 OR TRANSACTION-AMOUNT > 10000 OR CASH DISPLAY 'Requires special handling' END-IF *> OR for multiple valid values IF PAYMENT-METHOD = 'C' OR PAYMENT-METHOD = 'D' OR PAYMENT-METHOD = 'P' DISPLAY 'Electronic payment' END-IF STOP RUN.
OR is useful when any of several conditions is acceptable, such as checking for multiple valid values or handling multiple error cases.
NOT negates (inverts) a condition. If a condition is true, NOT makes it false, and vice versa.
| Condition | NOT Result |
|---|---|
| True | False |
| False | True |
1234567891011121314151617181920212223242526272829WORKING-STORAGE SECTION. 01 END-OF-FILE PIC X VALUE 'N'. 88 EOF VALUE 'Y'. 01 ACCOUNT-STATUS PIC X. 88 ACTIVE-ACCOUNT VALUE 'A'. PROCEDURE DIVISION. NOT-EXAMPLES. *> Negate a condition IF NOT EOF DISPLAY 'More records to process' END-IF *> NOT with condition name IF NOT ACTIVE-ACCOUNT DISPLAY 'Account is not active' END-IF *> NOT with comparison IF NOT (CUSTOMER-AGE < 18) DISPLAY 'Customer is adult' END-IF *> NOT with complex expression (use parentheses) IF NOT (ACCOUNT-BALANCE < 0 OR CLOSED-ACCOUNT) DISPLAY 'Account is in good standing' END-IF STOP RUN.
NOT is useful for inverting conditions, checking for the absence of conditions, and creating negative logic.
You can combine AND, OR, and NOT in complex expressions. Use parentheses to control evaluation order and clarify intent.
1234567891011121314151617181920212223242526272829303132WORKING-STORAGE SECTION. 01 CUSTOMER-AGE PIC 9(3). 01 ACCOUNT-BALANCE PIC 9(9)V99. 01 ACCOUNT-STATUS PIC X. 88 ACTIVE-ACCOUNT VALUE 'A'. 88 PREMIUM-ACCOUNT VALUE 'P'. 01 CREDIT-SCORE PIC 9(3). PROCEDURE DIVISION. COMPLEX-EXPRESSIONS. *> Combining AND and OR IF (CUSTOMER-AGE >= 18 AND CUSTOMER-AGE <= 65) OR (PREMIUM-ACCOUNT AND ACCOUNT-BALANCE > 50000) DISPLAY 'Eligible for standard loan' END-IF *> Using NOT with AND and OR IF ACTIVE-ACCOUNT AND NOT (ACCOUNT-BALANCE < 0) AND (CREDIT-SCORE >= 700 OR PREMIUM-ACCOUNT) DISPLAY 'Approved for credit increase' END-IF *> Multiple levels of logic IF (CUSTOMER-AGE >= 21 AND ACCOUNT-BALANCE >= 1000) OR (PREMIUM-ACCOUNT AND NOT (CREDIT-SCORE < 600)) DISPLAY 'Qualified for premium services' END-IF STOP RUN.
Operator precedence determines evaluation order:
123456789101112131415161718*> Without parentheses, precedence applies: *> A = 1 OR B = 2 AND C = 3 *> Is evaluated as: A = 1 OR (B = 2 AND C = 3) *> Because AND has higher precedence than OR IF A = 1 OR B = 2 AND C = 3 *> This means: A = 1 OR (B = 2 AND C = 3) END-IF *> Use parentheses to change evaluation order: IF (A = 1 OR B = 2) AND C = 3 *> This means: (A = 1 OR B = 2) AND C = 3 END-IF *> NOT is evaluated first: IF NOT A = 1 AND B = 2 *> This means: (NOT A = 1) AND B = 2 END-IF
Always use parentheses in complex expressions to ensure clarity and correct evaluation order.
Follow these best practices:
Think of logical operations like rules for a party:
Just like party rules combine conditions (ticket AND ID, chips OR cookies), logical operations combine program conditions to make decisions!
1. What does AND require for the expression to be true?
2. What does OR require for the expression to be true?
3. What is the operator precedence order?
4. How do you negate a condition?
5. What does "IF A = 1 OR B = 2 AND C = 3" evaluate as?
6. When should you use parentheses in logical expressions?