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

Andy Biewer andrew at csd4.csd.uwm.edu
Fri Aug 17 12:51:09 AEST 1990


Much of the time during programming in C I run into the same question about
conditional statements such as the subject.  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;

It may be a trivial question, however, is there any?  Will `y' in the first
conditional be tested if `x' fails?  I know that it won't in the second.

Because it's a conjunction, logically it is unnecessary to test `y' because
the whole evaluation is false if `x' fails.  And, of course, with a
disjunction it would be necessary to test `y'.  But, is the compiler smart
enough to take these facts into consideration (or is it even possible to)?

Andy B.




More information about the Comp.lang.c mailing list