TAL Programmer's Guide

FOR Statement
Controlling Program Flow
096254 Tandem Computers Incorporated 12–15
Optimized FOR Loops For index, optimized FOR loops specify a register reserved by a USE statement.
Optimized FOR loops execute faster than standard FOR loops; they execute as
follows:
When the looping terminates, index is equal to limit.
limit is calculated only once, at the start of the first iteration of the loop.
You optimize a FOR loop as follows:
1. Before the FOR statement, specify a USE statement to reserve an index register.
2. In the FOR statement:
For index, use the identifier of the index register.
Omit the step value (thereby using the default value of 1).
For limit, use the TO keyword.
3. If you modify the register stack, save and restore it before the end of the looping.
(Modifying the register stack, however, is not portable to future software
platforms.)
4. After the FOR statement, specify a DROP statement to release the index register.
Do not drop the index register during the looping.
The following example contrasts a standard FOR loop with an equivalent optimized
FOR loop. Both FOR loops clear the array by assigning a blank space to each element
in the array:
LITERAL len = 100;
LITERAL limit = len-1; !Declare ARRAY to use
STRING .array[0:limit]; ! in both FOR loops
INT index; !Declare INDEX
FOR index := 0 TO limit DO !Standard FOR loop;
array[index] := " ";
USE x; !Reserve index register
FOR x := 0 TO limit DO !Optimized FOR loop
array[x] := " ";
DROP x; !Release index register
For more information on the USE and DROP statements, see the TAL Reference Manual.
Inclusion of procedure calls in the FOR loop slows down the loop because the
compiler must save and restore registers before and after each call.