Path: utzoo!attcan!uunet!ncrlnk!ncrcae!hubcap!gatech!uflorida!haven!adm!xadmx!spector%BRILLIG.UMD.EDU@cunyvm.cuny.edu From: spector%BRILLIG.UMD.EDU@cunyvm.cuny.edu (Lee Spector) Newsgroups: comp.lang.pascal Subject: Re: Control variables in FOR loops Message-ID: <17855@adm.BRL.MIL> Date: 16 Dec 88 21:31:21 GMT Sender: news@adm.BRL.MIL Lines: 56 In article <1440@csuna.UUCP> abcscnuk@csuna.UUCP (Naoto Kimura) writes: > >program nonsense(input,output); > var > i : integer; > > procedure barf; > begin > for i := 1 to 10 do > write(i:3); > write(' i=',i) (****) > end; > > procedure ack; > begin > for i := 1 to 10 do begin > write('i=',i:2,' '); > barf; > writeln('i=',i:2) > end > end; > >begin > for i := 1 to 10 do > ack >end. (The question is: what SHOULD this do and what DOES it do in implementation X.) I ran this on Berkeley pascal on a big Vax here at U. MD. The compiler did not complain. I don't like what resulted from the last line in barf. (the one I marked (****) What it did was to always print 10. This is not consistent with any rules for access to i that I can formulate. I thought I understood how loop counters were supposed to work, but now I'm quite confused and I'd appreciate any clarification anyone can offer. (If all references to i are to the global - even asssuming that the values that a loop counter takes on are precomputed and that the loop variable is set to the proper value at the top of the loop - then the last line in ack should also always print 10. If entering a loop pushes a new activation record then the line in question (****) refers to an uninitialized variable; this is not the case as the result is the same even if you add a statement to initialize the global.) (the output was the following repeated 10 times:) i= 1 1 2 3 4 5 6 7 8 9 10 i= 10i= 1 i= 2 1 2 3 4 5 6 7 8 9 10 i= 10i= 2 i= 3 1 2 3 4 5 6 7 8 9 10 i= 10i= 3 i= 4 1 2 3 4 5 6 7 8 9 10 i= 10i= 4 i= 5 1 2 3 4 5 6 7 8 9 10 i= 10i= 5 i= 6 1 2 3 4 5 6 7 8 9 10 i= 10i= 6 i= 7 1 2 3 4 5 6 7 8 9 10 i= 10i= 7 i= 8 1 2 3 4 5 6 7 8 9 10 i= 10i= 8 i= 9 1 2 3 4 5 6 7 8 9 10 i= 10i= 9 i=10 1 2 3 4 5 6 7 8 9 10 i= 10i=10