pTAL Reference Manual (G06.24+, H06.09+, J06.03+)
Example 157 Nested FOR Statement
INT .multiples[0:10*10-1];
INT row;
INT column;
FOR row := 0 TO 9 DO
FOR column := 0 TO 9 DO
multiples [row * 10 + column] := column * (row + 1);
Standard
For index, standard FOR statements specify an INT or INT(32) variable. Standard FOR statements
execute as follows:
• When the looping terminates, index is greater than limit if:
The step value is 1.◦
◦ You use the TO keyword (not DOWNTO).
◦ The limit value (not a GOTO statement) terminates the looping.
• limit and step are recomputed at the start of each iteration of the statement.
The standard FOR statement in Example 158 (page 214) uses the DOWNTO clause to reverse a
string from "BAT" to "TAB".
Example 158 Standard FOR Statement
LITERAL len = 3;
LITERAL limit = len - 1;
STRING .normal_str[0:limit] := "BAT";
STRING .reversed_str[0:limit];
INT index;
FOR index := limit DOWNTO 0 DO
reversed_str[limit - index] := normal_str[index];
Optimized
For index, an optimized FOR statement specifies a temporary variable created by the statement
USE (page 232). Optimized FOR statements execute faster than standard FOR statements because
limit is computed only once, at the start of the first iteration of the statement.
Example 159 Standard and Optimized FOR Statements
INT x;
INT y;
INT PROC f;
BEGIN
x := x + 1;
RETURN 10;
END;
INT PROC p1; ! p1 has standard FOR statement
BEGIN
INT i;
x := 0;
FOR i := 1 to f() DO ... ; ! f is called 10 times
! i=11 here
RETURN x; ! p1 returns 10
END;
INT PROC q;
BEGIN
x := y + 1;
214 Statements