In COBOL you combine conditions in IF, EVALUATE, and other statements using the logical operators AND, OR, and NOT. The order in which these are applied is determined by precedence: NOT first, then AND, then OR. Without parentheses, the result can be different from what you expect, especially when mixing AND and OR. This page explains each operator, how precedence works, how to use parentheses, and how to avoid the common mistake of writing incomplete conditions (e.g. X = 1 OR 2 instead of X = 1 OR X = 2).
When you ask the computer "is this true?" you sometimes need to ask more than one thing. AND means "both must be true"—like "you must finish your vegetables AND your homework." OR means "at least one must be true"—like "we can have apples OR oranges." NOT means "the opposite"—like "NOT rainy" means it is not rainy. The computer has a rule for which word it checks first: NOT, then AND, then OR. If you want a different order, you use parentheses so the computer does the part in brackets first, just like in math.
COBOL uses the words AND, OR, and NOT to combine or negate conditions. Each condition is a comparison (e.g. WS-X = 5), a class test (NUMERIC, ALPHABETIC), a sign test (POSITIVE, ZERO), or a condition-name test (88-level). The result of the combined expression is true or false and controls whether the IF (or other) block executes.
| Operator | Precedence | Meaning |
|---|---|---|
| NOT | 1 (first) | Negates the condition that follows |
| AND | 2 | True only when both conditions are true |
| OR | 3 (last) | True when at least one condition is true |
AND connects two (or more) conditions. The combined condition is true only when every condition is true. If the first condition is false, the result is false and the implementation may not evaluate the rest (short-circuit). Example: IF WS-AGE > 18 AND WS-STATUS = 'A' — both must be true. Use AND when you need "all of these must hold."
OR connects two (or more) conditions. The combined condition is true when at least one condition is true. If the first condition is true, the result may be true without evaluating the rest. Example: IF WS-DAY = 'SAT' OR WS-DAY = 'SUN' — either makes the condition true. A common error is writing IF WS-DAY = 'SAT' OR 'SUN'; the second part is not a full condition (WS-DAY = something). You must write IF WS-DAY = 'SAT' OR WS-DAY = 'SUN' or use an 88-level with multiple values.
NOT negates the condition that immediately follows it. IF NOT WS-EOF means "if WS-EOF is false." NOT has the highest precedence, so NOT A AND B is read as (NOT A) AND B, not NOT (A AND B). For NOT (A AND B) you must use parentheses. NOT is useful for flags and for inverting a single test.
When you mix operators without parentheses, COBOL evaluates in this order: NOT first, then AND, then OR. Same precedence is usually left to right. So:
Because this can be confusing, it is good practice to use parentheses whenever you combine AND and OR so that the intent is clear and the evaluation order is explicit.
1234567*> Precedence: AND before OR IF WS-TYPE = 'A' OR WS-TYPE = 'B' AND WS-AMT > 100 *> Same as: IF WS-TYPE = 'A' OR (WS-TYPE = 'B' AND WS-AMT > 100) *> Clear intent with parentheses IF (WS-TYPE = 'A' OR WS-TYPE = 'B') AND WS-AMT > 100 *> Both type and amount must match
Parentheses override the default precedence. The innermost parentheses are evaluated first. So (A OR B) AND C means: evaluate A OR B, then AND that result with C. Use parentheses whenever the combination of AND and OR could be misread, and to document intent. They do not change the result when precedence already matches your intent but can make the code easier to read and maintain.
A frequent error is abbreviating multiple values for one variable. Writing IF WS-CODE = 1 OR 2 does not mean "if WS-CODE is 1 or 2." The second operand (2) is not a full condition; the compiler may treat it as a literal in an invalid or unexpected way. The correct form is IF WS-CODE = 1 OR WS-CODE = 2: two complete conditions. For many values, 88-level condition names are clearer: define 88 WS-CODE-VALID VALUES 1 2 3 and write IF WS-CODE-VALID.
1234567891011121314*> Wrong: second part is not a condition *> IF WS-CLASS = 'First Yr' OR 'Second Yr' *> Correct: two full conditions IF WS-CLASS = 'First Yr' OR WS-CLASS = 'Second Yr' DISPLAY 'Underclassman' END-IF *> Or with 88-level *> 01 WS-CLASS PIC X(10). *> 88 UNDERCLASS VALUES 'First Yr' 'Second Yr'. IF UNDERCLASS DISPLAY 'Underclassman' END-IF
Many COBOL implementations use short-circuit evaluation: as soon as the overall truth value is known, the rest of the conditions may not be evaluated. For A OR B, if A is true, B might not be evaluated. For A AND B, if A is false, B might not be evaluated. You should not rely on side effects (e.g. a paragraph that sets a variable) in the second condition, because it might never run. Keep conditions free of side effects so that short-circuit behavior does not change program logic.
1. In IF A AND B OR C, which is evaluated first?
2. To test "WS-CODE is 1 or 2" you should write:
3. NOT in COBOL has: