Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations TouchToneTommy on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

and/or relational checks in cobol 3

Status
Not open for further replies.

leewest

Programmer
Mar 19, 2007
6
US
I have the following COBOL statement.
004250 IF SW-PRINT = 1
AND HLD-REC-TYPE = '5' OR '4' OR '6'
R11725 MOVE PRT-LINE TO HD-LINE
R11725* MOVE STAA-PRT-LINE TO HD-LINE
004270 PERFORM P1000-HEADING
011725 MOVE HD-LINE TO PRT-LINE
R11725* MOVE HD-LINE TO STAA-PRT-LINE
004280 GO TO W0005-CONTINUE.

My understanding is;
If SW-PRINT = 1 the AND statements are checked.
if HOLD-REC-TYPE does not happen to be '5' the checks for '4' and '6' will not be executed because of
not having been enclosed with (). Is this the correct understanding or am I incorrect?
Thanks
 
My suggestion: always use parens when mixing AND and OR operators:
IF SW-PRINT = 1
AND (HLD-REC-TYPE = '5' OR '4' OR '6')

Hope This Helps, PH.
Want to get great answers to your Tek-Tips questions? Have a look at FAQ219-2884 or FAQ181-2886
 
Your question has already been answered in a FAQ: faq209-1604.
leewest said:
Is this the correct understanding or am I incorrect?
[small]Ok, I can't help myself![/small]

The answer is, "Yes."

[small]Ask an OR question, get an OR answer.

Sorry... :-D [/small]


Tom Morrison
 
Think of your posted condition like this:
IF SW-PRINT = 1 AND HLD-REC-TYPE = '5'
OR HLD-REC-TYPE = '4'
OR HLD-REC-TYPE = '6'

Is it clearer for you ?
BTW, in my opinion your understanding is not correct ...

Hope This Helps, PH.
Want to get great answers to your Tek-Tips questions? Have a look at FAQ219-2884 or FAQ181-2886
 
Thank you, you stated what I have known for years.
However, for the remainder of the question.
If the first check is not true, will the second and third
check be made? Or does control pass to the next statement
ending the IF statement too soon and distorting expected output?

thanks
 
The IF condition is true if ANY of the following is true:
1) SW-PRINT = 1 AND HLD-REC-TYPE = '5'
2) HLD-REC-TYPE = '4' (even if SW-PRINT NOT = 1)
3) HLD-REC-TYPE = '6' (even if SW-PRINT NOT = 1)

So, anyway, without parens, you don't have expected output ...

Hope This Helps, PH.
Want to get great answers to your Tek-Tips questions? Have a look at FAQ219-2884 or FAQ181-2886
 
PH

Thanks for your input. You say to always use the parens, but you did not use parens in your second example. any reason why? Years ago I though I recalled to always use parens in the AND/OR relation when either the AND or the OR contained multiples, that control whould stop when the first not true was met and would not check the others. Perhaps I recall incorrectly.
Again thanks

lw
 
leewest,

Did you read the FAQ: FAQ209-1604?

The test must evaluate the expression, left-to-right subject to parentheses (which you do not have) and operator precedence, until the truth value of the conditional expression can be determined with certainty.

Operator precedence has AND higher than OR. Therefore, since the left-hand boolean expression of the AND is true ([tt]SW-PRINT = 1[/tt]), the value of the entire conditional expression depends entirely on the value of the right-hand boolean expression, viz. [tt]HLD-REC-TYPE = '5' OR '4' OR '6'[/tt].

The boolean expression [tt]HLD-REC-TYPE = '5' OR '4' OR '6'[/tt] uses abbreviated combined conditions. The full boolean expression is [tt]HLD-REC-TYPE = '5' OR HLD-REC-TYPE = '4' OR HLD-REC-TYPE = '6'[/tt]. A boolean expression composed of ORs is true as soon as one of the terms is true. So, since the expression by standard must be evaluated left-to-right, if HLD-REC-TYPE not = '5' the value of the expression remains unknown, causing the evaluation of HLD-REC-TYPE = '4', and so forth.

The honorable PHV said:
in my opinion your understanding is not correct ...
It is not an opinion, but a fact: your understanding is incorrect.

Tom Morrison
 
ph

you say the IF condition is true if ANY of the following is true:
1) SW-PRINT = 1 AND HLD-REC-TYPE = '5'
2) HLD-REC-TYPE = '4' (even if SW-PRINT NOT = 1)
3) HLD-REC-TYPE = '6' (even if SW-PRINT NOT = 1)

