Unit 5.3 -- Do Loop Example 3:  Nested Do Loops

PROG

See the following program's comment lines.

PED

learn how to nest DO Loops

Concept

DO loops are nested analagously to IF statements.
Just translate each DO loop in place,
making sure labels are unique.

11.html
                  Unit 5.3 --Do Loop Example 3

Just  as  we  learned  that we can nest  IF  statements,  by
translating  the  inner IF statement and  then  putting  the
corresponding ASSEMBLER code in the appropriate  place,  the
same  thing is true with nested FOR and DO loops.  The inner
loop just gets translated and put in the corresponding place
of the translated outer loop.

In our example, we translate the following code:

FOR I := 1 TO M DO
  FOR J := 1 TO N DO
    SUM:=SUM+I+J
  END
END

First   this   is  converted  to  the  corresponding   WHILE
statements:

I:=1
WHILE (I<=M) DO BEGIN
  J:=1
  WHILE (J<=N) DO BEGIN
    SUM:=SUM+(I+J)
    J:=J+1
  END
  I:=I+1;
END;

I:=1  is  done at lines 12 through 13.  We zero out  SUM  at
line 15.

The  comparison of I with M is done in lines 17 through  19.
J:=1 is done at lines 20 to 21.

The  outer  loop continues at LOOP2E.  I:=I+1 is initialized
at  lines  35 to 37 and the return to the beginning  of  the
outer loop is at line 38.

The  inner loop runs from lines 20 to 34.  J:=1 is  done  at
lines 20 to 21.

The  comparison, J<=N, is done at lines 23 through 25.

SUM:=SUM+I+J is done at lines 26 through 29.

J:=J+1 is done at lines 30 through 33 and the return to  the
beginning of the inner loop is done at line 33.