Quick start manual
Syntactic elements
4-27
Declarations and statements
Control loops
Loops allow you to execute a sequence of statements repeatedly, using a control
condition or variable to determine when the execution stops. Delphi has three kinds
of control loop: repeat statements, while statements, and for statements.
You can use the standard Break and Continue procedures to control the flow of a
repeat, while, or for statement. Break terminates the statement in which it occurs,
while Continue begins executing the next iteration of the sequence. For more
information about these procedures, see the online Help.
Repeat statements
The syntax of a repeat statement is
repeat statement
1
; ...; statement
n
; until expression
where expression returns a Boolean value. (The last semicolon before until is
optional.) The repeat statement executes its sequence of constituent statements
continually, testing expression after each iteration. When expression returns True, the
repeat statement terminates. The sequence is always executed at least once because
expression is not evaluated until after the first iteration.
Examples of repeat statements include
repeat
K := I mod J;
I := J;
J := K;
until J = 0;
repeat
Write('Enter a value (0..9): ');
Readln(I);
until (I >= 0) and (I <= 9);
While statements
A while statement is similar to a repeat statement, except that the control condition is
evaluated before the first execution of the statement sequence. Hence, if the condition
is false, the statement sequence is never executed.
The syntax of a while statement is
while expression do statement
where expression returns a Boolean value and statement can be a compound statement.
The while statement executes its constituent statement repeatedly, testing expression
before each iteration. As long as expression returns True, execution continues.