The was this statement is currently written, not using parens, you may be right. But, I think the checks of HLD-REC-TYPE should have been inside parens making it true only if;

1) SW-PRINT = 1 AND HLD-REC-TYPE = '5'
2) SW-PRINT = 1 AND HLD-REC-TYPE = '4'
3) WS-PRINT = 1 AND HLD-REC-TYPE = '6'

Unless things have changed the checks for HLD-REC-TYPE will not even be made unless SW-PRINT is = 1. Again, a point in favor of always using the parens for clarity.

Your thoughts?

lw
 
lw,

[small]though maybe I am being ignored...[/small]

[blush] extremely [blush]

I just talked to my compiler guy, because I always make this mistake with stupid abbreviated combined conditions. The eye sees parentheses where none can be put.

Code:
IF (SW-PRINT = 1 AND HLD-REC-TYPE = '5') OR 
   (                 HLD-REC-TYPE = '4') OR
   (                 HLD-REC-TYPE = '6')

So the 19 Mar 07 16:39 posting of PHV is spot-on.

The lesson of all this is that one should never, ever use abbreviated combined conditions because they cause a maintenance nightmare. The eye can incorrectly impute parentheses to the area with omitted subjects/operators.pr

Tom Morrison
 
Tom, are you saying that if SW-PRINT NOT = 1 then no evaluation of HLD-REC-TYPE is done ?
If so, my (old) MFC compiler is not compliant ...
 
Oh, sorry Tom, didn't refresh before posting ...
The lesson of all this is that one should never, ever use abbreviated combined conditions
Sorry to disagree.
With proper parentheses (as in my post stamped 19 Mar 07 15:42) there is no ambiguity.

Hope This Helps, PH.
Want to get great answers to your Tek-Tips questions? Have a look at FAQ219-2884 or FAQ181-2886
 
k5tm

Your input so far seems most reasonable. We agree that unless SW-PRINT = 1 then no evaluation of HLD-REC-TYPE is done. However, you say since the expression by standard must be evaluated left-to-right, if HLD-REC-TYPE not = '5' the value of the expression remains unknown, causing the evaluation of HLD-REC-TYPE = '4', and so forth. But, I was under the impression control stopped at the first untrue, thus if HLD-REC-TYPE is not '5' no check will be made of the '4' and the '6'. I have not read faq209-1604 but am heading there now.
 
leewest, your posted expression has no parens, so the result should be as I explain in my post stamped 19 Mar 07 16:39 (thanks to Tom to have confirmed that).
Bottom line, I'm convinced that your code gives unexpected result ...
 
phv

Well, I guess I need to start over. First of all, the code is NOT my code. Were it my code it would be written correctly using the parens for clarity. That said, the code resides in a program I have been assigned to make changes in and the statement is not one appearing on my specs. However, I noticed it because it was a couple of lines lower and it caught my attention because of having so many problems with this type of statement over the years. I appreciate your input on all of this but sill need to clarify; if SW-PRINT is not = 1 control simply falls through to the next statement. Howevere, if SW-PRINT is = 1, and the first OR expression is not, will control continue to check the second OR expression, and if necessary the third? I have always felt control would stop if the first of multiple expressions was untrue unless parens had been used.
lw
 
PH,

It is not an issue of ambiguity, and in relatively simple situations, involving only AND or OR but not both, abbreviated combined conditions may be read without ambiguity.

I would suggest that we put the expression in the OP on a code reading challenge. The miss rate will be high.

Tom Morrison
 
if SW-PRINT is not = 1 control simply falls through to the next statement
I don't think so, as I've explained you in the posts stamped 19 Mar 07 16:10 and 19 Mar 07 16:39.
In fact, the code you posted seems buggy to me.
 
To ans your ques another way:

If SW-PRINT is not = 1 no other comparisons are done If it is = to 1 and HLD-REC-TYPE = '5' no other comparisons are done. If HLD-REC-TYPE is not = '5', compare HLD-REC-TYPE for '4', if not = '4', compare for '6', if not = by-pass rest of sentence.

Regards, Jack.

"A problem well stated is a problem half solved" -- Charles F. Kettering
 
If SW-PRINT is not = 1 no other comparisons are done
Again, I don't think so.
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top