Relay-Version: version B 2.10 5/3/83; site utzoo.UUCP Posting-Version: version B 2.10.1 6/24/83; site rna.UUCP Path: utzoo!watmath!clyde!burl!ulysses!allegra!mit-eddie!godot!harvard!seismo!cmcl2!rna!kc From: kc@rna.UUCP (Kaare Christian) Newsgroups: net.lang.mod2 Subject: Re: for i := 1 to 0 Message-ID: <364@rna.UUCP> Date: Thu, 31-Jan-85 16:18:25 EST Article-I.D.: rna.364 Posted: Thu Jan 31 16:18:25 1985 Date-Received: Sat, 2-Feb-85 12:51:00 EST Organization: Rockefeller Neurobiology, NYC Lines: 81 Joel McCormack is correct, Modula-2 has sane for loops. They are not executed if the bounds are such that they should not be executed. However I do have gripes about the rest of Joel's comments. > Incidentally, it used to be that > > VAR i: 1..10; > > made it illegal to do > > FOR i := 1 TO 0, because of the limits were supposed to be assignment >compatible with the control variable. This has been relaxed to merely >compatible. This change should be in the 3rd edition of the book. 1. The syntax for subranges should be VAR i : [ 1 .. 10 ]; 2. In his example, i is a subrange of cardinal, and 1 and zero are perfectly good cardinals. Thus the for loop would loop twice if an increment of -1 were supplied, otherwise it would not execute but it should be accepted as valid m2 code. (-1 is presumed to be an integer, but integers are assignment compatible with cards. Yes I know, the -1 isn't being assigned to a card, rather it is being added to a card. However assignment compatibility is what used to be required by the language.) 3. Joel is correct that the limits in a for loop used to be assignment compatible with the control variable; now they must be compatible. (BTW, the change is in Wirth's third edition, pg 158.) However Joel has the sense of the change backwards. Assignment compatibility is a weaker form of compatibility than plain old compatibility. Cards and ints are assignment compatible, but not compatible. Thus the language change requires stricter checking, not laxer checking. Using the decwrl Modula-2 compiler, I compiled and ran the following. It prints 1, 0, and then 1 through 10. Thus only the first two for loops executed, the third is legal but it doesn't execute. MODULE x; FROM InOut IMPORT WriteInt, WriteLn; VAR i : [1 .. 10]; BEGIN FOR i := 1 to 0 by -1 do WriteInt(i,5); WriteLn; END; FOR i := 1 to 10 do WriteInt(i,5); WriteLn; END; FOR i := 1 to 0 do WriteInt(i,5); WriteLn; END; END x. Note 1: I've used the WriteInt routine to output the value of i, a cardinal subrange. This works because WriteInt expects a value parameter, and value formal parameters only need to be assignment compatible with the actual parameter. If WriteInt used a VAR formal parameter, i would be an unacceptable actual parameter because VAR formal parameters must be compatible with the actual parameter. Note 2: The decwrl Modula-2 follows the original Modula-2 standard, not the standard in Wirth's third edition. When updated to reflect changes in the third edition, the first for loop will be illegal (as I understand it). The second for loop will continue to be ok, and I suspect that the third for loop will be legal but wirth-less :-). The difference between assignment compatible and compatible is tricky. I suppose it was invented to moderate between the strictness of a true type checking language and the requirements and convenience of day-to-day usage. I don't see this oft misunderstood distinction as one of the better aspects of Modula-2's design. Kaare Christian The Rockefeller Univ. cmcl2!rna!kc