# 1.5 How a program ends

> Every run ends in one of six states: it finished well, it failed, it is waiting for input, it is waiting for the outside world, it was cancelled or it ran out of a limit.

Web page: https://pseudolearn.app/en/spec/vision/terminacion

> **In one sentence**
>
> Every run ends in one of six states: it finished well, it failed, it is waiting for input, it is waiting for the outside world, it was cancelled or it ran out of a limit.

## 1.5.1 How it works

When a program stops moving forward, the engine says exactly why. That reason is called the **termination** and it is always one of these six:

| Termination | What it means | Can it continue? |
| --- | --- | --- |
| `completed` | The algorithm reached its `EndAlgorithm` | No: it finished |
| `failed` | A runtime failure stopped it | No |
| `awaiting_input` | A `Read` needs a value that has not arrived yet | Yes, once it receives it |
| `awaiting_effect` | It is waiting for an answer from a file, clock or network | Yes, once it receives it |
| `cancelled` | Whoever was running it stopped it | No |
| `limit_reached` | It ran out of a statement, memory or depth budget | No |

**Figure 1.5-1: The six terminations**

At the top, the running state. An arrow goes down to a box with the four final terminations: completed, failed, cancelled and limit_reached. Two side arrows lead to the pauses awaiting_input and awaiting_effect, which resume.

From "running" one of six states is reached. Two of them are pauses you can come back from.

The normal case is `completed`:

### Program: Ends normally

```pseudolearn
Algorithm Bien
  Write "Fin del programa";
EndAlgorithm
```

**Output**

```text
Fin del programa
```

*The program finishes normally.*

A runtime failure stops the program at the exact statement. What was already written is kept:

### Program: Ends with a failure

```pseudolearn
Algorithm Falla
  Define divisor As Integer <- 0;
  Write "Antes del fallo";
  Write 10 Div divisor;
  Write "Esto nunca se escribe";
EndAlgorithm
```

**Output**

```text
Antes del fallo
```

**Diagnostics**
- `divisionByZero` (error, 4:12): Division by zero

*The program finishes with an error.*

If a `Read` receives no value, the program pauses. The editor asks you for the data and the run continues where it stopped:

### Program: Waits for input

```pseudolearn
Algorithm Espera
  Define edad As Integer <- 0;
  Write "¿Cuántos años tienes?";
  Read edad;
  Write "El año que viene tendrás ", edad + 1;
EndAlgorithm
```

**Output**

```text
¿Cuántos años tienes?
```

*The program is left waiting for input.*

> **Limit**
>
> The terminations `completed`, `failed`, `cancelled` and `limit_reached` are final: no more effects happen after them. The pauses `awaiting_input` and `awaiting_effect` resume at exactly the statement where they stopped.

**In short**
- Six possible terminations; only `completed` means everything went well.
- A failure stops the program at the exact statement and keeps what was already written.
- `awaiting_input` and `awaiting_effect` are pauses that resume.

**See also**
- [1.6 Errors, warnings and failures](https://pseudolearn.app/en/spec/vision/errores.md)
- [13.4 Effect order and the tape](https://pseudolearn.app/en/spec/motor/efectos.md)
- [13.6 Engine limits](https://pseudolearn.app/en/spec/motor/limites.md)
