EVAL Math Problem

605 pts.
iSeries development
Bruce, can you figure out this piece of code? I don't understand the result I get. Before code is executed: XXAVAL = 0, LTSTHQ = 30, LTCUSQ = 30, LTPICQ = 0, XPQTY = 30, OLOLQT = 30
After code is executed: XXAVAL = 0. I figured XXAVAL should be 60?

Answer Wiki

Thanks. We'll let you know when a new response is added.

The hierarchy of the EVAL states that all calculations within parentheses are done first. Your eval would translate to:

XXAVAL = 30 - (30 + 0 + 30) + 30 which would then become
XXAVAL = 30 - 60 + 30 whci would give a value of 0.


Parenthesis are indeed done first. For the “rest of the story” see Operation Precedence in the ILE RPG Reference.

Bruce Vining

Thanks, guys!
Well, my brain doesn’t always see what the my eyes show it. I copied that line of code direct from the source member. What I didn’t see was that the minus sign is abutted against the paren.

That changes the operation from a binary operation (subtract) to a unary operation (ie. like z-sub).

That’s the real problem/answer, not Operation Precedence, but Expression Operators!

So, the real formula is:

XXAVAL= 30 + (60-) + 30 making XXAVAL = zero.

I’ll remember to check for spaces in a formula, ’cause it changes + and -.

Discuss This Question: 1  Reply

There was an error processing your information. Please try again later.
Thanks. We'll let you know when a new response is added.
Send me notifications when members answer or reply to this question.
  • Sloopy
    Kevin, you are wrong. It's Operator Precedence that gives the zero result, just as has been explained. The position of the minus sign is nothing to do with it. Abutting the sign against the parenthesis does NOT turn it into a 'unary' operator - try changing your code and see. How do you get this: "XXAVAL= 30 + (60-) + 30 making XXAVAL = zero." From this: XXAVAL= LTSTHQ-(LTCUSQ+LTPICQ+XPQTY)+OLOLQT ??? Where did your new plus sign come from? It just appeared from nowhere! If the code actually WAS XXAVAL= LTSTHQ+ -(LTCUSQ+LTPICQ+XPQTY)+OLOLQT then you would be correct. But that's not what you have got. So, please understand what advice you are being given, because if you continue to think that evaluation depends on operator spacing you will end up making some serious mistakes. Regards, Sloopy
    2,195 pointsBadges:

Forgot Password

No problem! Submit your e-mail address below. We'll send you an e-mail containing your password.

Your password has been sent to:

To follow this tag...

There was an error processing your information. Please try again later.

Thanks! We'll email you when relevant content is added and updated.


Share this item with your network: