Path: utzoo!utgpu!news-server.csri.toronto.edu!mailrus!uflorida!travis!shirono From: shirono@ssd.csd.harris.com (Roberto Shironoshita) Newsgroups: comp.lang.c Subject: Re: if ( x && y ) or if ( x ) then if ( y ) ... Message-ID: Date: 11 Sep 90 21:38:17 GMT References: <5781@uwm.edu> <1990Aug17.164730.25750@zip.eecs.umich.edu> <367@bally.Bally.COM> Sender: news@travis.csd.harris.com Reply-To: shirono@ssd.csd.harris.com Distribution: usa Organization: Harris Computer Systems Lines: 33 In-reply-to: siva@bally.Bally.COM's message of 11 Sep 90 17:44:20 GMT In article <367@bally.Bally.COM> siva@bally.Bally.COM (Siva Chelliah) writes: > In article <1990Aug17.164730.25750@zip.eecs.umich.edu> huggins@zip.eecs.umich.edu (James K. Huggins) writes: > >In article <5781@uwm.edu> andrew@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@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.