+ 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

 Page 1 of 1 [ 2 post ]

Relevant Pages

Powered by phpBB® Forum Software