Path: utzoo!utgpu!news-server.csri.toronto.edu!rutgers!ucsd!sdd.hp.com!hplabs!hpcc01!hpcuhb!hpda!hpwala!hpavla!gary From: gary@hpavla.AVO.HP.COM (Gary Jackoway) Newsgroups: comp.lang.c Subject: Re: if ( x && y ) or if ( x ) then if ( y ) ... Message-ID: <9130016@hpavla.AVO.HP.COM> Date: 17 Aug 90 14:21:22 GMT References: <5781@uwm.edu> Organization: Hewlett-Packard Avondale Division Lines: 36 Andy asks: > 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; The language C guarantees "partial evaluation". That is, if x yields 0, y will not be evaluated at all. That means that if y involves a function call, the function will not be called. So, for example, the following code is safe in C: if (pointer!=NULL && pointer->i == 3) You cannot say this in Pascal, because if the pointer is NULL the derefence will still occur and that's not good. (Most Pascals have a PARTIAL_EVAL switch to allow for this.) So the only real difference between 1 and 2 above is that you can write an else clause on the intermediate cases: if (x) if (y) s1; else s2; else s3; Note that partial evaluation also holds for the || operator as well: if the first operand yields non-zero, the second operand will not be evaluated. Gary Jackoway