🚀 go-pugleaf

RetroBBS NetNews Server

Inspired by RockSolid Light RIP Retro Guy

Article View: comp.lang.modula2
Article #4600

Re: "for" loops (was Re: C++ vs. Modula2)

#4600
From: Chris Torek
Date: Sun, 29 Jan 1989 18:23
77 lines
2595 bytes
In article <6419@polya.Stanford.EDU> crew@Polya.Stanford.EDU (Roger Crew)
writes:
>Here's a quiz:
>How would you write the following in C?
>
>	VAR a, b, c : Char;
>	...
>	FOR c = a TO b DO ... END;
>
>You can even assume that the body of the loop affects neither a nor b.

(I do not know what this does if the range of c does not include a and
b---e.g., FOR c = 9999 to 10001 DO ... END;---so I am sort of ignoring
that case.  Probably it is a compile-time error, but what about a, b
integer, c character?)

Typically `a' and `b' are not arbitrary, and it is possible to
determine beforehand that they do not cover the entire range of `c'.
The worst-case expansion is

	if (a <= b) for (c = a;; c++) {
		...
		if (c == b) break;
	}

(This is what a compiler typically generates for a Modula loop with
indeterminate bounds---actually, it is more like

	if (a > b) goto out;
	c = a;
   top: ...;
	if (c==b) goto out;
	c++;
	goto top;
   out: ;

but only code generators and FORTRAN programmers *like* all those jumps.
Incidentally, note that c itself is never assigned if a>b.  This is the
real reason that the value of a loop iteration counter is undefined in
Pascal and Modula II after the loop, despite rationalisations.  Go ahead,
ask Wirth.  Betcha betcha :-) )

If the range of c is an improper superset of a, this can be written

	if ((c = a) <= b) for (;; c++) {
		...
		if (c == b) break;
	}

and if overflow can be ignored it can be shortened to

	if ((c = a) <= b) do {
		...
	} while (c++ != b);

Finally (or more properly firstly), if b can be determined to be
less than the maximum value of c, the whole thing can be written

	for (c = a; c <= b; c++) {
		...
	}

Incidentally, the `char' type in C is often signed:

>Hint:  the answer is *not*
>
>	char a, b, c;
>	...
>	for (c = a; c <= b; ++c) { ... }
>
>(just try a = 0 and b = 255 in the case of 8 bit chars).

If `char' is signed, and a=0, b=255, the loop runs not at all,
since b is actually -1.
-- 
In-Real-Life: Chris Torek, Univ of MD Comp Sci Dept (+1 301 454 7163)
Domain:	chris@mimsy.umd.edu	Path:	uunet!mimsy!chris
Technical Details
Message-ID: <15694@mimsy.UUCP>
Path: arc1-ca.pugleaf.net!utzoo!utgpu!jarvis.csri.toronto.edu!mailrus!ncar!tank!mimsy!chris
References: <19579@agate.BERKELEY.EDU> <8515@lanl.gov> <6419@polya.Stanford.EDU>

Show Full Headers
Complete Article Headers
Path: arc1-ca.pugleaf.net!utzoo!utgpu!jarvis.csri.toronto.edu!mailrus!ncar!tank!mimsy!chris
From: chris@mimsy.UUCP (Chris Torek)
Newsgroups: comp.lang.modula2
Subject: Re: "for" loops (was Re: C++ vs. Modula2)
Message-ID: <15694@mimsy.UUCP>
Date: 29 Jan 89 18:23:08 GMT
References: <19579@agate.BERKELEY.EDU> <8515@lanl.gov> <6419@polya.Stanford.EDU>
Organization: U of Maryland, Dept. of Computer Science, Coll. Pk., MD 20742
Lines: 77