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

Karl Heuer karl at haddock.ima.isc.com
Fri Aug 17 13:52:13 AEST 1990


In article <5781 at uwm.edu> andrew at 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 at 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.



More information about the Comp.lang.c mailing list