[comp.lang.pascal] Loops

gast@lanai.cs.ucla.edu (David Gast) (04/08/88)

In article <294@tmsoft.UUCP> mason@tmsoft.UUCP (Dave Mason) writes:
>In a language I designed for my students to implement in my compiler
>course loops look like:
>	do
>	   ...
>	   while expr
>	   ...
>	od
>where the while can go anywhere & there can be any number of them (or
>until's which are similar but with opposite logic).  This leads to
>Pascal style while & repeat loops:
>The do keyword can be followed by 1 or more 'for' clauses,

The semantics are similar to those provided by Algol 68.

>Notes:
>4) all the safety of Pascal subscript calculation

While it is commonly believed that it is impossible to change the
index variable in a for loop, it is possible to do so.  Consider the
following program.

program illegal (output);

{This program is not legal pascal, but the compiler does not detect the
 error.  If the scope of the index variable were local to the for loop
 (ala Algol 68), instead of global, then this error would be detected
 at compiler time. } 

	var
		i : integer;

        procedure illegal_assignment (k: integer);
			begin
				i := k + 10;  {i is the index variable in the for loop}
				writeln (i)
			end;

	begin
        for i := 1 to 10 do
			begin
				illegal_assignment (i);
				writeln (i)
			end
	end.
	
Although I have not read the latest Pascal standard, the Pascal I used
has many insecurities.

David Gast
gast@cs.ucla.edu
{ucbvax,rutgers}!ucla-cs!gast

Ralf.Brown@B.GP.CS.CMU.EDU (04/08/88)

In article <11047@shemp.CS.UCLA.EDU>, gast@lanai.cs.ucla.edu (David Gast) writes:
}While it is commonly believed that it is impossible to change the
}index variable in a for loop, it is possible to do so.  Consider the
}following program.
}
[code deleted for brevity]
}
}David Gast
}gast@cs.ucla.edu
}{ucbvax,rutgers}!ucla-cs!gast

I don't know about Turbo Pascal 4.0, but versions 1 through 3 use a "hidden"
variable for the loop counter, so you can change the visible loop counter
to your heart's content and the loop will still execute exactly the same
number of times.

--
{harvard,ucbvax}!b.gp.cs.cmu.edu!ralf -=-=- DISCLAIMER? I claimed something?
ARPA: RALF@CS.CMU.EDU  FIDO: Ralf Brown 1:129/31  BIT: RALF%CS.CMU.EDU@CMUCCVMA 
TalkNet: (school) | "Tolerance means excusing the mistakes others make.
(412)268-3053     |  Tact means not noticing them." --Arthur Schnitzler

bobdi@omepd (Bob Dietrich) (04/12/88)

In article <11047@shemp.CS.UCLA.EDU> gast@lanai.UUCP (David Gast) writes:
>While it is commonly believed that it is impossible to change the
>index variable in a for loop, it is possible to do so.  Consider the
>following program.

Sorry, but the existing ANSI/IEEE and ISO Pascal standards do not allow such
modification. There is a concept called "threatening" explained under the
for-statement (section 6.8.3.9). Basically, if a variable has been
"threatened", it cannot be used as a for control variable. A variable is
threatened when it appears as the target of an assignment, as a variable
parameter, as the non-file parameter of read or readln, or if a statement
enclosing the for-statement has the same control variable (a for-statement
nested within a for-statement). These restrictions apply to routines nested
within the current routine as well. This check can be done at translation
(compile) time, and requires only a Boolean flag for variables that can be a
for control.

>
>program illegal (output);
>
>{This program is not legal pascal, but the compiler does not detect the
> error.  If the scope of the index variable were local to the for loop
> (ala Algol 68), instead of global, then this error would be detected
> at compiler time. } 
>
>	var
>		i : integer;
>
>        procedure illegal_assignment (k: integer);
>			begin
>				i := k + 10;  {i is the index variable in the for loop}
                                ^-- a threat to i (all it takes is one!)

>				writeln (i)
>			end;
>
>	begin
>        for i := 1 to 10 do
             ^-- At this point threats to i are checked, and the violation
	     detected.

>			begin
>				illegal_assignment (i);
>				writeln (i)
>			end
>	end.
>	
>Although I have not read the latest Pascal standard, the Pascal I used
>has many insecurities.

This may well be, and is unfortunate. However, please try to separate the 
language from your particular implementation. Like any other language or
tool, there's mediocre implemententations and excellent implementations, and
a lot in-between. Speaking as both a compiler writer and a user, I suggest
you beat on your vendor to get rid of the insecurities that are giving you
problems, or take your business elsewhere if they don't respond.

>
>David Gast
>gast@cs.ucla.edu
>{ucbvax,rutgers}!ucla-cs!gast


				Bob Dietrich
				Intel Corporation, Hillsboro, Oregon
				(503) 696-4400 or 2092(messages x4188,2111)
		usenet:		tektronix!ogcvax!omepd!bobdi
		  or		tektronix!psu-cs!omepd!bobdi
		  or		ihnp4!verdix!omepd!bobdi

jipping@frodo.cs.hope.EDU (Mike Jipping) (04/13/88)

In responding about for loops and scope of control variables, 
Ralf.Brown@b.gp.cs.cmu.edu writes:

> I don't know about Turbo Pascal 4.0, but versions 1 through 3 use a "hidden"
> variable for the loop counter, so you can change the visible loop counter
> to your heart's content and the loop will still execute exactly the same
> number of times.

Here's a "what-do-you-folks-think" question:  Borland obviously implemented
for loops this way to get around the changing-the-control-variable problem.
I think it's dumb -- at best, a crummy way to implement the loop, and a nice
way to trip up users.  It uses implicit declaration -- something found in 
FORTRAN (gasp) and some BASICs (argh).  Ada uses the same scheme for the FOR
loop.  

Fortunately, Borland went back to the ol' real-live-declared control
variable approach in Turbo 4.0.
                                            -- Mike
      Mike Jipping
      Hope College
      Department of Computer Science
      jipping@cs.hope.edu

rober@weitek.UUCP (Don Rober) (04/13/88)

In article <11047@shemp.CS.UCLA.EDU> gast@lanai.UUCP (David Gast) writes:
>
>While it is commonly believed that it is impossible to change the
>index variable in a for loop, it is possible to do so.  Consider the
>following program.
>
 [illegal program with threatening for loop reference deleted]

While it is a pain, it it not really that difficult to enforce this area of the
Pascal standard. When an assignment to a variable is made, the symbol table is 
marked noting a potentially threatening reference. When the for-loop is seen, 
the variable is checked to see if a threatening reference happened; if so,
an error.  (Appropriate care must be taken for threatening references that 
*follow* the for-loop.)

The Unisys A-series compiler does this checking.

It was interesting to see the number of otherwise "portable" programs that 
would fail with a standard-enforcing compiler.-- 
----------------------------------------------------------------------------
Don Rober				UUCP: {pyramid, cae780}!weitek!rober
Weitek Corporation	1060 East Arques		Sunnyvale, CA 94086