# 5.5 Variables without a value

> Before running, the engine walks every possible path of the program and rejects reading a variable that some path has not given a value to.

Web page: https://pseudolearn.app/en/spec/declaraciones/sin-valor

> **In one sentence**
>
> Before running, the engine walks every possible path of the program and rejects reading a variable that some path has not given a value to.

## 5.5.1 How it works

A variable declared without an initial value is empty. It is not 0 or anything similar. Reading it at a point that some path reaches without having given it a value is the error `variableUsedUninitialized`, and it is detected **before running**.

### Program: A branch that gives no value

```pseudolearn
Algorithm SinValor
  Define premio As Integer;
  Define puntos As Integer <- 40;
  If puntos > 50 Then
    premio <- 10;
  EndIf
  Write premio;
EndAlgorithm
```

**Diagnostics**
- `variableUsedUninitialized` (error, 7:9): Variable premio may have no value at this point (cause: a condition without an alternative branch may leave it without a value)

*The program finishes with an error.*

It does not matter that `puntos` is 40: the engine does not run the program to decide, it looks at the paths. There is a path —the one where the condition is false— that reaches `Write` with `premio` empty.

**Figure 5.5-1: Paths without a value**

A diagram with the condition puntos > 50. Along the true branch premio receives 10; along the false one it receives nothing. Both paths reach Write premio, which is marked as an error.

Both paths leaving the `If` reach `Write`. Along one of them `premio` is still empty.

1. Step 1: Before the `If`, `premio` is empty.
2. Step 2: Along the true branch, `premio` receives 10.
3. Step 3: Along the false branch there is no assignment: `premio` is still empty.
4. Step 4: The paths join. Since one arrives without a value, reading `premio` is an error.

It is enough to give a value on **every** path:

### Program: Both branches give a value

```pseudolearn
Algorithm ConValor
  Define premio As Integer;
  Define puntos As Integer <- 40;
  If puntos > 50 Then
    premio <- 10;
  Else
    premio <- 0;
  EndIf
  Write premio;
EndAlgorithm
```

**Output**

```text
0
```

*The program finishes normally.*

## 5.5.2 The causes

The error message says **why** the value is missing:

| Cause | Situation |
| --- | --- |
| no path assigns | the variable never received a value before |
| condition without `Else` | only the true branch assigns |
| branch without assignment | some branch of `If`, `Switch` or `Select` does not assign |
| the loop may not run | only the body of `While`, `For` or `For Each` assigns |
| `Switch` without `Otherwise` | a value of the selector enters no branch |

### Program: A loop that might not run

```pseudolearn
Algorithm Bucle
  Define ultimo As Integer;
  Define i As Integer <- 0;
  While i < 3 Do
    ultimo <- i;
    i <- i + 1;
  EndWhile
  Write ultimo;
EndAlgorithm
```

**Diagnostics**
- `variableUsedUninitialized` (error, 8:9): Variable ultimo may have no value at this point (cause: the loop that assigns it a value may not run)

*The program finishes with an error.*

`Repeat` is different: its body runs at least once, so what it assigns counts after the loop. And `Return` or `Exit` cut their path: that path no longer requires anything.

> **Limit**
>
> The fields of a local record are tracked one by one. Array cells and object attributes are not tracked: reading an empty one is discovered when running, with the trap `uninitializedVariableRead`.

**In short**
- A variable without an initial value starts empty.
- Reading it where some path leaves it empty is an error before running.
- The message explains the cause: branch, loop or incomplete `Switch`.
- `Repeat` always runs its body once.

**See also**
- [5.1 Declaring variables with Define](https://pseudolearn.app/en/spec/declaraciones/definir.md)
- [6.1 The If decision](https://pseudolearn.app/en/spec/control/si.md)
- [6.3 The While loop](https://pseudolearn.app/en/spec/control/mientras.md)
