由网友(:演戏而已↘.)分享简介:请在PASCAL编程中如何将下面的FOR...DO循环重写为WHILE...DO循环下面是以下代码Program Matmuli(input,output);::FOR i:=1 TO m DOFOR j:=1 TO p DOBEGINC[i,j]:= 0.0;FOR k:=1 TO n DOC[i,j]:= C[...![第10节循环结构 for while do..while](https://p.xsw88.cn/allimgs/daicuo/20230903/4636.png)
请在PASCAL编程中如何将下面的FOR...DO循环重写为WHILE...DO循环
下面是以下代码
Program Matmuli(input,output);
:
:
FOR i:=1 TO m DO
FOR j:=1 TO p DO
BEGIN
C[i,j]:= 0.0;
FOR k:=1 TO n DO
C[i,j]:= C[i,j] + A[i,k] * B[k,j];
END;
推荐答案
![第10节循环结构 for while do..while](https://p.xsw88.cn/allimgs/daicuo/20230903/4636.png)
for
循环根据while
循环、协商ISO 7185 "Standard Pascal"、引用:
除了这些要求施加的限制外,for语句
for v := e1 to e2 do body
应相当于
begin
temp1 := e1;
temp2 := e2;
if temp1 <= temp2 then
begin
v := temp1;
body;
while v <> temp2 do
begin
v := succ(v);
body
end
end
end
[…]
其中temp1
和temp2
表示程序不包含的辅助变量,并且具有变量v
[…]所具有的类型
这段扩展的代码的重要之处在于,正如linuxfan says Reinstate Monica已经指出的那样,Pascal中的循环的限制只计算一次。此外,仅当确实至少有一次迭代时,才会为控制变量赋值。
但是,通常按照Andreas Rejbrand的建议转换for
循环就足够了,尽管在技术上不是。
相关推荐
最新文章