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.
ProgramA branch that gives no value
Algorithm SinValor
Define premio As Integer;
Define puntos As Integer <- 40;
If puntos > 50 Then
premio <- 10;
EndIf
Write premio;
EndAlgorithmDiagnostics
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.
1Before the
If,premiois empty.2Along the true branch,
premioreceives 10.3Along the false branch there is no assignment:
premiois still empty.4The paths join. Since one arrives without a value, reading
premiois an error.
If reach Write. Along one of them premio is still empty.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.
It is enough to give a value on every path:
ProgramBoth branches give a value
Algorithm ConValor
Define premio As Integer;
Define puntos As Integer <- 40;
If puntos > 50 Then
premio <- 10;
Else
premio <- 0;
EndIf
Write premio;
EndAlgorithmOutput
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 |
ProgramA loop that might not run
Algorithm Bucle
Define ultimo As Integer;
Define i As Integer <- 0;
While i < 3 Do
ultimo <- i;
i <- i + 1;
EndWhile
Write ultimo;
EndAlgorithmDiagnostics
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.
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. Repeatalways runs its body once.