pTAL Reference Manual (H06.03+)

Statements
HP pTAL Reference Manual523746-005
12-22
Optimized
limit and step are recomputed at the start of each iteration of the statement.
The standard FOR statement in Example 12-19 on page 12-22 uses the DOWNTO
clause to reverse a string from "BAT" to "TAB".
Optimized
For index, an optimized FOR statement specifies a temporary variable created by the
statement USE on page 12-45. 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 12-19. 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];
Example 12-20. Standard and Optimized FOR Statements (page 1 of 2)
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;
RETURN 10;
END;