TAL Programmer's Guide

FOR Statement
Controlling Program Flow
096254 Tandem Computers Incorporated 12–13
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];
Nesting FOR Loops You can nest FOR loops to any level. The following 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);
Standard FOR Loops For index, standard FOR loops specify an INT variable. Standard FOR loops execute as
follows:
When the looping terminates, index is one greater than limit if:
The step value is 1.
The TO keyword (not DOWNTO) is used.
The limit value (not a GOTO statement) terminates the looping.
limit and step are recomputed at the start of every iteration of the loop.