dimare@ucla-cs.UUCP (01/25/85)
I was reading Wirth's book this morning and I noticed that he defines the for loop using a repeat until. I couldn't find any example where the body of the for loop would be executed 0 times. Are we back to dear Fortran, who always goes through DO loops at least once? Adolfo ///
joel@decwrl.UUCP (Joel McCormack) (01/29/85)
Wirth is not particularly clear, but no, FOR i := 1 TO 0 does not execute the loop body. He did not DEFINE the FOR loop in terms of REPEAT, but showed how (what would be) a very common case of REPEAT is more appropriately expressed with a FOR loop. The examples for minimum and sort would not work for very small arrays if the FOR loop body always executed. The report in the back of the book is somewhat clearer, when it says that "A+nC is the last term not exceeding B". This implies to me that if A > B, the loop body is not executed. 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. -- - Joel McCormack {ihnp4|decvax|ucbvax|allegra|sequent|utcsrgv}!decwrl!joel
kc@rna.UUCP (Kaare Christian) (02/01/85)
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