6.4.1 How to write it
Repeat
«instrucciones»
Until «condición»6.4.2 How it works
It mirrors [While](#control.mientras): the question goes at the end and its meaning is the opposite. While goes on with True; Repeat stops with True.
ProgramRolling until a 6 comes up
Algorithm Dado
Define tirada As Integer;
Define intentos As Integer <- 0;
Repeat
tirada <- random(6) + 1;
intentos <- intentos + 1;
Until tirada = 6
Write "Salió el 6 en el intento ", intentos;
EndAlgorithmOutput
Salió el 6 en el intento 3
The program finishes normally.
The result is always the same because the engine's randomness is deterministic with this book's seed.
Repeat the body comes before the question. Even if the condition already holds, the body runs once.On the left, While: first the question and then the body, which can run zero times. On the right, Repeat: first the body and then the question; with False it repeats and with True it leaves, so the body runs at least once.
Since the body comes first, whatever it assigns is available to the condition and after the loop: a variable without an initial value that the body assigns ends up with a value.
ProgramOne round even if the condition already holds
Algorithm UnaVez
Define n As Integer <- 10;
Repeat
Write "Vuelta con n = ", n;
n <- n + 1;
Until n > 5
EndAlgorithmOutput
Vuelta con n = 10
The program finishes normally.
In short
Repeatruns the body and then asks.- It ends when the condition is
True. - It always does at least one round.
Untilcloses the block.