I long ago decided never to bother looking up or thinking about operator 
precedence. If I am not immediately certain straight out of the box then I use 
parentheses.

Charles


-----Original Message-----
From: IBM Mainframe Discussion List [mailto:[email protected]] On Behalf 
Of Bernd Oppolzer
Sent: Friday, June 5, 2020 12:23 PM
To: [email protected]
Subject: Re: COBOL Question

I hope you don't mind if I comment once again;

my original coding was:

         IF TVOLL (IND1) NOT = HIGH-VALUE
         AND SMOD (IND1) = 'B' OR 'R'

and as I learned now from your helpful posts,
this is expanded to

         IF TVOLL (IND1) NOT = HIGH-VALUE
         AND SMOD (IND1) = 'B' OR SMOD (IND1) = 'R'

(which would have alarmed me, if I had seen it in this variant, BTW)
and then, because of operator precedence, evaluated as

         IF (TVOLL (IND1) NOT = HIGH-VALUE
         AND SMOD (IND1) = 'B')
         OR SMOD (IND1) = 'R'

so that in my case (SMOD ... being 'R') the condition evaluated to true,
which was not what I had expected.

This is a little bit counter intuitive IMHO, because the abbreviation
'B' OR 'R' suggests that the list of values both rely to the right 
condition (only)
and so I thought the evaluation would be

         IF TVOLL (IND1) NOT = HIGH-VALUE
         AND (SMOD (IND1) = 'B' OR 'R')

I expected somehow an implicit paranthese because of the abbreviation ...

What I would take as a conclusion (for me) from this experience:
it is best to use parantheses, especially if there is a combination
of AND and OR ... and when using abbreviations.

Thanks again, kind regards
have a nice weekend

Bernd



Am 05.06.2020 um 20:48 schrieb Bob Bridges:
> Seems to me that ~is~ operator precedence:  We evaluate AND before OR, just 
> as we evaluate * before +.  But that's closely related to the distributive 
> rule, right?
>
>     P and Q or R
>     R or P and Q
>
> ...both evaluate the same way, to "(P and Q) or R".  The distributive 
> property says that
>
>     P and (Q or R)
>
> ...evaluates to
>
>     (P and Q) or (P and R)
>
> I'm just as rusty in COBOL as Mr Oppolzer, so I didn't know you could say
>
>     IF VAR = 'B' OR 'R'
>
> But if you can, it must mean "IF VAR = 'B' OR VAR = 'R'".
>
> ---
> Bob Bridges, [email protected], cell 336 382-7313
>
> /* No one would talk much in society if he knew how often he misunderstands 
> others.  -Goethe */
>

----------------------------------------------------------------------
For IBM-MAIN subscribe / signoff / archive access instructions,
send email to [email protected] with the message: INFO IBM-MAIN

----------------------------------------------------------------------
For IBM-MAIN subscribe / signoff / archive access instructions,
send email to [email protected] with the message: INFO IBM-MAIN

Reply via email to