Problem with ()?():() as ending test in for-loop

Chris Torek chris at mimsy.UUCP
Fri May 26 07:39:12 AEST 1989


>In article <17722 at mimsy.UUCP> I claimed that
>>[logical] e1?e2:e3 expression[s] can ... be transformed ... into
>>(e1&&e2)||e3

In article <344.nlhp3 at oracle.nl> bengsig at oracle.nl (Bjorn Engsig) writes:
>No Chris, this was a bit too fast.  If e1!=0 and e2==0, (e1&&e2)||e3 evaluates
>e3 and returns it's truth value, whereas (e1?e2:e3) returns 0 and does not
>evaluate e3.

Oops, right.  I think my brain was on fire when I wrote <17722 at mimsy.UUCP>.
But e1?e2:e3 *can* be turned into (e1&&e2 || !e1&&e3) [&& has higher
precedence than ||, but you should parenthesise in real code]:

	e1  e2  e3	e1?e2:e3	e1&&e2  !e1&&e3  e1&&e2||!e1&&e3
	--  --  --	--------	------	-------  ---------------
	F   F   F	   F		  F	    F		F
	F   F   T	   T		  F	    T		T
	F   T   F	   F		  F	    F		F
	F   T   T	   T		  F	    T		T
	T   F   F	   F		  F	    F		F
	T   F   T	   F		  F	    F		F
	T   T   F	   T		  T	    F		T
	T   T   T	   T		  T	    F		T

(where F = false = 0, and T = true = nonzero-in 1-out).  The ultimate
test for boolean operations is to draw up a truth table....
-- 
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