Skip to content

SpecificationIV · Expressions and operators

4.4

Comparison

Comparison operators ask something about two values and answer with a Boolean: True or False.

Markdown version

4.4.1 How it works

OperatorQuestion
=are they equal?
<>are they different?
< <=is it less? less or equal?
> >=is it greater? greater or equal?

What can be compared:

  • Numbers: equality and order. An integer is widened to real when compared with a real.
  • Texts and characters: equality and order, character by character by their Unicode code.
  • Booleans: only equality, no order.
  • Enumerations and records of the same type: only equality.
  • Objects: identity equality (whether they are the same object).

ProgramComparing different types

Algorithm Comparar
  Write 3 < 3.5, " ", "abc" < "abd", " ", 'a' = "a";
  Write "Zeta" < "alfa", " ", True <> False;
EndAlgorithm

Output

true true true
true true

The program finishes normally.

Figure 4.4-1.Texts are compared character by character by their code. All upper-case letters have a lower code than lower-case ones.

A line with upper-case A and Z on the left, with codes 65 and 90, and lower-case a and z on the right, with codes 97 and 122.

The result of "Zeta" < "alfa" is surprising: upper-case Z has a lower code than lower-case a. The order of texts is not dictionary order.

In short

  • Comparisons return a Boolean.
  • Numbers and texts allow equality and order; booleans, enumerations and records only equality.
  • Texts are ordered by Unicode code, not like in a dictionary.