Path: utzoo!attcan!uunet!ncrlnk!ncrcae!hubcap!gatech!uflorida!haven!adm!xadmx!art%BUENGC.BU.EDU@cunyvm.cuny.edu From: art%BUENGC.BU.EDU@cunyvm.cuny.edu (A. R. Thompson) Newsgroups: comp.lang.pascal Subject: Re: Control variables in FOR loops Message-ID: <17854@adm.BRL.MIL> Date: 16 Dec 88 21:08:05 GMT Sender: news@adm.BRL.MIL Lines: 83 In article <1439@csuna.UUCP> abcscnuk@csun.UUCP (Naoto Kimura) writes: > > I have gotten into some pretty heated discussions regarding the >control variable in a for loop. I was arguing that it was a bad idea to >use a global variable, in fact it is an error to do so. I'm arguing >from the ISO standard, while the people I was arguing against were going >by specific implementations that allowed it. Which demonstrates why you should never allow a particular implementation to define a language. Only the standard, or lacking a standard, the official "report" of the creator of the language. > > Should the use of a relatively global variable be allowed as a >control variable for a for loop ? Should the use of a passed parameter >be allowed as a control variable ? I say no to both of these. Absolutely not. The standard is quite clear on this. In fact, when the standard was being written a desire was expressed to force the control variable to be local to the for statement itself. Try defining that. > > One person that I got into an argument with said that the behavior >should be that you can change a control variable, but should not have >any effect on the loop itself. I don't think that is reasonable. >Consider the confusion over what the variable 'i' should contain when >you use it in several procedures as control variables for a for loop. How can you change the control variable without having any effect on the loop itself? If they want to fiddle with the control variable, I suggest they use the while and repeat statements. That's what they're for. > > 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) ? > The following listing shows what the Encore Pascal-2 compiler "thinks" of this program. You're right and your friends are wrong. Pascal-2 3.01 Encore UMAX 14-Dec-88 3:47 PM Page 1-1 nonsense.p 1 2 program nonsense(input,output); 3 var 4 i : integer; 5 6 procedure barf; 7 begin 8 for i := 1 to 10 do ^108 *** 108: FOR-loop control variable must be declared at this level 9 write(i:3); 10 write(' i=',i) 11 end; 12 13 procedure ack; 14 begin 15 for i := 1 to 10 do begin ^108 *** 108: FOR-loop control variable must be declared at this level 16 write('i=',i:2,' '); 17 barf; 18 writeln 19 end 20 end; 21 22 begin 23 for i := 1 to 10 do 24 ack 25 end. 26 Invocation line: -list nonsense.p *** There were 2 lines with errors detected ***