+ evaluates RIGHT to LEFT? 
Author Message
 + evaluates RIGHT to LEFT?

Quote:

> The following code (only 5 lines) is written in Borland Pascal 7.0:

> {$B+}
> Function A : Boolean; Begin Write('A'); A := True End;
> Function B : Boolean; Begin Write('B'); B := True End;
> Function C : Boolean; Begin Write('C'); C := True End;
> Begin If A And B And C Then WriteLn End.

> The program writes: CBA
> If the first line is changed to {$B-}, the program outputs: ABC

> Can someone explain why?

Hovik Melikian replied:

Quote:
> Really, very interesting. I think on the same level of priority the compiler
> uses recursion instead of loop. In any case, such important things *must* be
> documented.

and Armin Kuebelbeck said:

Quote:
> So in your example compiler will translate in case of $B- to a code which will
> check boolean expression from A to B to C. In case of $B+ compiler checks from
> last to first expression C->B->A.
> Why? Ask Borland!

Pascal does not guarantee that a boolean expression be evaluated in a
certain order.  In that sense, it *is* documented.  I think the confusion
is caused by C.  The C standard *does* guarantee that a boolean expression
will be evaluated left to right until the truth may be determined.  One of
the first things I learned in Pascal (about 15 years ago) was that you can
*not* say

  IF i <= 10 AND aray[i] < 4.7 THEN

to make sure "i" is within the array bounds.  aray[i] will always be
evaluated, even if i = 11 and the index is out of bounds.

On a related note, you should never assume any expression is evaluated in
a special order.  Compilers are allowed to parse and optimize an expression
any way they want.  If the order is critical, use parentheses.

                                        dsw, fferd
                                        Fred S. Brundick
                                        aka Pascal Postman
                                        USARL, APG, MD.



Sun, 18 Apr 1999 03:00:00 GMT  
 + evaluates RIGHT to LEFT?

Quote:

>   IF i <= 10 AND aray[i] < 4.7 THEN

> to make sure "i" is within the array bounds.  aray[i] will always be
> evaluated, even if i = 11 and the index is out of bounds.

> On a related note, you should never assume any expression is evaluated in
> a special order.  Compilers are allowed to parse and optimize an expression
> any way they want.  If the order is critical, use parentheses.

Please, show me how to use parentheses in your example.

- Hovik Melikian



Sun, 18 Apr 1999 03:00:00 GMT  
 
 [ 2 post ] 

 Relevant Pages 

1. $B+ evaluates RIGHT to LEFT?

2. + evaluates RIGHT to LEFT

3. detecting shift (left right up dn pgup pgdn home end)

4. language right to left problems

5. Report with data flow from left to right

6. UP - DOWN - LEFT - RIGHT (HELP)

7. Report with data flow from left to right

8. Needed: Component to handle currencies while digits appear right-to-left

9. LEFT JOIN: Wrong results when left table is empty

10. Help me evaluate this web database recommendation

11. Comparison always evaluates to TRUE

12. evaluate prefix expression

 

 
Powered by phpBB® Forum Software