IBM Mainframe Forum Index
 
Log In
 
IBM Mainframe Forum Index Mainframe: Search IBM Mainframe Forum: FAQ Register
 

In what order the below IF statement evaluated


IBM Mainframe Forums -> COBOL Programming
Post new topic   Reply to topic
View previous topic :: View next topic  
Author Message
socker_dad

Active User


Joined: 05 Dec 2006
Posts: 177
Location: Seattle, WA

PostPosted: Wed Sep 24, 2008 5:15 am
Reply with quote

This section of code was questioned in a code review.

Original code (yes, the parentheses are exactly as in the program):

Code:

IF (MAINTENANCE)  AND
              (AUDIT-OFFENSE-3 = SDC01-ANSI-CD)   OR
              (AUDIT-OFFENSE-3 = SDC01-ACD-CD)

   PERFORM THIS STUFF.




I replaced it with this:

Code:


IF   MAINTENANCE
AND  DL-AUDIT-OFFENSE-3 = SDC01-ANSI-CD
OR   DL-AUDIT-OFFENSE-3 = SDC01-ACD-CD


     PERFORM THIS STUFF
END-IF.


The reviewers had two questions:

1. In what order is the IF statement evaluated?

2. Does removing the parentheses from the conditions affect the evaluation?


My answers were:

1. I don't know - it is evaluating correctly in production at this time and will continue to do so.

2. No. The IF will continue to evaluate the exact same way. Placing parenthese around the individual conditions do not in any way give them any kind of precedence. Now, if we were to group the second and third lines of the IF, it would change the statement.

Am I right on this????
Back to top
View user's profile Send private message
Robert Sample

Global Moderator


Joined: 06 Jun 2008
Posts: 8696
Location: Dubuque, Iowa, USA

PostPosted: Wed Sep 24, 2008 6:57 am
Reply with quote

From the Language Reference manual:
Quote:
Unless modified by parentheses, the following precedence rules (from highest to lowest) apply:

1. Arithmetic operations
2. Simple conditions
3. NOT
4. AND
5. OR
Back to top
View user's profile Send private message
dick scherrer

Moderator Emeritus


Joined: 23 Nov 2006
Posts: 19244
Location: Inside the Matrix

PostPosted: Wed Sep 24, 2008 7:07 am
Reply with quote

Hello,

Surely not highly foolish. . . Probably not even foolish icon_cool.gif

Last things first - i'd agree with 2. Both IFs will function the same way. Also agree that placing parentheses around
Code:
DL-AUDIT-OFFENSE-3 = SDC01-ANSI-CD
OR   DL-AUDIT-OFFENSE-3 = SDC01-ACD-CD
would change the way the statement executes.

As coded, i believe the code will be the same as
Code:
IF   (MAINTENANCE
AND  DL-AUDIT-OFFENSE-3 = SDC01-ANSI-CD)
OR   DL-AUDIT-OFFENSE-3 = SDC01-ACD-CD


     PERFORM THIS STUFF
END-IF.

My development system is "offline" until morning, so i can't run a test.

I believe evaluation will be from the left.

I'm sure we'll hear if i've stepped on my hand icon_smile.gif
Back to top
View user's profile Send private message
CICS Guy

Senior Member


Joined: 18 Jul 2007
Posts: 2146
Location: At my coffee table

PostPosted: Wed Sep 24, 2008 7:15 am
Reply with quote

socker_dad wrote:
Code:

IF (MAINTENANCE)  AND
              (AUDIT-OFFENSE-3 = SDC01-ANSI-CD)   OR
              (AUDIT-OFFENSE-3 = SDC01-ACD-CD)

   PERFORM THIS STUFF.

In this case the parens don't mean a thing....
But, looking at the IF, I wonder if the intention was more this:
Code:

IF (MAINTENANCE)  AND
              (AUDIT-OFFENSE-3 = SDC01-ANSI-CD   OR
               AUDIT-OFFENSE-3 = SDC01-ACD-CD)

   PERFORM THIS STUFF.

?????
Back to top
View user's profile Send private message
Anuj Dhawan

Superior Member


Joined: 22 Apr 2006
Posts: 6250
Location: Mumbai, India

PostPosted: Wed Sep 24, 2008 7:38 am
Reply with quote

