if ( x && y ) or if ( x ) then if ( y ) ...

Roberto Shironoshita shirono at ssd.csd.harris.com
Wed Sep 12 07:38:17 AEST 1990


In article <367 at bally.Bally.COM> siva at bally.Bally.COM (Siva Chelliah) writes:

> In article <1990Aug17.164730.25750 at zip.eecs.umich.edu> huggins at zip.eecs.umich.edu (James K. Huggins) writes:
> >In article <5781 at uwm.edu> andrew at csd4.csd.uwm.edu (Andy Biewer) writes:
> >| [ wonders about "if (x && y) stmt;" and "if (x) if (y) stmt;" ]
> >K&R 2 specify that if 'x' fails (i.e. has value 0), 'y' will not be
> >tested.
> I was told by my teachers that this is compiler dependent.  Some
> compilers will 
> evaluate both x and y first before evaluating ( x && y).

Let's get things straight.

The LANGUAGE specifies lazy evaluation:

K&R (1ed) p. 38  [emphasis mine]:
	More interesting are the logical connectives && and ||.
	Expressions connected by && or || are evaluated left to
	right, **>and evaluation stops as soon as the truth or
	falsehood of the result is known<***

If some compiler doesn't do this, then it doesn't compile C, and never did.

SIDE NOTE: Pascal either allows or requires full evaluation of every
	expression.
--

     Roberto Shironoshita      ||   Internet: shirono at ssd.csd.harris.com
      Harris Corporation       ||
   Computer Systems Division   ||   UUCP:     ...!uunet!hcx1!shirono
                               ||
DISCLAIMER: The opinions expressed here are my own; they in no way reflect the
            opinion or policies of Harris Corporation.



More information about the Comp.lang.c mailing list