MainframeMaster
MainframeMaster

COBOL Tutorial

Progress0 of 0 lessons

COBOL Logical Operations

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.

What are Logical Operations?

Logical operations combine conditions using logical operators:

  • AND: All conditions must be true
  • OR: At least one condition must be true
  • NOT: Negates (inverts) a condition

These operators allow you to build complex expressions that evaluate to true or false, controlling program behavior based on multiple factors.

The AND Operator

AND requires all conditions to be true for the overall expression to be true. If any condition is false, the entire expression is false.

AND Truth Table

AND Operator Truth Table
Condition 1Condition 2Result
TrueTrueTrue
TrueFalseFalse
FalseTrueFalse
FalseFalseFalse

AND Examples

cobol
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
WORKING-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.

The OR Operator

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.

OR Truth Table

OR Operator Truth Table
Condition 1Condition 2Result
TrueTrueTrue
TrueFalseTrue
FalseTrueTrue
FalseFalseFalse

OR Examples

cobol
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
WORKING-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.

The NOT Operator

NOT negates (inverts) a condition. If a condition is true, NOT makes it false, and vice versa.

NOT Truth Table

NOT Operator Truth Table
ConditionNOT Result
TrueFalse
FalseTrue

NOT Examples

cobol
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
WORKING-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.

Combining Operators

You can combine AND, OR, and NOT in complex expressions. Use parentheses to control evaluation order and clarify intent.

Complex Logical Expressions

cobol
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
WORKING-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

Operator precedence determines evaluation order:

  • NOT: Highest precedence (evaluated first)
  • AND: Medium precedence (evaluated second)
  • OR: Lowest precedence (evaluated last)

Precedence Examples

cobol
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
*> 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.

Best Practices

Follow these best practices:

  • Use parentheses: Clarify intent and control evaluation order
  • Keep expressions readable: Break complex expressions into multiple IF statements if needed
  • Use condition names: 88-level names make logic clearer
  • Document complex logic: Add comments explaining business rules
  • Test thoroughly: Verify all combinations of conditions
  • Avoid deep nesting: Simplify when possible

Explain Like I'm 5: Logical Operations

Think of logical operations like rules for a party:

  • AND is like "you need BOTH a ticket AND an ID" - you need everything
  • OR is like "you can bring chips OR cookies OR both" - any one is enough
  • NOT is like "NOT wearing red" - the opposite of the condition

Just like party rules combine conditions (ticket AND ID, chips OR cookies), logical operations combine program conditions to make decisions!

Test Your Knowledge

1. What does AND require for the expression to be true?

  • At least one condition true
  • All conditions true
  • No conditions true
  • Exactly one condition true

2. What does OR require for the expression to be true?

  • All conditions true
  • At least one condition true
  • No conditions true
  • Exactly two conditions true

3. What is the operator precedence order?

  • AND, OR, NOT
  • NOT, AND, OR
  • OR, AND, NOT
  • All have equal precedence

4. How do you negate a condition?

  • Use AND
  • Use OR
  • Use NOT
  • Use IF NOT

5. What does "IF A = 1 OR B = 2 AND C = 3" evaluate as?

  • (A = 1 OR B = 2) AND C = 3
  • A = 1 OR (B = 2 AND C = 3)
  • Syntax error
  • Depends on implementation

6. When should you use parentheses in logical expressions?

  • Never
  • Always
  • When you need to override operator precedence or clarify intent
  • Only with NOT

Related Concepts

Related Pages