Pascal Programming/Enumerations
One powerful notational as well as syntactical tool of Pascal is the declaration of custom enumeration data types.
Handling
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) [[../Variables and Constants#Identifiers|identifiers]].
type
weekday = (Monday, Tuesday, Wednesday, Thursday, Friday,
Saturday, Sunday);
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:
var
startOfWeek: weekday;
begin
startOfWeek := Sunday;
end.
The variable startOfWeek is restricted to assume only legal values of the data type weekday.
Note that Sunday is not enclosed by typewriter quotation marks (') which usually indicate a string literal.
The identifier Sunday indicates a value in its own right.
Ordinal values
Automatism
Every enumeration data type declaration implicitly defines an order.
The comma-separated list is per definition a sorted list.
The built‑in function ord, short for ordinal value, gives you the opportunity to obtain the ordinal value of an enumeration element, that is an integer-value unique/specific to that enumeration member.
The first element of an enumeration is numbered as 0.
The second, if applicable, has the number 1, and so forth.
Override
Some compilers, such as the Template:Abbr, allow you to specify explicit indexes for some, or even all elements of an enumeration:
type
month = (January := 1, February, March, April, May, June,
July, August, September, October, November, December);
Here, January will have the ordinal value 1.
And all following items have an ordinal value greater than 1.
The automatic assignment of numbers still ensures every enumeration member has a unique number among the entire enumeration data type.
February will have the ordinal value 2, March the value 3, and so on.
The value 0, however, is not assigned to any element of that enumeration.
Specifying explicit indices is a non-standard extension.
In Template:Abbr’s Template:Nowrap 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 January, for instance, if it is supplied with the integer-value 1.[fn 1]
Neighbors
The standard functions pred and succ, 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 succ(January) will return February, as it is the successor of the value January.
However, pred(January) will fail as there is technically no member prior January.
An enumeration list is not cyclical.
Although in real life January follows December, the enumeration data type month does not “know” that.
The Template:Abbr standard allows a second optional integer parameter to be supplied to either pred or succ.
Template:Nowrap is identical to succ(succ(January)), yet more convenient and shorter, but also Template:Nowrap returns the same value.
Utilizing this functionality you can obtain an enumeration value given its index.
Template:Nowrap evaluates to the weekday value that has the ordinal value 3, thus virtually providing a means for an inverse ord 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
Template:Columns-list
work in conjunction with enumeration values.
For example, Template:Nowrap will evaluate to true, because January has a smaller ordinal value than February.
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 weekday value with a month value.
Nonetheless, something like Template:Nowrap is legal, since you are then in fact comparing integer values.
Note, arithmetic operators Template:Nowrap so on) do not work with enumeration data types, despite their ordinal values.
Boolean as an enumeration data type
Definition
The data type Boolean is a built‑in special enumeration data type.
It is guaranteed that
ord(false)= 0,ord(true)= 1, and, in consequence,pred(true)=false.
Logical operators
Boolean 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 not.
By preceding a Boolean expression with not (and some separator such as a space character), the expression is negated.
| expression | result |
|---|---|
not true |
false
|
not false |
true
|
Conjunction
While this may be pretty straightforward, the so-called logical conjunction, indicated by and, might not be.
The truth table for it looks like this:
value of tired
|
value of intoxicated
|
result of tired and intoxicated
|
|---|---|---|
false |
false |
false
|
false |
true |
false
|
true |
false |
false
|
true |
true |
true
|
In Template:Abbr this is frequently written as (“times”) or even omitted, because (like an mathematics) an invisible “times” is assumed.
Given that the ordinal values of false and true are as defined above, you could calculate the and result by multiplying them.
Disjunction
A little more confusing, because it may be contradictory to someone’s natural language, is the word or.
If either operand is true, the overall expression’s result becomes true.
value of raining
|
value of snowing
|
result of raining or snowing
|
|---|---|---|
false |
false |
false
|
false |
true |
true
|
true |
false |
true
|
true |
true |
true
|
Electrical engineers frequently use the symbol to denote this operation.
With respect to Boolean’s ordinal value, though, you must “define” that was still .
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
not hungry or thirsty
is fundamentally different to
not (hungry or thirsty)Ranges
Ordinal types
Enumeration data types belong to the category of ordinal data types. Other ordinal data types are:
integer,char,- and all enumeration data types, including
Boolean.
They all have in common, that a value of them can be mapped to a distinct integer-value.
The ord function lets you retrieve that value.
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 0 up to and including 23.
Yet the data type integer 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. g. integer, 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 (..):
type
majuscule = 'A'..'Z';The limits may be given as any computable expression, as long as it does not depend on run-time data.[fn 2] For example constants (that have already been defined) may be used:
type
integerNonNegative = 0..maxInt;
Note, we named this range integerNonNegative and not nonNegativeInteger, because this will facilitate alphabetical sorting of some documentation tools or in Template:Abbr.
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):
./a.out: value out of range (error #300 at 402a54)The corresponding test program has been compiled with Template:Abbr. Other compilers may emit different messages.
The default configuration of the Template:Abbr, 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 Template:Abbr cite compatibility reasons to other compilers, which decided to ignore out-of-range values for speed reasons.[fn 3]
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:
Template:Nowrap (case-insensitive) or {$R+} 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.
Template:Abbr’s ‑Cr command-line switch has the same effect.
Selections
Template:Wikibook
With the advent of enumeration data types, it may become cumbersome and tedious to check for values just using if‑branches.
Explanation
The case selection statement unites multiple exclusive if‑branches in one language construct.[fn 4]
case sign(x) of
-1:
begin
writeLn('You have entered a negative number.');
end;
0:
begin
writeLn('The numbered you have entered is sign-less.');
end;
1:
begin
writeLn('That is a positive number.');
end;
end;Between case and of any expression that evaluates to an ordinal value may appear.
After that, -1:, 0: and 1: are case labels.
These case labels mark the start of alternatives.
After a case label follows a statement.
-1, 0 and 1 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 case and of.
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 Template:Abbr case labels may contain ranges.
program letterReport(input, output);
var
c: char;
begin
write('Give me a letter: ');
readLn(c);
case c of
'A'..'Z':
begin
writeLn('Wow! That’s a big letter!');
end;
'a'..'z':
begin
writeLn('What a nice small letter.');
end;
end;
end.This shorthand notation allows you to catch many cases.
The case label 'A'..'Z': 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 Template:Abbr yields the error message duplicate case-constant in `case' statement, the Template:Abbr reports just duplicate case label[fn 5], 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.[fn 6]
If this is not desired the “Extended Pascal” standard allows a special case label called otherwise (note, without a colon).
This case treats all values that have no explicit case label associated with them.
program asciiTest(input, output);
var
c: char;
begin
write('Supply any character: ');
readLn(c);
case c of
// empty statement, so the control characters are not
// considered by the otherwise-branch as non-ASCII characters
#0..#31, #127: ;
#32..#126:
begin
writeLn('You entered an ASCII printable character.');
end;
otherwise
begin
writeLn('You entered a non-ASCII character.');
end;
end;
end.otherwise may only appear at the end.
There must be at least one case label beforehand, otherwise (no pun intended) the otherwise case is always taken, rendering the entire case-statement useless.
Template:Abbr, that is Delphi, re-uses the word [[../Expressions and Branches#Alternative statement|else]] having the same semantics, the same meaning as otherwise.
The Template:Abbr and Template:Abbr support both, although GPC can be instructed to only accept otherwise.
Tasks
Template:Question-answer Template:- Template:Question-answer Template:Newpage
Notes: Template:Smallrefs
Template:Auto navigation
Cite error: <ref> tags exist for a group named "fn", but no corresponding <references group="fn"/> tag was found