Sign in Join
Wiki copytemporary

Repeat

Deutsch · English · Français · Suomi · Русский

This reserved words repeat in conjunction with until are used to create tail-controlled loops.

syntax

A tail-controlled loops start with repeat, followed by a possibly empty list of statements, and concluded by until and a Boolean expression.

The following (infinite) loops demonstrate the syntax:

pascal
// empty loop body is legal:
repeat
until false;

Note, repeat and until already form a frame in their own right. You do not need to surround your statements by an extra begin … end-frame. In fact, you can put as many sequences (also called compound statements) between repeat and until as you want:

pascal
repeat
begin
    write('x');
end;
begin
    write('o');
end;
until false;

Note, it is not necessary, but allowed to put a [[;|semicolon]] prior until:

pascal
repeat
    write('zZ')
until false;

semantics

Since the loop “head” appears at the tail, the loop body is executed at least once and the loop condition evaluated at the end of every iteration. If the condition evaluates to false, another iteration occurs.

Therefore, repeat … until loops are particularly useful to ensure a certain sequence of statements is run at least once.

pascal
repeat
    write('Enter a positive number: ');
    readLn(i);
    
    // readLn loads a default value if the source is EOF.
    // For integer values the default is zero.
    // Since our loop condition requires _positive_ values,
    // this loop would be stuck _indefinitely_ if EOF(input).
    // Ergo, we check for that:
    if eof(input) then
    begin
        writeLn;
        writeLn(stdErr, 'error: input has reached EOF');
        halt(1);
    end;
until i > 0;

The user will be prompted again and again, but at least once, until he finally enters a positive number.

Another standard usage example is the reverse Horner scheme as demonstrated in Base converting.

see also


From the Free Pascal wiki, page "Repeat", as it stood on 2023-01-25 after a change by Kai Burghardt, out of the wiki's dump of 2024-05-26. Copyright stays with the author who wrote it, according to the old wiki's Licensing page. If you have concerns about this material, message the admin.

Revision 1, last changed by WikiArchive .

Talk about this page

Nobody has opened a bench for this page yet. Sign in to open one.