Path: utzoo!utgpu!attcan!uunet!cs.utexas.edu!tut.cis.ohio-state.edu!ukma!uflorida!haven!mimsy!chris From: chris@mimsy.UUCP (Chris Torek) Newsgroups: comp.lang.c Subject: Re: Problem with ()?():() as ending test in for-loop Message-ID: <17722@mimsy.UUCP> Date: 24 May 89 14:47:04 GMT References: <1200@liszt.kulesat.uucp> Organization: U of Maryland, Dept. of Computer Science, Coll. Pk., MD 20742 Lines: 29 In article <1200@liszt.kulesat.uucp> vanpetegem@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@mimsy.umd.edu Path: uunet!mimsy!chris