× The internal search function is temporarily non-functional. The current search engine is no longer viable and we are researching alternatives.
As a stop gap measure, we are using Google's custom search engine service.
If you know of an easy to use, open source, search engine ... please contact support@midrange.com.



I have a question about Operation Precedence in RPG-IV.  FWIW, v5r2.  I have 
found the chapter on
Expressions in the Reference and, specifically, the sections titled "Operation 
Precedence" and
"Short Circuit Evaluation".  Upon my brief foray into these topics, it appears 
to me that the two
sections permit a contradiction to the rules they set forth.

"Operation Precedence" says:

The precedence of operations determines the order in which operations are 
performed within
expressions. High precedence operations are performed before lower precedence 
operations.

The following list indicates the precedence of operations from highest to 
lowest:
  1. ()
  2. Built-in functions, user-defined functions
  3. unary +, unary -, NOT
  4. **
  5. *, /
  6. binary +, binary -
  7. =, <>, >, >=, <, <=
  8. AND
  9. OR

"Short Circuit Evaluation" says:

Relational operations AND and OR are evaluated from left to right. However, as 
soon as the value
is known, evaluation of the expression stops and the value is returned. As a 
result, not all
operands of the expression need to be evaluated.

For operation AND, if the first operand is false, then the second operand is 
not evaluated.
Likewise, for operation OR, if the first operand is true, the second operand is 
not evaluated.

There are two implications of this behaviour. First, an array index can be both 
tested and used
within the same expression. The expression
      I<=%ELEM(ARRAY) AND I>0 AND ARRAY(I)>10
will never result in an array indexing exception.

<my comments/questions follow>

If AND & OR are always the lowest precedence, how can the array index example 
above work? 
According to the rules of precedence, as I interpret them, all of the operands:
      I<=%ELEM(ARRAY) 
      I>0 
      ARRAY(I)>10
would be evaluated before the AND relational operators.  If 'I' is zero, I 
would expect a run-time
error on the ARRAY(I)>10 because the > operator has a higher precedence than 
the AND operator.

BTW, I know from experience that this example works.  I am questioning whether 
the precedence of
operations really should be re-arranged to place AND & OR higher in the list.

- Dan

__________________________________________________
Do you Yahoo!?
Yahoo! Platinum - Watch CBS' NCAA March Madness, live on your desktop!
http://platinum.yahoo.com

As an Amazon Associate we earn from qualifying purchases.

This thread ...

Follow-Ups:

Follow On AppleNews
Return to Archive home page | Return to MIDRANGE.COM home page

This mailing list archive is Copyright 1997-2024 by midrange.com and David Gibbs as a compilation work. Use of the archive is restricted to research of a business or technical nature. Any other uses are prohibited. Full details are available on our policy page. If you have questions about this, please contact [javascript protected email address].

Operating expenses for this site are earned using the Amazon Associate program and Google Adsense.