Contestcen@aol.com wrote:
Using array elements as control variables is very useful when you want the ability to stop a deeply nested loop, checkpoint the program, and then
resume
the execution from the same point later. I would not like to see this capability removed.
As first glance, since only constant indices are allowed, it would seem to be equivalent to using a1, a2, ... instead of a[1], a[2], or am I missing something?
Can you give an example for the situation you describe?
The essential difference between using a1, a2, ... and a[1], a[2], ... is that I can write loops that do something useful with the a[i] elements. For example, suppose that I have the statements
if KeyPressed then begin CheckPoint (depth); exit; end;
located at various places in a deeply nested loop. Here depth indicates how deep in the nest the checkpoint was taken. Now I can put the following inside the CheckPoint procedure.
for n := 1 to depth do writeln (checkfile, a[n]);
This will write out all of the known control variable values. The restart procedure can do this
depth := 0; while not eof(checkfile) do begin depth := depth + 1; readln (checkfile, start[depth]); end; for n := depth+1 to maxdepth do start[n] := 1; {or whatever the lower value is}
Then in each loop in the nest the control variable can be tested like this
for a[10] := 1 to max[10] do begin eq := true; for n := 1 to 9 do if a[n] <> start[n] then eq := false; if eq and (a[10] < start[10]) then continue; ... end;
If you did not have the ability to use array elements as control variables, then the previous code would have to be the verbose
for a10 := 1 to max10 do begin if (a1 = start[1]) and (a2 = start[2]) and (a3 = start[3]) and (a4 = start[4]) and (a5 = start[5]) and (a6 = start[6]) and (a7 = start[7]) and (a8 = start[8]) and (a9 = start[9]) and (a10 < start[10]) then continue; end;
Yes, it can be done, but I much prefer the loop version to the last version.
Can't you use a macro processor to generate the verbose code? ;-)
Anyway, I see that this is an example, at least in theory. However, I'd probably do it differently in practice. I think this is more efficient, since you don't need any comparisons, and in particular no extra code in the innermost loop which is the most time-critical. If you need the original start values afterwards, copy the array before the first loop, of course.
for a1 := start[1] to max[1] do begin for a2 := start[2] to max[2] do begin for a3 := start[3] to max[3] do begin ... end; start[3] := 1 end; start[2] := 1 end;
Frank