# 12.6 Read and Write

> `Write` shows values on the output and `Read` asks the person using the program for values: they are the simplest way to talk to the outside.

Web page: https://pseudolearn.app/en/spec/biblioteca/consola

> **In one sentence**
>
> `Write` shows values on the output and `Read` asks the person using the program for values: they are the simplest way to talk to the outside.

## 12.6.1 How to write it

```pseudolearn
Write ⟦«expresión», «expresión»⟧ ⟦Without Newline⟧;
Read «destino», «destino»;
```

> **Parts**
>
> - `Write` evaluates zero or more comma-separated expressions, from left to right.
> - `Without Newline` is optional and goes at the end: it avoids the line break.
> - `Read` asks for one value per [designator](https://pseudolearn.app/en/spec/declaraciones/designadores.md), one at a time and in order.

## 12.6.2 How it works

`Write` glues the representations of its values **without a separator** and ends with a line break, unless `Without Newline` is given. An empty `Write` writes a blank line.

`Read` converts every input according to the target's type. If the input does not fit, it warns with `readValueTypeMismatch` and **asks again** for the same value without touching the variable. Once `Read` gives a variable a value, that variable has a value for the [initialization check](https://pseudolearn.app/en/spec/declaraciones/sin-valor.md).

### Program: Asking for two numbers

```pseudolearn
Algorithm Suma
  Define a, b As Integer;
  Write "Primer número: " Without Newline;
  Read a;
  Write "Segundo número: " Without Newline;
  Read b;
  Write a, " + ", b, " = ", a + b;
  Write;
  Write "fin";
EndAlgorithm
```

**Input**

```text
3
4
```

**Output**

```text
Primer número: Segundo número: 3 + 4 = 7

fin
```

*The program finishes normally.*

**Figure 12.6-1: Input, variables and output**

The input holds 3 and 4. Read stores them in a and b. Write shows 3 + 4 = 7 on the output.

`Read` takes values from the input, one per target. `Write` puts text on the output, on one line unless `Without Newline` is used.

If no input is available, execution waits. In the app a field appears to type into; in the terminal standard input is read.

> **Common mistakes**
>
> ### Program: Without Newline in the middle
>
> ```pseudolearn
> Algorithm Mal
>   Write Without Newline "a";
> EndAlgorithm
> ```
>
> **Diagnostics**
> - `unexpectedModifierPosition` (error, 2:25): Without newline modifier must be placed at the end of the statement
>
> *The program finishes with an error.*

> **Under the hood**
>
> `Write` becomes one output instruction per expression and a final line break. `Read` is an instruction that suspends execution until the host hands over a value.
>
> **Intermediate representation**
>
> ```text
> module Suma en
> func suspendible @main() -> unit {
> b0() mass 8:
>   stmt.enter @2:3-2:25
>   %0 = value.undefined i64 @2:3-2:25
>   %1 = value.undefined i64 @2:3-2:25
>   stmt.exit @2:3-2:25
>   stmt.enter @3:3-3:42
>   %2 = const.str "Primer número: " @3:9-3:26
>   out.write %2 @3:3-3:42
>   stmt.exit @3:3-3:42
>   stmt.enter @4:3-4:9
>   %3 = in.read i64 @4:3-4:9
>   stmt.exit @4:3-4:9
>   stmt.enter @5:3-5:43
>   %4 = const.str "Segundo número: " @5:9-5:27
>   out.write %4 @5:3-5:43
>   stmt.exit @5:3-5:43
>   stmt.enter @6:3-6:9
>   %5 = in.read i64 @6:3-6:9
>   stmt.exit @6:3-6:9
>   stmt.enter @7:3-7:34
>   %6 = const.str " + " @7:12-7:17
>   %7 = const.str " = " @7:22-7:27
>   %8 = int.add %3, %5 @7:31-7:32
>   out.write %3, %6, %5, %7, %8, newline @7:3-7:34
>   stmt.exit @7:3-7:34
>   stmt.enter @8:3-8:8
>   %9 = const.str ""
>   out.write %9, newline @8:3-8:8
>   stmt.exit @8:3-8:8
>   stmt.enter @9:3-9:14
>   %10 = const.str "fin" @9:9-9:14
>   out.write %10, newline @9:3-9:14
>   stmt.exit @9:3-9:14
>   return @10:1-10:13
> }
> ```

**In short**
- `Write` joins values without a separator and breaks the line, unless `Without Newline`.
- `Read` asks for one value per target and converts it to the type.
- An invalid input is asked for again with a warning.

**See also**
- [10.2 Severity and classes](https://pseudolearn.app/en/spec/diagnosticos/severidad.md)
- [4.6 Concatenation](https://pseudolearn.app/en/spec/expresiones/concatenacion.md)
- [11.4 Effects and capabilities](https://pseudolearn.app/en/spec/tareas/efectos.md)
