0

Please how do I rewrite the below FOR....DO loop into a WHILE...DO loop in Pascal programming

Below is the following code

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;
Browyn Louis
  • 218
  • 3
  • 14

1 Answers1

0

The for loop is well defined in terms of a while loop, confer ISO 7185 “Standard Pascal”, quote:

Apart from the restrictions imposed by these requirements, the for-statement

for v := e1 to e2 do body

shall be equivalent to

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

[…]

where temp1 and temp2 denote auxiliary variables that the program does not otherwise contain, and that possess the type possessed by the variable v […]

The important thing from this expanded piece of code is, as linuxfan says Reinstate Monica already noted, that in Pascal the limits of a for loop are only evaluated once. Furthermore, the control-variable is only assigned a value if there was indeed at least one iteration.

However, usually it’s sufficient to transform a for loop as Andreas Rejbrand already suggested, although it is technically not the same.

Kai Burghardt
  • 1,046
  • 1
  • 8
  • 23