TAL Reference Manual
Statements
TAL Reference Manual—526371-001
12-24
Examples of FOR Statements
Inclusion of procedure calls in the FOR loop slows down the loop because the compiler
must emit code to save and restore registers before and after each CALL statement.
The following operations are not portable to future software platforms:
•
Using a CODE statement to conditionally jump out of an optimized FOR loop. The
compiler generates correct code only for the nonjump condition.
•
Jumping into an optimized FOR loop.
Examples of FOR Statements
1. This standard FOR loop uses the DOWNTO clause to reverse a string from "BAT"
to "TAB":
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];
2. This nested FOR loop treats MULTIPLES as a two-dimensional array. It fills the
first row with multiples of 1, the next row with multiples of 2, and so on:
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);
3. This example compares a standard FOR loop to its optimized equivalent. Both
FOR loops clear the array by assigning a 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