Infinite Loops
An infinite loop is a loop that executes a statement or a block of statements repeatedly, without a guarding condition to determine its end (such as the while
/do
and repeat
/until
loops or a pre-defined set of items to loop over, like the for
loop).
An infinite loop will run indefinitely, until it is explicitly broken out of using either a break
, exit
or raise
statement.
The syntax for an infinite loop is simply the loop
keyword, followed by the statement to be repeated:
loop DoSomething();
Infinite Loops and begin
/end
blocks.
On its own, the infinite loop only takes a single statement to be executed for each iteration.
Given the need to eventually break out of the loop with a break
or exit
statement, the infinite loop is almost always used in combination with a begin
/end
block statement to allow the execution of multiple statements for each iteration:
loop begin
DoSomething();
DoSomethingElse();
if DoneSomethingThird then
break;
end;
Prematurely Exiting the Loop or a Loop Iteration
Like all loops, infinite loops can be exited prematurely using the break
and exit
statements, and a single loop iteration can be cut short by using the continue
statement, which jumps to the next loop iteration.
See Also
- Loop Statements
- Flow Control Statements
begin
/end
Block Statementsfor
andwhile
/do
loopsrepeat
/until
loops