AND, OR, NOT and precedence
A condition can be a combination of conditions, and COBOL combines them with AND, OR and NOT. The rules are the ones of Boolean logic with one addition of its own: the abbreviated combined relation, which repeats the subject of a comparison for the reader and which behaves very differently from the similar-looking expression in C.
AND binds tighter than OR
AND is evaluated before OR, as in most languages, so a condition written without parentheses groups the AND first:
IDENTIFICATION DIVISION.
PROGRAM-ID. LOGIC.
DATA DIVISION.
WORKING-STORAGE SECTION.
01 A-F PIC 9 VALUE 1.
01 B-F PIC 9 VALUE 0.
PROCEDURE DIVISION.
IF A-F = 1 OR B-F = 1 AND A-F = 2
DISPLAY "unparenthesised: true"
ELSE
DISPLAY "unparenthesised: false"
END-IF.
IF (A-F = 1 OR B-F = 1) AND A-F = 2
DISPLAY "parenthesised: true"
ELSE
DISPLAY "parenthesised: false"
END-IF.
IF A-F = 1 OR B-F = 1
DISPLAY "one of them is 1"
END-IF.
IF NOT (A-F = 1 OR B-F = 1)
DISPLAY "neither is 1"
ELSE
DISPLAY "at least one is 1"
END-IF.
IF A-F NOT = 2
DISPLAY "A is not 2"
END-IF.
STOP RUN.
cobc -x -free -Wall -o logic logic.cob
./logic
unparenthesised: true
parenthesised: false
one of them is 1
at least one is 1
A is not 2
Parentheses change the answer
The two conditions differ only in a pair of brackets and they give opposite answers. Without them the compiler reads A = 1 OR (B = 1 AND A = 2): the first comparison is true and the whole condition is true whatever follows the OR. With them the reader asked whether A is 1 or B is 1 and A is 2 at the same time, which is false for these values because A is 1.
The lesson is not that one form is right: it is that a condition with both operators needs the brackets, because the reader who writes A = 1 OR B = 1 AND A = 2 almost never means the unparenthesised grouping.
NOT and NOT =
NOT in front of a bracketed condition negates the whole of it, and NOT = is the inequality operator that also has the spelling NOT EQUAL TO. Both are in the program above and both printed what they say.
A NOT without brackets negates the comparison it stands in front of and not a combination. In NOT A = 1 OR B = 1 the negation belongs to the first comparison only, and the condition is (A NOT = 1) OR B = 1. That reading surprises readers from SQL and from C, so the course always writes the brackets.
The abbreviated combined relation
COBOL lets the subject of a comparison be left out when it is the same one as before. IF CODE-F = 1 OR 2 is read as CODE-F = 1 OR CODE-F = 2, and that is what the run shows:
IDENTIFICATION DIVISION.
PROGRAM-ID. ABBREV.
DATA DIVISION.
WORKING-STORAGE SECTION.
01 CODE-F PIC 9 VALUE 2.
01 ONE-F PIC 9 VALUE 1.
01 OTHER-F PIC 9 VALUE 7.
PROCEDURE DIVISION.
IF CODE-F = 1 OR 2
DISPLAY "2: the abbreviation is true"
ELSE
DISPLAY "2: the abbreviation is false"
END-IF.
IF OTHER-F = 1 OR 2
DISPLAY "7: the abbreviation is true"
ELSE
DISPLAY "7: the abbreviation is false"
END-IF.
IF OTHER-F = 1 OR OTHER-F = 2
DISPLAY "7: the long form is true"
ELSE
DISPLAY "7: the long form is false"
END-IF.
IF CODE-F NOT = 1 OR 2
DISPLAY "2: not-equal abbreviation is true"
ELSE
DISPLAY "2: not-equal abbreviation is false"
END-IF.
IF ONE-F NOT = 1 OR 2
DISPLAY "1: not-equal abbreviation is true"
ELSE
DISPLAY "1: not-equal abbreviation is false"
END-IF.
STOP RUN.
cobc -x -free -Wall -o abbrev abbrev.cob
./abbrev
2: the abbreviation is true
7: the abbreviation is false
7: the long form is false
2: not-equal abbreviation is true
1: not-equal abbreviation is true
The first three lines are the point: for a code of 2 the abbreviation is true, for a code of 7 it is false, and the long form agrees with it. A C programmer expects code == 1 || 2 to be always true, because there the 2 is a value and not a comparison; in COBOL the subject is repeated and the condition is the one the reader meant.
The negated abbreviation is always true
The abbreviation repeats the subject in a negated comparison as well, so CODE-F NOT = 1 OR 2 is CODE-F NOT = 1 OR CODE-F NOT = 2. Every value is different from at least one of two different values, so the condition is true for 1, for 2 and for 7, which is what the last two lines of the run show.
A program that means "neither one nor two" writes NOT (CODE-F = 1 OR CODE-F = 2) or CODE-F NOT = 1 AND CODE-F NOT = 2. The next lesson replaces a chain of these tests with one EVALUATE, which cannot get the negation wrong because it has no NOT.
Exercises
- Swap the values of A-F and B-F in the first program and run it again. Which of the five lines change?
- Write the condition A = 1 OR B = 1 AND A = 2 with brackets that give the same answer as the compiler gives.
- Test IF CODE-F NOT = 1 OR 2 with a code of 3. Why is the answer no help at all?
- Write a condition that is true when a code is 1, 2 or 3, first with an abbreviation and then with parentheses. Which one would you keep in a program that someone else will read?
Questions
A new set of questions in a new order every time. Choose one answer for each, then check.
-
Which operator binds tighter in a COBOL condition?
Answer
AND, so A OR B AND C is A OR (B AND C)
The unparenthesised condition on this page was true only because the AND was grouped first.
-
What does IF CODE-F = 1 OR 2 mean?
Answer
CODE-F = 1 OR CODE-F = 2
COBOL repeats the subject of the comparison, which is what the run with a code of 7 shows.
-
Why is IF CODE-F NOT = 1 OR 2 true for every value?
Answer
It means NOT = 1 OR NOT = 2, and no value equals both
The subject is repeated in both comparisons, so any value differs from at least one of the two.
-
What does NOT in front of a comparison negate?
Answer
That comparison, unless brackets group more of the condition
Without brackets NOT belongs to the comparison it precedes, so NOT A = 1 OR B = 1 is (A NOT = 1) OR B = 1.
-
What does this program print?
IDENTIFICATION DIVISION. PROGRAM-ID. Q5. DATA DIVISION. WORKING-STORAGE SECTION. 01 A-F PIC 9 VALUE 1. 01 B-F PIC 9 VALUE 1. PROCEDURE DIVISION. IF A-F = 1 AND B-F = 2 DISPLAY "both" ELSE DISPLAY "not both" END-IF. STOP RUN.Answer
not both
B is 1 and not 2, so the AND is false and the ELSE branch runs.
-
What does this program print?
IDENTIFICATION DIVISION. PROGRAM-ID. Q6. DATA DIVISION. WORKING-STORAGE SECTION. 01 A-F PIC 9 VALUE 1. 01 B-F PIC 9 VALUE 0. PROCEDURE DIVISION. IF A-F = 1 OR B-F = 1 AND A-F = 2 DISPLAY "yes" ELSE DISPLAY "no" END-IF. STOP RUN.Answer
yes
The AND is grouped first and is false, but A = 1 is true and the OR makes the whole condition true.
-
What does this program print?
IDENTIFICATION DIVISION. PROGRAM-ID. Q7. DATA DIVISION. WORKING-STORAGE SECTION. 01 A-F PIC 9 VALUE 1. PROCEDURE DIVISION. IF A-F = 2 OR 3 DISPLAY "two or three" ELSE DISPLAY "other" END-IF. STOP RUN.Answer
other
The abbreviation tests A = 2 OR A = 3, and one is neither, so the ELSE branch runs.
-
What does this program print?
IDENTIFICATION DIVISION. PROGRAM-ID. Q8. DATA DIVISION. WORKING-STORAGE SECTION. 01 A-F PIC 9 VALUE 1. PROCEDURE DIVISION. IF NOT (A-F = 1 OR A-F = 2) DISPLAY "neither" ELSE DISPLAY "one of them" END-IF. STOP RUN.Answer
one of them
A is 1, so the bracketed condition is true and its negation is false.
-
What does this program print?
IDENTIFICATION DIVISION. PROGRAM-ID. Q9. DATA DIVISION. WORKING-STORAGE SECTION. 01 A-F PIC 9(2) VALUE 5. PROCEDURE DIVISION. IF A-F IS GREATER THAN 3 AND A-F IS LESS THAN 10 DISPLAY "in range" END-IF. IF A-F IS LESS THAN 3 OR A-F IS GREATER THAN 10 DISPLAY "out of range" END-IF. STOP RUN.Answer
in range
Five is above 3 and below 10, so the first condition is true and the second is false.
-
A reader writes IF A NOT = 1 OR 2 expecting "A is neither one nor two". What is wrong?
Answer
The abbreviation repeats NOT =, so the condition is true for every value
That is the negated abbreviation on this page, which printed true for both 1 and 2.
The code on this page was built with cobc (GnuCOBOL) 3.2.0 and run on Alpine Linux 3.24.3 (x86_64) in a Docker container.
Article author: Arthur Isaev