Xref: utzoo comp.lang.modula2:1270 comp.lang.c:16109 Checksum: 04591 Lines: 48 Path: utzoo!sq!msb From: msb@sq.uucp (Mark Brader) Date: Wed, 8-Feb-89 18:39:45 EST Message-ID: <1989Feb8.183945.21485@sq.uucp> Newsgroups: comp.lang.modula2,comp.lang.c Subject: Re: Loop semantics: FOR C := A TO B DO ... References: <763@csd4.milw.wisc.edu> Reply-To: msb@sq.com (Mark Brader) Organization: SoftQuad Inc., Toronto > Find a suitable translation in C for > a, b, c: CHAR > ... > FOR c := a TO b DO > > that correctly accounts for c's remaining inside its subrange 0..127 or > 0..255 or whatever. I haven't seen the following answer yet. (I'm sure that net propagation delay means that I'll see three such answers tomorrow, but anyway....) As a non-speaker of Modula2, I am assuming that the original statement calls for the greater of 0 and b-a+1 iterations, i.e., including both the a and b cases if a >= b but nothing if a < b. Otherwise it'd be simple. The problem is of course that c might loop through all values that its type can hold, so there's no single test such as c < b that you can use in a for- or while-header, no matter how you fiddle the increment side. However, this doesn't mean that you have to repeat the whole body. Just provide a separate test before the first iteration: if (a <= b) { c = a; do { } while (c++ < b); } This does what was asked for -- and yes, I tested it. So you might keep it in mind the next time you need a loop whose index that might exactly reach the minimum legal maximum value for its data type. But I don't think it really meets the requirement of a *suitable* C translation... it's more of a transliteration. A suitable C translation would use the suitable datatype for a loop index whose absolute value doesn't exceed 32767: int. Since the loop index isn't going to reach 32767, the loop can now be coded in (wait "for" it) a straight"for"ward fashion: for (c = a; c <= b; ++c) { } Of course, the loop body might have to be altered if it does something that depends on the type of the loop index being char. Such things are relatively rare, usually involving & operations. Mark Brader, Toronto sed -e "s;??\\([-=(/)']\\);?\\\\?\\1;g" utzoo!sq!msb, msb@sq.com will fix them... -- Karl Heuer