Problem with ()?():() as ending test in for-loop
Chris Torek
chris at mimsy.UUCP
Thu May 25 00:47:04 AEST 1989
In article <1200 at liszt.kulesat.uucp> vanpetegem at liszt.kulesat.uucp writes:
>I have a problem with VAX C V2.4-026 under VMS V5.0-2.
> for(n = 1; ((n % 10) ? (k > 2) : (n < 100)); n++)
>... When n becomes equal to 10 the "for-loop" is stopped already.
>So I think neither the first nor the second test is evaluated and only
>(n % 10) is taken into account (the first test (k > 2) is only important
>for n taking values from 50 on).
It sounds as though you have found a bug in the compiler. The large
`for' expression should evaluate to either 1 or 0: if (n % 10) is
nonzero, the result is 1 iff k > 2; otherwise, the result is 1 iff n <
100. You should look at the assembly produced by the compiler to see
whether the compiler simply `forgot' to generate parts of the
expression---for instance, it may have produced
n % 10 && k > 2
instead. If so, rewriting the test as
((n % 10) != 0 && k > 2) || n < 100
will probably get around the bug. (Any valid `logical' [true/false]
e1?e2:e3 expression can always be transformed this way into (e1&&e2)||e3,
provided e3 has no side effects.)
--
In-Real-Life: Chris Torek, Univ of MD Comp Sci Dept (+1 301 454 7163)
Domain: chris at mimsy.umd.edu Path: uunet!mimsy!chris
More information about the Comp.lang.c
mailing list