Skip to content
Loops
step 1/5

Reading — step 1 of 5

Learn

~1 min readControl Flow

Pascal has three loop forms:

for — counted, fixed range:

for i := 1 to 10 do
    WriteLn(i);

for i := 10 downto 1 do      { reverse }
    WriteLn(i);

The loop variable must be declared in var. After the loop, its value is undefined.

while — pre-check:

n := 1;
while n < 100 do
    n := n * 2;

repeat ... until — post-check (always runs at least once). Note: until <condition to STOP>, opposite sense from while:

repeat
    ReadLn(line);
until line = 'quit';

Multiple statements use begin/end:

for i := 1 to n do
begin
    sum := sum + i;
    WriteLn(i, ' ', sum);
end;

Discussion

Ask a question, share an insight, or help someone who’s stuck.

Sign in to post a comment or reply.

Loading…