FOR CYCLE

Back to:
index

Cycle FOR is the most complex kind of cycles. It gives the possibility to control automatically a special variable (cycle parameter), incrementing or decrementing its value until definite end value.

In real Pascal language the variable must have type which is called ordinal. But "COMPAS" has limited set of types, and all of them are ordinal (so you needn't take care about variable type).

Examples and operator structure

"E97" code examples

Intel code examples


Examples and operator structure

Typical examples of FOR cycle, available in "COMPAS" are the following:

  • FOR i := 1 TO 10 DO WRITELN(i);
  • FOR i := 10 DOWNTO 1 DO s := s + i;
  • FOR a[i] := 1 TO k DO BEGIN j := i + 1; a[j] := 2 * i END;
  • FOR c := 'A' TO 'Z' DO d[c] := c;
  • FOR b := FALSE TO TRUE DO BEGIN WRITELN(b); w := NOT b; WRITELN(w) END;

Possible structures of this operator can be written the following way:

FOR {cycle parameter} := {beginning value} TO {end value} DO {operator} ;

FOR {cycle parameter} := {beginning value} DOWNTO {end value} DO {operator} ;

Note, that instead of {operator} several operators, confined between reserved words BEGIN and END - so called compound statement - can be used.

The above variant with TO word is used when cycle parameter receives values in growing order, with DOWNTO - in decrescent one.

FOR scheme The construction under discussion has the following logic.

  • Get the initial and the last number for cycle parameter.
  • Compare them and skip cycle if they are incorrect to prevent endless cycle
    (FOR i := 10 TO 1 DO...)
  • Only in the case of correct values (!) store the beginning value into parameter variable.
  • Execute cycle body.
  • Compare cycle variable with end value and exit if they are equal.
  • Calculate next value for TO and previouse one for DOWNTO.
  • Repeat the cycle again.
As you can see from above, FOR construction constantly needs two values for operation - current parameter and the last value for it. To proceed FOR cycle "COMPAS" stores parameter address and end value in the stack.
Stack is a special way of memory organization. It requires a special description.

Back to:
index top E97 Intel