Path: utzoo!utgpu!news-server.csri.toronto.edu!mailrus!ncar!ico!haddock!karl From: karl@haddock.ima.isc.com (Karl Heuer) Newsgroups: comp.lang.c Subject: Re: if ( x && y ) or if ( x ) then if ( y ) ... Message-ID: <17444@haddock.ima.isc.com> Date: 17 Aug 90 03:52:13 GMT References: <5781@uwm.edu> Reply-To: karl@kelp.ima.isc.com (Karl Heuer) Distribution: usa Organization: Interactive Systems, Cambridge, MA 02138-5302 Lines: 20 In article <5781@uwm.edu> andrew@csd4.csd.uwm.edu (Andy Biewer) writes: >I have been wondering for quite some time now about what, if any, differences >there are between the two conditional statements: > 1) if ( x && y ) statement; > 2) if ( x ) if ( y ) statement; If each represents an entire statement, then the two are semantically identical (the language *requires* that `y' not be tested if `x' fails), and the main advantage of (1) is conciseness. Things change if you add an `else' clause: `if (x && y) s1; else s2;' is equivalent to `if (x) { if (y) s1; else s2; } else s2;' and the presence of the `&&' operator saves you from having to duplicate code%. Similarly, `while (x && y) s;' is much cleaner than the equivalent code written without `&&'. So, `&&' is more than just syntactic sugar for `if...if'. Karl W. Z. Heuer (karl@kelp.ima.isc.com or ima!kelp!karl), The Walking Lint ________ % Yes, one could also emulate `&&' without duplicating the code. The point is, `&&' is better than `if...if' in this case.