# 6.3 The While loop

> `While` repeats a block **while** a condition is true, and checks it before every round, including the first.

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

> **In one sentence**
>
> `While` repeats a block **while** a condition is true, and checks it before every round, including the first.

## 6.3.1 How to write it

```pseudolearn
While «condición» Do
  «instrucciones»
EndWhile
```

> **Parts**
>
> - `While` opens the loop.
> - «condition» is a `Boolean` expression.
> - `Do` separates the condition from the body. It is required.
> - «statements» is the body. It can be empty.
> - `EndWhile` closes the loop.

## 6.3.2 How it works

Every **iteration** starts by checking the condition. If it is `True`, the body runs and control goes back to the top. If it is `False`, the loop ends and the program continues after `EndWhile`. If the condition is false from the start, the body runs **zero** times.

### Program: Countdown

```pseudolearn
Algorithm CuentaAtras
  Define n As Integer <- 3;
  While n > 0 Do
    Write n;
    n <- n - 1;
  EndWhile
  Write "¡Despegue!";
EndAlgorithm
```

**Output**

```text
3
2
1
¡Despegue!
```

*The program finishes normally.*

**Figure 6.3-1: The While cycle**

A diamond with the condition n > 0. If it is true the body runs and control returns to the diamond; if it is false the loop is left towards EndWhile. Beside it, the value of n goes down from 3 to 0.

The question comes before the body. With `n` at 0 the answer is `False` and the loop is left on the right.

1. Step 1: `n` is 3: the condition is true and the body runs.
2. Step 2: The body writes 3 and leaves `n` at 2. Back to the question.
3. Step 3: After three rounds `n` is 0: the condition is false and the loop ends.

Something inside the body must bring the condition closer to `False`. If nothing changes it, the loop never ends. Depending on where it runs, a host [limit](https://pseudolearn.app/en/spec/motor/limites.md) on steps or time cuts it, or it keeps going until you stop it.

> **Common mistakes**
>
> ### Program: A numeric condition
>
> ```pseudolearn
> Algorithm MientrasNumero
>   Define n As Integer <- 3;
>   While n Do
>     n <- n - 1;
>   EndWhile
> EndAlgorithm
> ```
>
> **Diagnostics**
> - `nonBooleanCondition` (error, 3:9): Condition must be a boolean expression
>
> *The program finishes with an error.*

> **Under the hood**
>
> A `While` is three blocks: the header with the condition, the body that jumps back to the header and the exit. Notice the backward jump at the end of the body.
>
> **Intermediate representation**
>
> ```text
> module CuentaAtras en
> func suspendible @main() -> unit {
> b0() mass 2:
>   stmt.enter @2:3-2:27
>   %0 = const.i64 3 @2:26-2:27
>   stmt.exit @2:3-2:27
>   stmt.enter @3:3-6:11
>   jump b1(%0)
> b1(%1: i64) mass 0:
>   %2 = const.i64 0 @3:13-3:14
>   %3 = int.cmp gt, %1, %2 @3:11-3:12
>   decide.bool %3 @3:3-6:11
>   branch %3, b2(), b3()
> b2() mass 2:
>   stmt.enter @4:5-4:12
>   out.write %1, newline @4:5-4:12
>   stmt.exit @4:5-4:12
>   stmt.enter @5:5-5:15
>   %4 = const.i64 1 @5:14-5:15
>   %5 = int.sub %1, %4 @5:12-5:13
>   stmt.exit @5:5-5:15
>   jump b4()
> b3() mass 1:
>   stmt.exit @3:3-6:11
>   stmt.enter @7:3-7:21
>   %6 = const.str "¡Despegue!" @7:9-7:21
>   out.write %6, newline @7:3-7:21
>   stmt.exit @7:3-7:21
>   return @8:1-8:13
> b4() mass 0:
>   task.yield_check @1:1-1:22
>   jump b1(%5) @1:1-1:22
> }
> ```

> **Note**
>
> The typical mistake is forgetting to update the variable in the condition. Predict what happens if `n <- n - 1;` is deleted, and check it in the app, which cuts the run when it reaches its limit.

**In short**
- `While` checks before every round.
- It can run the body zero times.
- The body must bring the condition closer to `False`.

**See also**
- [6.4 The Repeat loop](https://pseudolearn.app/en/spec/control/repetir.md)
- [6.5 The counted loop For](https://pseudolearn.app/en/spec/control/para.md)
- [6.7 Leaving a loop](https://pseudolearn.app/en/spec/control/salir.md)
