Help with casts

Chris Torek torek at elf.ee.lbl.gov
Tue Feb 26 07:11:59 AEST 1991


In an article whose referent has been deleted (perhaps <414 at ceco.cecom.com>?)
Garry Garrett writes:
>>For instance, in pascal the index of a for loop cannot be referenced
>>outside of that loop ...

In article <1991Feb25.143544.11950 at watdragon.waterloo.edu>
dsebbo at dahlia.uwaterloo.ca (David Ebbo) writes:
>No.  In Pascal, you can use any integer variable as a for loop index, and it
>can be referenced outside the loop.

Garry Garrett is correct.

Pascal (at least `old' Pascal; I have not kept up with the ISO variants)
makes three constraints on loop index variables:

	- they must be local;
	- they may not be altered inside the loop;
	- they may not be examined outside the loop.

Many compilers do not enforce these restrictions, but

	program foo;
	var i : integer;
	procedure nothing; begin end;
	begin for i := 1 to 10 do nothing end.

is illegal because `i' is global;

	procedure bar;
	var i : integer;
	begin for i := 1 to 10 do i := 3 end;

is illegal because `i' is altered inside the loop; and

	procedure baz;
	var i : integer;
	begin for i := 1 to 5 do nothing; writeln('i=5? i=', i) end;

is illegal because `i' is used outside the loop without first being
redefined.  Changing baz to:

	procedure baz;
	var i : integer;
	begin for i := 1 to 5 do nothing; i := 5; writeln('i=', i) end;

makes it legal.
-- 
In-Real-Life: Chris Torek, Lawrence Berkeley Lab EE div (+1 415 486 5427)
Berkeley, CA		Domain:	torek at ee.lbl.gov



More information about the Comp.lang.c mailing list