Path: utzoo!utgpu!news-server.csri.toronto.edu!cs.utexas.edu!sun-barr!apple!agate!ucbvax!ucsd!nosc!dog.ee.lbl.gov!elf.ee.lbl.gov!torek From: torek@elf.ee.lbl.gov (Chris Torek) Newsgroups: comp.lang.c Subject: Re: Careful "for" Loops Message-ID: <11117@dog.ee.lbl.gov> Date: 19 Mar 91 19:45:19 GMT References: Reply-To: torek@elf.ee.lbl.gov (Chris Torek) Distribution: comp Organization: Lawrence Berkeley Laboratory, Berkeley Lines: 70 X-Local-Date: Tue, 19 Mar 91 11:45:19 PST In article mcdaniel@adi.com (Tim McDaniel) writes: >Case a) > semi-open intervals, like [low,high). low is in the range, but > high is not. If high==0, the range extends from low through all > higher memory. The problem is that high==0 is likely to prove a > special case in iteration. >Case b) > closed intervals, like [low,high]. The range is inclusive: high is > in the range. The problem is that upper bounds look ugly, like > 0x01ffffff. >... Zero-length ranges are a possibility. Languages that do this sort of thing usually take closed intervals, although there are ways to handle both. For instance, the loop for i := 1 to 300 do foo (in Pascal) is usually generated as if (1 <= 300) { for (i = 1;; i++) { foo; if (i == 300) break; } } (equivalent C code). (Yes, it is OK to leave `i' unset; Pascal index variables may not be examined after the loop ends, until they are set to some other values.) If there is a step, the loop must compute the terminator value (or the number of iterations): for i := 1 to 4 by 2 should compare for 3 (for ending) or compute a count of ((4-1)+1)/2 iterations. In some cases this can be done at compile time. To do the same for half-open intervals, simply subtract one from the end value (using unsigned arithemtic, if necessary, to avoid underflow) and do the same. The loop for i in [m..n) do foo; can be `compiled' to if (m < n) { stop = n - 1; for (i = 0;; i++) { foo; if (i == stop) break; } } Iteration count computations are identical except that instead of ((end - start) + 1) / incr you simply use (end - start) / incr -- In-Real-Life: Chris Torek, Lawrence Berkeley Lab CSE/EE (+1 415 486 5427) Berkeley, CA Domain: torek@ee.lbl.gov