Last Updated:

Repeat Delphi loop

As in the case of the while statement, the repeat Delphi statement is used when it is necessary to perform repeated calculations (to organize a loop), but the number of repetitions during the creation of the program is not known in advance, and therefore this number can be determined only at the time of program execution, that is, the number depends on the progress of calculations.

General view of the repeat Delphi cycle:

A general view of the repeat Delphi instruction is presented below:


where the condition is a Boolean expression that defines the conditions for loop completion.

The sequence of the repeat Delphi loop is:

The sequence of execution of the repeat statement in the Delphi language is as follows:

  1. At the first stage, the instructions of the loop body located between the service words repeat and until are performed.
  2. Next, you determine which value the condition expression will accept. If this condition turns out to be false (that is, the condition has been set to False), then the instructions of the loop body are repeatedly executed.
  3. If the condition is true (that is, it is equal to True), then the repeat loop completes its execution.

As a result, we get that the loop statements that are between the reserved words repeat and until are repeated while the condition is false (that is, until the condition takes the value False).

Let's imagine an algorithm (figure below) that implements the repeat statement:

Note. Loop statements between the service words repeat and until must be executed at least once. For the loop to complete, you must ensure that the statements in the repeat loop that sit between the reserved words repeat and until eventually change the values of the variables that make up the condition expression.

An example of a program with a repeat Delphi loop:

Let's create a program that uses the repeat statement, which would check the number (entered by the user from the keyboard) whether this number is simple. From mathematics it is known that a number is called prime in the case when it is divided only by itself and by one, for example, the number 42 is ordinary, since it is divided by 42, by 1, by 2, by 3, by 6, by 7, by 21, and the number 11 is just simple (divided only by 13 and by 1.

In a program, to check the number n, whether it is prime, you can divide the number n by 2, by 3, etc. to n and then check the remainder that appears after each division. When the next division has ended with the appearance of a zero residue, it means that the number by which the number n is divided by the target without a remainder has been determined.

By comparing two numbers—the number n and the number by which the number n was divided without a remainder—you can determine whether the number n entered by the user is prime. The figure below shows a form of the application called "Prime Number":