Path: utzoo!utgpu!news-server.csri.toronto.edu!cs.utexas.edu!uunet!snorkelwacker!husc6!spdcc!ima!haddock!karl From: karl@haddock.ima.isc.com (Karl Heuer) Newsgroups: comp.lang.c Subject: Re: a style question Message-ID: <18370@haddock.ima.isc.com> Date: 2 Oct 90 03:46:37 GMT References: <7341@darkstar.ucsc.edu> <1990Sep30.050655.13212@zoo.toronto.edu> <1990Sep30.172917.2951@Neon.Stanford.EDU> <1990Oct1.174941.22195@zoo.toronto.edu> Reply-To: karl@kelp.ima.isc.com (Karl Heuer) Organization: Interactive Systems, Cambridge, MA 02138-5302 Lines: 21 In article <1990Oct1.174941.22195@zoo.toronto.edu> henry@zoo.toronto.edu (Henry Spencer) writes: >What *can* make a difference is to run the loop backwards, algorithm >permitting: > for (x = 99; x >= 0; x--) In keeping with the idea that x is ranging through the half-open interval [0,100) rather than the closed interval [0,99], I often write this as for (x = 100; --x >= 0;) instead. (Possibly more efficient, too, since the decrement and the test are not separated by a basic-block-boundary, and hence the test instruction can be omitted on vaxlike architectures.) Note that with either Henry's example or mine you will lose big if x is an unsigned integer (and if you don't use lint to catch it). I usually use for (x = 100; x != 0; --x) on unsigned countdown loops where the index doesn't matter (i.e. just "do 100 times"); if you actually need the interval [0,100), for (x = 100; x-- != 0;) does the job (though probably less efficiently). Karl W. Z. Heuer (karl@kelp.ima.isc.com or ima!kelp!karl), The Walking Lint