Skip to content

SpecificationIV · Expressions and operators

4.1

Arithmetic operators

+, - and * compute with numbers: between two integers they give an exact integer and, if either is real, they give a real.

Markdown version

4.1.1 How it works

An expression combines values with operators to compute another value. The basic arithmetic operators are addition +, subtraction - and multiplication *, plus the minus sign in front of a number.

The type of the result depends on the operands:

OperandsResult
Integer and Integerexact Integer
at least one RealReal
Figure 4.1-1.Two integers give an integer; a single real is enough for the result to be real.

At the top, Integer plus Integer gives Integer. Below, Integer times Real gives Real.

ProgramThe basic sums

Algorithm Aritmetica
  Define precio As Integer <- 7;
  Define cantidad As Integer <- 3;
  Write precio + cantidad, " ", precio - cantidad, " ", precio * cantidad;
  Write precio * 1.5, " ", -precio;
EndAlgorithm

Output

10 4 21
10.5 -7

The program finishes normally.

Arithmetic operators only accept numbers. Adding a number and a boolean, or a number and a text, is detected before running.

In short

  • +, - and * operate on numbers.
  • Two integers give an exact integer; if there is a real, the result is real.
  • Going outside the integer range stops the program.