Pascal Programming/Enumerations

One powerful notational as well as syntactical tool of Pascal is the declaration of custom enumeration data types.

Notion
An enumeration data type is a finite list of named discrete values. Enumerations virtually give names to individual integer values, however, you cannot (directly) do arithmetic operations on it.

Declaration
An enumeration data type is declared by following the data type identifier with a non-empty comma-separated list of (new, not previously used) identifiers. The individual list items refer to specific values the data type may assume. The data type identifier identifies the data type as a whole.

Operations
Once an enumeration data type has been declared, you can use it like any other data type: The variable  is restricted to assume only legal values of the data type. Note that  is not enclosed by typewriter quotation marks  which usually indicate a string literal. The identifier  indicates a value in its own right.

Automatism
Every enumeration data type declaration implicitly defines an order. The comma-separated list is per definition a sorted list. The built‑in function, short for ordinal value, gives you the opportunity to obtain the ordinal value of an enumeration element, that is an  -value unique/specific to that enumeration member.

The first element of an enumeration is numbered as. The second, if applicable, has the number, and so forth.

Override
Some compilers, such as the FPC, allow you to specify explicit indexes for some, or even all elements of an enumeration: Here,  will have the ordinal value. And all following items have an ordinal value greater than. The automatic assignment of numbers still ensures every enumeration member has a unique number among the entire enumeration data type. will have the ordinal value,   the value  , and so on. The value, however, is not assigned to any element of that enumeration.

Specifying explicit indices is a non-standard extension. In FPC’s you need to use a plain equal sign  instead of. This is also referred to as “C‑style enumeration declaration”, since the programming language C uses that syntax.

Inverse
Pascal does not provide a generic function that lets you determine the enumeration element based on a number. There is no function returning, for instance, if it is supplied with the  -value.

Neighbors
The standard functions  and , short for predecessor and successor respectively, are automatically defined for every enumeration data type. These functions return the previous or next value of an enumeration value. For example  will return , as it is the successor of the value.

However,  will fail as there is technically no member prior. An enumeration list is not cyclical. Although in real life January follows December, the enumeration data type  does not “know” that.

The EP standard allows a second optional  parameter to be supplied to either   or. is identical to, yet more convenient and shorter, but also  returns the same value.

Utilizing this functionality you can obtain an enumeration value given its index. evaluates to the  value that has the ordinal value , thus virtually providing a means for an inverse   function. However, it is necessary to know the first element of the enumeration though, and the enumeration may not use any explicit indices in its declaration (unless all indices coincide with the automatic numbering pattern).

Operators
Enumeration data type values are automatically eligible to be used with several operators. Since every enumeration value has an ordinal value, they can be ordered and you can test for that. The relational operators • 3 work in conjunction with enumeration values. For example, will evaluate to , because   has a smaller ordinal value than.

Although, technically you can compare apples and oranges (spoiler alert: they are unequal), all relational operators only work in conjunction with two values of the same kind. In Pascal, you cannot compare a  value with a   value. Nonetheless, something like is legal, since you are then in fact comparing   values.

Note, arithmetic operators (,, and so on) do not work with enumeration data types, despite their ordinal values.

Definition
The data type  is a built‑in special enumeration data type. It is guaranteed that
 * = 0,
 * = 1, and, in consequence,

Logical operators
is only enumeration data type operations can be directly performed on using logical operators.

Negation
The most basic operator is the negation. It is a unary operator, that means it expects only one operand. In Pascal it uses the keyword. By preceding a  expression with   (and some separator such as a space character), the expression is negated.

Conjunction
While this may be pretty straightforward, the so-called logical conjunction, indicated by, might not be. The truth table for it looks like this: In EE this is frequently written as $$\cdot$$ (“times”) or even omitted, because (like an mathematics) an invisible “times” is assumed. Given that the ordinal values of  and   are as defined above, you could calculate the   result by multiplying them.

