19

This answer made me wonder:

Did COBOL also handle that other newbie mistake, allowing one to write

if a == 'orange' or 'apple' or 'banana'

instead of

if a == 'orange' or a == 'apple' or a == 'banana'

(or using some set membership operator if your favourite language has one)?

Ture Pålsson
  • 293
  • 2
  • 5
  • 1
    Possibly something new with the IS. I can't recall ever having used an IS in a condition. Possibly a is equal to 'orange' or equal to 'apple' or equal to 'banana' . I'll have to install a cobol compiler and find out. – cup Jun 24 '21 at 10:57
  • 1
    What is the mistake? Both of those are equivalent in COBOL. – mannaggia Jun 24 '21 at 10:58
  • 2
    i meant those code snippets to be interpreted as if they were written in some language popular today, like Python or Java, where they are definitely not equivalent, but often trip people up, as witnessed by the number of dupes of this question. – Ture Pålsson Jun 24 '21 at 11:01
  • 1
    That is, both are equivalent, except COBOL uses = (or EQUALS) for equality, not ==. – mannaggia Jun 24 '21 at 11:02
  • 6
    Why the past tense? COBOL is, even though it is an ancient language, still very much in active use today. There are a fair number of retired COBOL programmers that earn a nice supplement to their pension doing consultancy, because their expertise is still in very high demand and there are very very few youngsters that have that expertise or are willing to learn. – Tonny Jun 24 '21 at 21:54

4 Answers4

29

The language defined in the original COBOL report from 1960 did indeed (see section 3.2.2). A normal “compound condition” consisted of a series of “simple conditions” (relations) separated by AND and OR; however, there were two abbreviated forms:

  1. An expression like X = Y AND X = Z could be abbreviated to X = Y AND Z. The report gives the tricky example A = B OR C AND D, which turns out to be equivalent to A = B OR (A = C AND A = D).
  2. When one logical operator is used throughout, you can replace all but the last with commas. Thus A = B, C OR D is the same as A = B OR A = C OR A = D.
texdr.aft
  • 3,495
  • 1
  • 19
  • 42
15

COBOL also has Level 88 conditions. Not quite what you are asking, but related.

For example:

01 WS-FRUIT      PIC X(20).
   88 APPLE      VALUE "Apple".
   88 BANANA     VALUE "Banana".
   88 ORANGE     VALUE "Orange".

Now I can use:

IF APPLE OR BANANA OR ORANGE

The variable name is not even necessary because it knows which variable those level 88 conditions are associated with.

I can also do this:

01 WS-FOOD      PIC X(20).
   88 FRUIT      VALUE "Apple" "Banana" "Orange.
   88 VEGGIE     VALUE "Corn" "Bean" "Potato".
   88 SNACK      VALUE "Pretzels" "Chips".

Then I can do this:

 MOVE "Bean" TO WS-FOOD.
 ..
 ..
 IF VEGGIE
   DISPLAY "It's a vegetable"
 ELSE
   DISPLAY "Not a vegetable".
