Repeat loop
The Repeat loop works similarly to the While loop with the difference that the Repeat loop itself first executes the given command and then tests the condition (expression) given after the word until. The Repeat loop is used if we do not have a specified number of repeats. With the Repeat loop, the command will perform at least once.
If we want to use more than one command, we do not need to use the compound command in the Repeat loop (we do not need to use the reserved words begin and end) because the commands are closed with the repeat and until keywords.
The Repeat loop is implemented as follows:
- First, a sequence of commands between the repeat and until keywords is executed.
- Then the logical expression (after the until word) is evaluated.
- If the condition (logical expression) is met, its value is true, the loop is terminated. If the value is false, the process is repeated. The loop ends when the condition is fulfilled for the first time.
We must not forget that something has to be changed in the body of the loop so that from a certain moment the condition ceased to apply because there would be an endless loop case and the program would never finish. In this case, you need to force the stop it with either ALT + F4, or stop in a quick menu.
Syntax of the Repeat loop:
repeat
command1;
command2;
...
until condition;
The Repeat loop implementation is as follows:
- First, a sequence of commands between the repeat and until keywords is executed, then the condition (the logical expression) written after the until word is evaluated. If the condition is met (valid), the loop is terminated. If the condition is not met, the loop is repeated. The loop will end when the condition is fulfilled for the first time.
- The difference between the While loop and the Repeat loop is that the Repeat loop is performed at least once, because the condition is evaluated only after the sequence of commands has been performed.
In the following example (Figure 70) we show how thanks to the Repeat loop we can move the Left key until its attribute Left will not be greater than 50 (Figure 71).