Loops


Usage
  • begin - end keywords are optional (for single-statement blocks).
  • Semicolons are optional for the last statement in a begin - end block.
while
Syntax
while (expr) do
    statement;

while (expr) do
begin
    statement;
    statement    { Semicolon optional for last statement in block }
end;
repeat
Syntax
repeat
    statements;
until (expr);
Usage
  • begin - end keywords are not needed for multiple-statement blocks.
for
Syntax
for VAR := initial-value to final-value do
    statement;

for VAR := initial-value downto final-value do
begin
    statement;
    statement    { Semicolon optional for last statement in block }
end;
Examples
for i := 1 to 5 do
    writeln(i);

for i := 5 downto 1 do
    writeln(i);
Loop control statements
Usage
  • break;  : Break from a for, while, or repeat statement.
  • continue;  : Continue with the next iteration of a for, while, or repeat statement.