mannaggia
  • 3,264
  • 2
  • 16
  • 15
  • 3
    88 Level conditionals were amazingly useful at simplifying huge, broken IF clauses. COBOL was an amazingly effective language if you were taught by experts, and didn't blanch at using GOTO. – RonJohn Jun 25 '21 at 03:49
  • @RonJohn elaborate if I'm wrong, but this looks like just a hackish way to get the same thing you could do more flexibly by simply defining some boolean variables, or perhaps nullary boolean-valued lambdas, and set-membership queries. – leftaroundabout Jun 25 '21 at 08:40
  • @leftaroundabout you call it hackish, but i call it brilliant. (One time, I replaced a densely-packed, 30-line long -- and broken -- IF clause with six clean lines of code using 88 conditionals. The end result read like a sentence. And worked!) – RonJohn Jun 25 '21 at 09:24
  • 2
    @RonJohn well, whoever thought a 30-line if statement could possibly be a good idea in the first place was either incompetent (in which case it's not too surprising that it can be refactored to a fraction of the size), or was forced to that desparate move by the limitations of a language that is more concerned with having the code read “like English” than implementing actually efficient, consistent semantics. Anyway, how you find these 88-thingies read “like a sentence” eludes me... the example here certainly isn't readable, despite being easily expressable as very English-like e.g. Python. – leftaroundabout Jun 25 '21 at 10:07
  • 6
    @l "whoever thought a 30-line if statement could possibly be a good idea in the first place" was writing corporate income tax software. I don't know about your country's tax laws, but they're damned complicated in the US. – RonJohn Jun 25 '21 at 10:11
  • @leftaroundabout how you find these 88-thingies read “like a sentence” eludes me. because you've never read COBOL that makes use of 88 conditionals to simplify complex sets of predicates. – RonJohn Jun 25 '21 at 10:12
  • @RonJohn you keep missing the point I'm making. No matter how complicated the logic you're trying to express, it's never good to put it in a single if statement when that makes it a 30-line monster. Any language worth its salt has mechanisms to factor such logic into smaller, simpler, more readable components. ... – leftaroundabout Jun 25 '21 at 11:05
  • 1
    Now, indeed I never had to read COBOL, so I could be wrong, but the fact that it has a built-in construct for this particular situation seems a red flag indicating that they needed to improvise to avoid such if statements, which are really just a very specific instance of the refactorability problem. Apparently 88 gets the job done, but it wouldn't work in many other situations, and it looks like it doesn't come with any of the other advantages of OO or functional programming – like making it easy to write unit tests. – leftaroundabout Jun 25 '21 at 11:05
  • @l "Any language worth its salt has mechanisms to factor such logic into smaller, simpler, more readable components. " and in this case for this purpose..., it's 88 Conditionals. "it looks like it doesn't come with any of the other advantages of OO or functional programming" LOL. How old are you ? – RonJohn Jun 25 '21 at 11:15
  • I found level 88 useful in COPY books, either when defining file descriptions, or when you had to muck around with system level interfaces and external interfaces, to hide/abstract things like magic values. For example, if you had to interface or read data from files supplied externally, and let's say a certain field had various values, like 1, 2, 3 and each had a different meaning, level 88 could be used to define names for those values instead of hard coding them into the program. Seems more readable to say IF TAX-RECORD than IF IMP-SALES-RECORD-TYPE = "3". – mannaggia Jun 25 '21 at 14:12
  • 2
    @RonJohn these kids today... In my day we had to code uphill barefoot in the snow... – barbecue Jun 25 '21 at 15:18
  • @barbecue snow? Luxury! We had to pull ourselves up a glacier using only our tongues every morning... — Seriously though, Lisp and Simula are both almost as old as COBOL, so there's nothing particularly hilarious about comparing COBOL features with OO and FP. – leftaroundabout Jun 25 '21 at 19:35
  • @leftoundabout the thing you need to keep in mind is that back then, changing core language features would get you fired in a heartbeat. I'm not sure you fully appreciate just how incredibly different programming was back then. – barbecue Jun 25 '21 at 19:45
  • @RonJohn Having had to maintain code for many years, it was not only a good idea to have the switch (being the if statement, imagined as a shunting yard) in the one place and not distributed in 15 separate programs, but it was generally important to sort by descending probability as well to get the short circuiting benefit. A good design would have the same order in the truth table as an EVALUATE A ALSO B ALSO C .... statement. The exception was bit-mapping first which we understand, but Business Analysts might not (along with de Morgan's theorem being applied). – mckenzm Jun 26 '21 at 19:02
  • @barbecue you coded on graph paper and then punched it in using an 029? – RonJohn Jun 26 '21 at 21:53
  • @RonJohn - and we liked it that way! We loved it!!! :-) – Bob Jarvis - Слава Україні Jun 27 '21 at 15:16
  • 1
    @RonJohn only briefly, then we got Vaxes and VT-100s. – barbecue Jun 27 '21 at 21:04
  • @barbecue I loved programming on the VAX (and later Alpha). Such a full-featured environment... – RonJohn Jun 28 '21 at 01:01
6

You can do

if a = 'orange' or 'apple' or 'banana'

in COBOL, it translates as

if a = 'orange' or a = 'apple' or a ='banana'

You need to be careful when doing this as it does not always work the way you expect when you mix in And or not clauses. I would suggest using brackets and keeping it simple i.e.

if (a == 'orange' or 'apple' or 'banana')
and fruit-age < 12

This question https://stackoverflow.com/questions/4342218/issues-with-ands-and-ors-cobol/4342263 shows an example of the problems.

Taken from the above

IF DL-CLASS-STANDING = 'First Yr' OR 'Second Yr' AND
GRAD-STAT-IN = ' ' OR 'X'

From memory means

IF DL-CLASS-STANDING = 'First Yr'
OR (DL-CLASS-STANDING = 'Second Yr' AND  GRAD-STAT-IN = ' ')
OR GRAD-STAT-IN ='X'
Bruce Martin
  • 161
  • 3
3

COBOL has an EVALUATE statement that takes the first form (need to use ALSO for multiple conditions).

EVALUATE TRUE ALSO TRUE
WHEN WS-A = 'ORANGE' OR 'APPLE' OR 'BANANA' ALSO ANY
  DISPLAY 'A FRUIT.'
WHEN WS-A = 'ONION' OR 'PEPPER' ALSO WS-COURSE = 'DESSERT'
  DISPLAY 'A VEGETABLE. DO NOT SERVE THIS FOR DESSERT!'
END-EVALUATE
Rick Smith
  • 513
  • 3
  • 7
Brian
  • 822
  • 9
  • 9