Hi,

Per me you were right with your explanation. Lets' take rather simple (variables) with Boolean Algebra (BA) in mind..

Code:
IF A AND B OR C
PERFORM THIS STUFF.
is equivalent to
Code:
 IF (A AND B) OR C
PERFORM THIS STUFF.


BA says, AND would be "true (1)" if both the "inputs" to it are true else its output is false (0) . In OR, output would be true if any one or both of the inputs are true & yes precedence is given to AND over OR.

If above lines make sense then
Code:
A AND B OR C= A*B+C
obviously A*BA is executed first which is nothing but (A*BA)+C. So keeping parentheses or not will not change the results. Hopefully "clients" are educated enough to understand this multiplication & Addition.

And Yes this
Quote:
Placing parenthese around the individual conditions
doesn't change anything..
Back to top
View user's profile Send private message
CICS Guy

Senior Member


Joined: 18 Jul 2007
Posts: 2146
Location: At my coffee table

PostPosted: Wed Sep 24, 2008 8:08 am
Reply with quote

Pardon, but who are you responding to?
Back to top
View user's profile Send private message
Anuj Dhawan

Superior Member


Joined: 22 Apr 2006
Posts: 6250
Location: Mumbai, India

PostPosted: Wed Sep 24, 2008 8:16 am
Reply with quote

Well, I didn't realize it, I thought if not any name in Salutation it would mean for OP.

- Ad
Back to top
View user's profile Send private message
CICS Guy

Senior Member


Joined: 18 Jul 2007
Posts: 2146
Location: At my coffee table

PostPosted: Wed Sep 24, 2008 8:21 am
Reply with quote

Anuj D. wrote:
Per me you were right with your explanation.
I'm sorry, it was the above that confused me....iYour question vs. your explanation....
Back to top
View user's profile Send private message
Anuj Dhawan

Superior Member


Joined: 22 Apr 2006
Posts: 6250
Location: Mumbai, India

PostPosted: Wed Sep 24, 2008 8:34 am
Reply with quote

Actually i opened this thread long back in office (when I opened it no reply was there) & then did a lots of stuff in between before posting, my post was ready with preview pain so just clicked on "submit" as soon as I reached home...Well, so not you I'm sorry.. icon_redface.gif
Back to top
View user's profile Send private message
socker_dad

Active User


Joined: 05 Dec 2006
Posts: 177
Location: Seattle, WA

PostPosted: Tue Sep 30, 2008 12:40 am
Reply with quote

Well, you are quite correct.

The idea was to only evaluate the offense codes when the MAINTENANCE condition was true; however, because the original programmer failed to group the second and third lines together correctly, it doesn't work that way!

If it is a maintenance transaction and the Offense Code = ANSI Code, it will do the stuff in the IF statement.

If it is a maintenance transaction and the Offense Code = ACD Codem it will do the stuff in the IF statement.

So far, so good.

But when the transaction is NOT a maintenance transaction, but the Offense Code = ACD Code, it does the stuff in the IF statement - not as was intended.

This code has been in production for 10 years and this is the first time anyone has noticed that it may not be correct.

We are definitely producing a VERY important report! icon_lol.gif
Back to top
View user's profile Send private message
dick scherrer

Moderator Emeritus


Joined: 23 Nov 2006
Posts: 19244
Location: Inside the Matrix

PostPosted: Tue Sep 30, 2008 2:56 am
Reply with quote

Hello,

Quote:
We are definitely producing a VERY important report!
Wait 'til the user(s) are disconcerted by the change in the content of the report. . . . icon_wink.gif
Back to top
View user's profile Send private message
View previous topic :: :: View next topic  
Post new topic   Reply to topic View Bookmarks
All times are GMT + 6 Hours
Forum Index -> COBOL Programming

 


Similar Topics
Topic Forum Replies
No new posts Rotate partition-logical & physic... DB2 0
No new posts JOIN STATEMENT PERFORMANCE. DFSORT/ICETOOL 12
No new posts DB2 Load - Sort Or order BY DB2 1
No new posts Relate COBOL statements to EGL statement All Other Mainframe Topics 0
No new posts GDG all in sequence order JCL & VSAM 9
Search our Forums:

Back to Top