Disjunction
A little more confusing, because it may be contradictory to someone’s natural language, is the word. If either operand is, the overall expression’s result becomes. Electrical engineers frequently use the $$+$$ symbol to denote this operation. With respect to ’s ordinal value, though, you must “define” that $$1 + 1$$ was still $$1$$.

Precedence
Like the usual rule in mathematics “multiplication and division first, then addition and subtraction”, a conjunction is evaluated first before a disjunction is. However, since the negation is a unary operator, it is evaluated first in any case. That means you must be really careful not to forget placing parenthesis. The expression is fundamentally different to

Ordinal types
Enumeration data types belong to the category of ordinal data types. Other ordinal data types are: They all have in common, that a value of them can be mapped to a distinct -value. The  function lets you retrieve that value.
 * and all enumeration data types, including.
 * and all enumeration data types, including.
 * and all enumeration data types, including.

Intervals
Sometimes, it makes sense to restrict a set of values to a certain range. For instance, the hours on a military time clock may show values from  up to and including. Yet the data type   will permit other values too.

Pascal allows you to declare (sub‑)range data types. A (sub‑)range data type has a host data type, e.&#8239;g. , and two limits. One lower and one upper limit. A range is specified by giving the limits in ascending order, separated through two periods back-to-back : The limits may be given as any computable expression, as long as it does not depend on run-time data. For example constants (that have already been defined) may be used: Note, we named this range  and not , because this will facilitate alphabetical sorting of some documentation tools or in IDEs.

Restriction
A variable possessing one (sub‑)range data type may only assume values within the range. If the variable exceeds its legal range, the program aborts. The following error message may appear (memory address at the end can vary): The corresponding test program has been compiled with GPC. Other compilers may emit different messages.

The default configuration of the FPC, however, ignores this. Assigning out-of-range values to variables will not yield an error (if it depends on run-time data). The developers of the FPC cite compatibility reasons to other compilers, which decided to ignore out-of-range values for speed reasons. You need to specifically request that illegal values cannot be assigned to ordinal type variables. This can be done by placing a specially crafted comment prior any (crucial) assignments: (case-insensitive) or  for short (case-sensitive) will ensure illegal values are not assigned and the program aborts if any attempts are made anyway. Specifying this compiler switch once in your source code file is sufficient. FPC’s  command-line switch has the same effect.

Selections
With the advent of enumeration data types, it may become cumbersome and tedious to check for values just using ‑branches.

Explanation
The  selection statement unites multiple exclusive  ‑branches in one language construct. Between  and   any expression that evaluates to an ordinal value may appear. After that,,   and   are case labels. These case labels mark the start of alternatives. After a case label follows a statement.

,  and   denote case values. Every case label consists of a non-empty comma-separated list of case values, followed by a colon. All case values have to be legal constant values, constant expressions, that are compatible to the comparison expression above, what is written between  and. Every specified case value needs to appear exclusively in one case label. No case label value can appear twice. It is not necessary to put them in order, according their ordinal value, although it can make your source code more readable.

Shorthand for many cases
In EP case labels may contain ranges. This shorthand notation allows you to catch many cases. The case label  includes all upper-case letters, without requiring you to list them all individually.

Take care that no range overlaps with other case label values. This is forbidden. Good processors will complain about such a mistake though. The GPC yields the error message, the FPC reports just  , both telling you some information about the location in your source code.

Fall-back
It is important that any (expected) value of the comparison expression matches one case label. If the comparison expression evaluates to a value no case label contains the corresponding value, the program aborts. If this is not desired the “Extended Pascal” standard allows a special case label called  (note, without a colon). This case treats all values that have no explicit case label associated with them. may only appear at the end. There must be at least one case label beforehand, otherwise (no pun intended) the  case is always taken, rendering the entire  -statement useless.

BP, that is Delphi, re-uses the word  having the same semantics, the same meaning as. The FPC and GPC support both, although GPC can be instructed to only accept.

Tasks
Notes: