Skip to content

SpecificationIII · Types and values

3.1

Primitive types

Every piece of data has a type that says which values it can hold and what can be done with it; PseudoLearn has five primitive types and every other type is built from them.

Markdown version

3.1.1 How to write it

Define «name» As Integer
Define «name» As Real
Define «name» As Boolean
Define «name» As Character
Define «name» As String
Figure 3.1-1.The five primitive types. Two hold numbers, one a yes-or-no answer and two hold text: a single symbol or a row of symbols.

Five boxes. Integer with the value 42; Real with 3.14; Boolean with its two values, True and False; Character with a single letter L; and String with the letters of Lucía in five boxes numbered 0 to 4.

3.1.2 How it works

A variable is declared with Define, its name, the word As and its type. From that moment it only accepts values of that type, and its type does not change while the program runs.

ProgramLucía's card

Algorithm FichaDeLucia
  Define edad As Integer <- 11;
  Define altura As Real <- 1.42;
  Define inicial As Character <- 'L';
  Define nombre As String <- "Lucía";
  Define aprobada As Boolean <- True;
  Write nombre + " (" + inicial + ")";
  Write edad, " años, ", altura, " m";
  Write aprobada;
EndAlgorithm

Output

Lucía (L)
11 años, 1.42 m
true

The program finishes normally.

TypeWhat it holdsExample values
IntegerA number without decimals0, 42, -7
RealA number with a decimal part3.14, 0.5, -2.0
BooleanThe answer to a yes-or-no questionTrue, False
CharacterA single symbol'A', "7"
StringA row of symbols, even an empty one"Hola", ""

3.1.3 Integer

An Integer is a number without a decimal part. Integer operations give the exact result as long as it fits in the range. Going outside the range does not wrap around silently: the program stops.

3.1.4 Real

A Real is a number with a decimal part. When written it always shows at least one decimal, so it can be told apart from an integer at a glance.

ProgramReals show their point

Algorithm Precios
  Define precio As Real <- 7;
  Write precio;
  Write 0.1 + 0.2;
EndAlgorithm

Output

7.0
0.30000000000000004

The program finishes normally.

The second line surprises almost everyone. It is not an engine bug: that is how reals work on any computer. A Real cannot hold just any number; it holds the nearest mark on a very fine ruler that still has gaps.

Figure 3.1-2.The ruler with gaps. 0.3 and 0.1 + 0.2 fall on neighbouring marks of the ruler, so they are not the same number.

A horizontal ruler with separate marks. The literal 0.3 points at one mark; the result of 0.1 plus 0.2 points at the neighbouring mark, 0.30000000000000004.

3.1.5 Boolean

A Boolean has exactly two values, True and False. It is the type of every question: edad >= 18 is not a number, it is a Boolean. It is not a number, it has no order and it does not turn into text by itself. When written it appears with the profile's word: Verdadero in Spanish and true in English.

3.1.6 Character and String

A Character is a single symbol: a letter, a digit, a sign, a space or an emoji. A String is a row of characters that may be empty. Positions inside a string count from zero.

ProgramMeasuring and looking inside a text

Algorithm Textos
  Define cara As Character <- "😀";
  Write length(""), " ", length("año"), " ", charAt("Hola", 0);
  Write cara, " ", charCode(cara), " ", charFromCode(76);
EndAlgorithm

Output

0 3 H
😀 128512 L

The program finishes normally.

3.1.7 Moving from one type to another

The language only converts a value on its own when no information can be lost: from Integer to Real and from Character to String. Everything else is requested with a function, because how to convert is the decision of whoever writes the program.

ProgramRequested conversions

Algorithm Conversiones
  Define nota As Real <- -8.7;
  Write trunc(nota), " ", round(nota), " ", toText(1.5) + "!";
EndAlgorithm

Output

-8 -9 1.5!

The program finishes normally.

trunc cuts the decimals towards zero and round rounds to the nearest integer. With -8.7 they give different results, which is why the language does not choose for you.

In short

  • There are five primitive types: Integer, Real, Boolean, Character and String.
  • The type of a variable never changes.
  • An Integer is exact within its range; going outside stops the program.
  • A Real holds the nearest mark on a finite ruler and always shows a decimal.
  • A Boolean is not a number and has no order.
  • Only Integer to Real and Character to String happen by themselves.