我有一个与Pascal中for循环工作方式相关的问题:
Program example;
var i:integer;
Begin
i:=7;
for i:=1 to i do write(i);
End.
这段代码输出:1234567
。
我认为编译器会创建变量i
的一个副本,然后在for
循环中使用它。
我有一个与Pascal中for循环工作方式相关的问题:
Program example;
var i:integer;
Begin
i:=7;
for i:=1 to i do write(i);
End.
这段代码输出:1234567
。
我认为编译器会创建变量i
的一个副本,然后在for
循环中使用它。
https://github.com/Leporacanthicus/lacsap/blob/master/test/Basic/iso7185pat.pas#L761
Program example;
var i:integer;
var addr:^word;
Begin
i:=10;
for i:=1 to i do addr:=@i;
addr^ := addr^+1; { I add 1 to the last loop counter }
writeln(i); { This is final variable, I don't add anything to the final variable of the loop }
writeln(addr^); { This is value of the last loop counter index }
{ Both variables give us the same result }
{ Proposal: In my opinion, your guess about the copy is correct }
End.
正如代码注释所述,我认为 Pascal 会创建这个变量的副本。
希望这能有所帮助!如果有帮助,请投票赞成。
祝好!
movw U_P$EX_I,%si
,其中U_P$EX_I
是i
的值。 - LeDev
endloop
的隐藏变量或寄存器):endloop := i; i := 1; while i < endloop do begin write(i); inc(i); end;
。就这样。没有第二个变量i
。 - Rudy Velthuis