# 6.4 The Repeat loop

> `Repeat` runs the body first and asks afterwards: it repeats **until** the condition is true, and always does at least one round.

Web page: https://pseudolearn.app/en/spec/control/repetir

> **In one sentence**
>
> `Repeat` runs the body first and asks afterwards: it repeats **until** the condition is true, and always does at least one round.

## 6.4.1 How to write it

```pseudolearn
Repeat
  «instrucciones»
Until «condición»
```

> **Parts**
>
> - `Repeat` opens the loop.
> - «statements» is the body.
> - `Until` closes the block: there is no `EndRepeat`.
> - «condition» is a `Boolean` expression. When it is `True`, the loop **ends**.

## 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`.

### Program: Rolling until a 6 comes up

```pseudolearn
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;
EndAlgorithm
```

**Output**

```text
Salió el 6 en el intento 3
```

*The program finishes normally.*

The result is always the same because the engine's randomness is [deterministic](https://pseudolearn.app/en/spec/biblioteca/azar.md) with this book's seed.

**Figure 6.4-1: While versus Repeat**

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.

In `Repeat` the body comes before the question. Even if the condition already holds, the body runs 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.

### Program: One round even if the condition already holds

```pseudolearn
Algorithm UnaVez
  Define n As Integer <- 10;
  Repeat
    Write "Vuelta con n = ", n;
    n <- n + 1;
  Until n > 5
EndAlgorithm
```

**Output**

```text
Vuelta con n = 10
```

*The program finishes normally.*

> **Common mistakes**
>
> ### Program: Until with a number
>
> ```pseudolearn
> Algorithm RepetirNumero
>   Define n As Integer <- 3;
>   Repeat
>     n <- n - 1;
>   Until n
> EndAlgorithm
> ```
>
> **Diagnostics**
> - `nonBooleanCondition` (error, 5:9): Condition must be a boolean expression
>
> *The program finishes with an error.*

> **Under the hood**
>
> The body is the first block and the condition goes at the end, with a backward jump while it is still false.
>
> **Intermediate representation**
>
> ```text
> module UnaVez en
> func suspendible @main() -> unit {
> b0() mass 2:
>   stmt.enter @2:3-2:28
>   %0 = const.i64 10 @2:26-2:28
>   stmt.exit @2:3-2:28
>   stmt.enter @3:3-6:14
>   jump b1(%0)
> b1(%1: i64) mass 2:
>   stmt.enter @4:5-4:31
>   %2 = const.str "Vuelta con n = " @4:11-4:28
>   out.write %2, %1, newline @4:5-4:31
>   stmt.exit @4:5-4:31
>   stmt.enter @5:5-5:15
>   %3 = const.i64 1 @5:14-5:15
>   %4 = int.add %1, %3 @5:12-5:13
>   stmt.exit @5:5-5:15
>   %5 = const.i64 5 @6:13-6:14
>   %6 = int.cmp gt, %4, %5 @6:11-6:12
>   decide.bool %6 @6:9-6:14
>   branch %6, b2(), b3()
> b2() mass 0:
>   stmt.exit @3:3-6:14
>   return @7:1-7:13
> b3() mass 0:
>   task.yield_check @1:1-1:17
>   jump b1(%4) @1:1-1:17
> }
> ```

> **Note**
>
> Translate `cuenta-atras` from `While` to `Repeat`. The condition has to be inverted, and with `n` at 0 from the start the `Repeat` version writes a 0 that the other one does not.

**In short**
- `Repeat` runs the body and then asks.
- It ends when the condition is `True`.
- It always does at least one round.
- `Until` closes the block.

**See also**
- [6.3 The While loop](https://pseudolearn.app/en/spec/control/mientras.md)
- [5.5 Variables without a value](https://pseudolearn.app/en/spec/declaraciones/sin-valor.md)
- [12.5 Reproducible randomness](https://pseudolearn.app/en/spec/biblioteca/azar.md)
