Pascal provides specialized data structures that improve code readability and efficiency when dealing with defined lists of constants and flags.
1. Enumerated Types (Enums)
An Enumerated Type (or Enum) allows you to define a list of meaningful identifiers that represent a sequence of underlying integer values.
- Benefit: Improves code readability immensely. Instead of using raw integers like
if Status = 1 then..., you use clear names:if Status = Running then.... - Definition: Defined in the
typesection. The identifiers are enclosed in parentheses.
A. Declaration and Implicit Value
By default, the compiler assigns an integer value starting from 0 to the first identifier, incrementing by 1 for each subsequent identifier.
program EnumDemo;
type
// TColor is the enumeration type
TColor = (Red, Green, Blue, Yellow); // Red = 0, Green = 1, Blue = 2, Yellow = 3
TDirection = (North, East, South, West);
var
CurrentColor: TColor;
begin
CurrentColor := Green; // Assign using the identifier, not the number
Writeln('The color is: ', CurrentColor);
// Implicitly, the value is an integer:
if Ord(CurrentColor) = 1 then // Ord() function returns the underlying integer
Writeln('The underlying ordinal value is 1.');
end.
B. Accessing Enum Values
You can use the following functions on any ordinal type, including enumerations:
| Function | Purpose | Example (CurrentColor = Green) | Result |
Ord(Value) | Returns the integer value of the ordinal. | Ord(CurrentColor) | 1 |
Succ(Value) | Returns the next value in the sequence. | Succ(CurrentColor) | Blue |
Pred(Value) | Returns the previous value in the sequence. | Pred(CurrentColor) | Red |
High(Type) | Returns the last value defined in the type. | High(TColor) | Yellow |
2. Set Types (Collections of Ordinals)
A Set is a structured type that represents a mathematical set, where the elements are a collection of values from a defined ordinal type (like Char, Boolean, or an Enum). Sets are used for efficient testing of membership and handling boolean flags.
- Size Restriction: Standard Pascal sets are often limited to ordinal types whose range fits within 256 or 512 elements, so they cannot be based on standard
IntegerorStringtypes. - Set Base Type: The ordinal type the set is built upon is called the base type.
A. Declaration and Membership
program SetDemo;
type
// Define the base type (here, TDirection from P2.1)
// TDirection = (North, East, South, West);
// TValidMoves is a set whose elements can be any of the TDirection values
TValidMoves = set of TDirection;
var
Moves: TValidMoves;
CurrentDirection: TDirection;
begin
// Initialize the set using square brackets []
Moves := [North, South, West]; // The set currently contains 3 elements
CurrentDirection := East;
// 1. Membership Test (Checking if an element is IN the set)
if CurrentDirection in Moves then
Writeln('Move is valid.')
else
Writeln('Move is blocked.'); // Output: Move is blocked.
// 2. Empty Set
Moves := []; // An empty set
end.
B. Set Operators
You can perform mathematical set operations on set variables:
| Operator | Meaning | Example |
+ | Union (Combines elements from both sets) | [A, B] + [C] $\rightarrow$ [A, B, C] |
- | Difference (Elements in the first, but NOT in the second) | [A, B, C] - [B] $\rightarrow$ [A, C] |
* | Intersection (Elements common to both sets) | [A, B] * [B, C] $\rightarrow$ [B] |
in | Membership (Checks if an element is in a set – used in conditional logic) | CharValue in ['A'..'Z'] |
type
TCharSet = set of Char;
var
AllowedChars: TCharSet;
Numbers: TCharSet;
begin
// Set creation using ranges
AllowedChars := ['A'..'Z', 'a'..'z'];
Numbers := ['0'..'9'];
// Union: All letters AND numbers
AllowedChars := AllowedChars + Numbers;
// Test membership
if '5' in AllowedChars then
Writeln('Character 5 is allowed.');
end.
Real-World Use: Sets are extremely efficient for checking multiple conditions simultaneously. For example, checking if a key pressed is a digit, a letter, or a control key in a single, fast operation.
