View previous topic :: View next topic
|
Author |
Message |
socker_dad
Active User
Joined: 05 Dec 2006 Posts: 177 Location: Seattle, WA
|
|
|
|
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 |
|
|
Robert Sample
Global Moderator
Joined: 06 Jun 2008 Posts: 8696 Location: Dubuque, Iowa, USA
|
|
|
|
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 |
|
|
dick scherrer
Moderator Emeritus
Joined: 23 Nov 2006 Posts: 19244 Location: Inside the Matrix
|
|
|
|
Hello,
Surely not highly foolish. . . Probably not even foolish
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 |
|
Back to top |
|
|
CICS Guy
Senior Member
Joined: 18 Jul 2007 Posts: 2146 Location: At my coffee table
|
|
|
|
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 |
|
|
Anuj Dhawan
Superior Member
Joined: 22 Apr 2006 Posts: 6250 Location: Mumbai, India
|
|
|
|
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 |
|
|
CICS Guy
Senior Member
Joined: 18 Jul 2007 Posts: 2146 Location: At my coffee table
|
|
|
|
Pardon, but who are you responding to? |
|
Back to top |
|
|
Anuj Dhawan
Superior Member
Joined: 22 Apr 2006 Posts: 6250 Location: Mumbai, India
|
|
|
|
Well, I didn't realize it, I thought if not any name in Salutation it would mean for OP.
- Ad |
|
Back to top |
|
|
CICS Guy
Senior Member
Joined: 18 Jul 2007 Posts: 2146 Location: At my coffee table
|
|
|
|
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 |
|
|
Anuj Dhawan
Superior Member
Joined: 22 Apr 2006 Posts: 6250 Location: Mumbai, India
|
|
|
|
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.. |
|
Back to top |
|
|
socker_dad
Active User
Joined: 05 Dec 2006 Posts: 177 Location: Seattle, WA
|
|
|
|
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! |
|
Back to top |
|
|
dick scherrer
Moderator Emeritus
Joined: 23 Nov 2006 Posts: 19244 Location: Inside the Matrix
|
|
|
|
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. . . . |
|
Back to top |
|
|
|