Portal | Manuals | References | Downloads | Info | Programs | JCLs | Mainframe wiki | Quick Ref
IBM Mainframe Computers Forums Index
 
Register
 
IBM Mainframe Computers Forums Index Mainframe: Search IBM Mainframe Forum: FAQ Memberlist Profile Log in to check your private messages Log in
 
In what order the below IF statement evaluated

 
Post new topic   Reply to topic    IBMMAINFRAMES.com Support Forums -> COBOL Programming
View previous topic :: :: View next topic  
Author Message
socker_dad

Active User


Joined: 05 Dec 2006
Posts: 147
Location: Columbia, MO

PostPosted: Wed Sep 24, 2008 5:15 am    Post subject: In what order the below IF statement evaluated
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: 8165
Location: East Dubuque, Illinois, USA

PostPosted: Wed Sep 24, 2008 6:57 am    Post subject:
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

Site Director


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

PostPosted: Wed Sep 24, 2008 7:07 am    Post subject:
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: 2150
Location: At my coffee table

PostPosted: Wed Sep 24, 2008 7:15 am    Post subject: Re: Highly Foolish Question
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

Senior Member


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

PostPosted: Wed Sep 24, 2008 7:38 am    Post subject:
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: 2150
Location: At my coffee table

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

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

Senior Member


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

PostPosted: Wed Sep 24, 2008 8:16 am    Post subject:
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: 2150
Location: At my coffee table

PostPosted: Wed Sep 24, 2008 8:21 am    Post subject:
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

Senior Member


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

PostPosted: Wed Sep 24, 2008 8:34 am    Post subject:
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: 147
Location: Columbia, MO

PostPosted: Tue Sep 30, 2008 12:40 am    Post subject: Reply to: In what order the below IF statement evaluated
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

Site Director


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

PostPosted: Tue Sep 30, 2008 2:56 am    Post subject:
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    IBMMAINFRAMES.com Support Forums -> COBOL Programming All times are GMT + 6 Hours
Page 1 of 1

 

Search our Forum:

Similar Topics
Topic Author Forum Replies Posted
No new posts WER268A OUTREC STATEMENT : SYNTAX E... frozenblood87 SYNCSORT 12 Sat Aug 26, 2017 9:45 pm
No new posts Sorting on text - but in non-alphabet... Roy Ware SYNCSORT 5 Wed Aug 23, 2017 9:15 pm
No new posts Suppress value reported by BREAK in D... Learncoholic DFSORT/ICETOOL 3 Wed Aug 16, 2017 6:03 pm
No new posts ON 2 AND EVERY 1 - Statement ??? UmeySan COBOL Programming 2 Tue Jul 25, 2017 1:20 pm
No new posts ROUNDED Problem with COMPUTE statement shalem COBOL Programming 11 Thu Feb 09, 2017 8:16 pm

Facebook
Back to Top
 
Job Vacancies | Forum Rules | Bookmarks | Subscriptions | FAQ | Polls | Contact Us