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

Mark A Terribile mat at mole-end.UUCP
Sat Aug 18 10:52:39 AEST 1990


> ... I run into the same question about conditional statements such as
> the subject.  ... what, if any, differences there are between the two
> conditional statements:
 
>   1)  if ( x && y )
>           statement;
 
>   2)  if ( x )
>           if ( y )
>                statement;
 

The real differences become apparent if you try to hang else's on them.
For the first one, you can write

	if( x && y )
		then_part;
	else
		else_part;

For the second, you must write

	if( x )
	{
		if( y )
			then_part;
		else
			else_part;
	}
	else
		else_part;

This might seem elementary and trite, but I have seen people lose track
of the structure of code that exceeds about twelve lines and fail to turn
code that looks like the second example into the first example.  And yes,
you should.  How can code that is twice as large as it should be be
anything but a hindrance to reliability, maintainability, readability,
etc.?  The duplication of a case is (perhaps) the worst part of all.

> 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'.  ...

Ah, now the truth comes out!

One of the great things about C (as opposed to FORTRAN or PASCAL or PL/I)
is that the logical operators `short-circuit'.  &&  and  ||  evaluate left-to-
right AND ONLY AS FAR AS NECESSARY.  In K&R-I, this is explained on page 19
and again on page 38.  In K&R-II, in just about the same words, it is
explained on pages 21 and 41.  What, you used some other textbook?  No, don't
tell us about it, please.

> 					     ...  But, is the compiler smart
> enough to take these facts into consideration (or is it even possible to)?

Yes, the compiler is smart enough; it is required to by the language, and
it really isn't so difficult to program.  Once you've got the expression
tree, it's fairly simple and pretty much machine independent (RISC
optimizations being handled later).

Incidentally, the Bourne and Korn shell && and || have the same property.

For those who have been bored this far and who don't know it, the designers
of Ada couldn't decide whether to have the logical AND and OR short-circuit
(allowing shorter code in programs that can use the feature) or not (allowing
the compiler to decide which operand is cheaper to compute and computing that
operand first).  They ended up with both kinds,  AND and AND THEN, OR and
OR ELSE.  Just like a spoiled rich kid who can't decide which to buy, DOD
bought both.  I read some where that short-circuits were outnumbering
reorderables by three to one in actual code, but that was a while ago.  It
may have changed.
-- 

 (This man's opinions are his own.)
 From mole-end				Mark Terribile



More information about the Comp.lang.c mailing list