Path: utzoo!attcan!uunet!husc6!bu-cs!buengc!art From: art@buengc.BU.EDU (A. R. Thompson) Newsgroups: comp.lang.pascal Subject: Re: Control variables in FOR loops Message-ID: <1771@buengc.BU.EDU> Date: 22 Dec 88 19:29:09 GMT References: <1439@csuna.UUCP> <45@csd4.milw.wisc.edu> Reply-To: art@buengc.bu.edu (A. R. Thompson) Followup-To: comp.lang.pascal Organization: Boston Univ. Col. of Eng. Lines: 54 In article <45@csd4.milw.wisc.edu> markh@csd4.milw.wisc.edu (Mark William Hopkins) writes: >In article <1439@csuna.UUCP> abcscnuk@csun.UUCP (Naoto Kimura) writes: > >You are right about its illegality. In any case, a programmer has no business >cluttering up the "main program" with extraneous variables. It makes the >program more difficult to read, and write. >Some people even initialize the counters (gag). > >I think the only sensible approach to defining for loops is to make it >equivalent to the corresponding while loop, much as in C. Example: > >for I := A to B do S equals I := A; > while I <= B do begin > S; > if I < B then I := succ(I) > end > >an optimized version would be this: > > I := A; > while I < B do begin S; I := succ(I) end; > if I = B then S > >> >> I'd like to know how you feel about the behavior of the following >>program. What should the output be (if the compiler even lets this >>code through) ? >> > >>program nonsense(input,output); >> var >> i : integer; >> >> procedure barf; >> begin >> for i := 1 to 10 do >> write(i:3); >> write(' i=',i) >> end; > >According to the definition I posed above, the output would be: > > 1 2 3 4 5 6 7 8 9 10 i= 10. > >(assuming that the default integer field width is 10.) Ah, but since you've already established that it's an illegal program you can no longer predict what will happen. That's strictly up to the compiler writer (who is duty bound to report the error and refuse to generate executable code). Secondarily, while your definition is the proper way to define the effect of the for statement it allows the control variable to be defined after completion of the while statement. The control variable is required to be undefined upon completion of the for statement.