TAL Programmer's Guide

WHILE Statement
Controlling Program Flow
12–8 096254 Tandem Computers Incorporated
WHILE Statement The WHILE statement is a repeating loop that executes a statement while a specified
condition is true.
To specify a WHILE statement, include:
WHILE condition
Specifies a condition that, if true, causes the loop to execute. If the condition is
false, the loop does not execute. The condition can be either:
A conditional expression
An INT arithmetic expression. If the result of the arithmetic expression is not
0, the condition is true. If the result is 0, the condition is false.
DO statement
Specifies a statement to execute while the condition is true. The statement can be
any TAL statement.
For example, this WHILE loop continues while ITEM is less than LEN:
LITERAL len = 100;
INT .array[0:len - 1];
INT item := 0;
WHILE item < len DO !WHILE statement
BEGIN
array[item] := 0;
item := item + 1;
END;
!ITEM equals LEN at this point
The WHILE statement tests the condition before each iteration of the loop. If the
condition is false before the first iteration, the loop never executes. If the condition is
always true, the loop executes indefinitely unless a statement in the loop causes an
exit. In the following example, a GOTO statement branches to a label outside the
WHILE statement when the IF condition is true:
WHILE -1 !true! DO
BEGIN
!Lots of code
IF <condition> THEN GOTO exit_loop;
!More code
END;
exit_loop: !Label
!<statement>
!More code