Skip to content

SpecificationV · Declarations and statements

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.

Markdown version

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;
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.

  1. 1Before the If, premio is empty.

  2. 2Along the true branch, premio receives 10.

  3. 3Along the false branch there is no assignment: premio is still empty.

  4. 4The paths join. Since one arrives without a value, reading premio is an error.

Figure 5.5-1.Both paths leaving the 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;
EndAlgorithm

Output

0

The program finishes normally.

5.5.2 The causes

The error message says why the value is missing:

CauseSituation
no path assignsthe variable never received a value before
condition without Elseonly the true branch assigns
branch without assignmentsome branch of If, Switch or Select does not assign
the loop may not runonly the body of While, For or For Each assigns
Switch without Otherwisea 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;
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.

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.