Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Issues with ANDs and ORs (COBOL)

I can't seem to get this one part right. I was given a input file with a bunch of names, some of which I need to skip, with extra information on each one. I was trying use ANDs and ORs to skip over the names I did not need and I came up with this.

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

It got rid of all but one person, but when I tried to add another set of ANDs and ORs the program started acting like the stipulations where not even there.

Did I make it too complex for the compiler? Is there an easier way to skip over things?

like image 387
Kimmy1235 Avatar asked Dec 03 '10 03:12

Kimmy1235


2 Answers

Try adding some parentheses to group things logically:

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

like image 99
Joel Spolsky Avatar answered Sep 19 '22 17:09

Joel Spolsky


You may want to look into fully expanding that abbreviated expression since the expansion may not be what you think when there's a lot of clauses - it's often far better to be explicit.


However, what I would do is use the 88 level variables to make this more readable - these were special levels to allow conditions to be specified in the data division directly rather than using explicit conditions in the code.

In other words, put something like this in your data division:

03  DL-CLASS-STANDING  PIC X(20).     88 FIRST-YEAR          VALUE 'First Yr'.     88 SECOND-YEAR         VALUE 'Second Yr'. 03  GRAD-STAT-IN       PIC X.     88 GS-UNKNOWN          VALUE ' '.     88 GS-NO               VALUE 'X'. 

Then you can use the 88 level variables in your expressions:

IF (FIRST-YEAR OR SECOND-YEAR) AND (GS-UNKNOWN OR GS-NO) ... 

This is, in my opinion, more readable and the whole point of COBOL was to look like readable English, after all.

like image 28
paxdiablo Avatar answered Sep 18 '22 17:09

paxdiablo