100% Free Forever
AI-Powered Learning
Industry Expert Content
Certificates & Badges
Learn At Your Own Pace
Programming

Sets in Pascal

Explore Pascal's native set type — union, intersection, difference, and membership testing over ordinal values.

Data StructuresIntermediate7 min readJul 10, 2026
Analogies

Introduction to Sets

Pascal has a built-in set type that directly models mathematical sets of a base ordinal type, declared as 'type TVowelSet = set of Char;' or more narrowly 'set of 1..100', and set literals are written with square brackets, such as '['A','E','I','O','U']'. Because sets are a native language feature rather than a library, operations like membership, union, and intersection compile to efficient bitwise operations rather than loops.

🏏

Cricket analogy: A set of dismissed batsmen is like a scorer's quick tally sheet of who's already out — checking 'if Player in OutSet' answers 'has this batsman already been dismissed' instantly, without scanning the whole scorecard.

Set Operations

Pascal defines set union with '+', intersection with '*', and difference with '-', so given 'A := [1,2,3]; B := [2,3,4];', the expression 'A + B' yields [1,2,3,4], 'A * B' yields [2,3], and 'A - B' yields [1]. Membership is tested with 'in', as in 'if 3 in A then', and these operators make sets an elegant way to express conditions that would otherwise require chains of 'or' comparisons, such as replacing 'if (ch = 'a') or (ch = 'e') or (ch = 'i') then' with 'if ch in ['a','e','i'] then'.

🏏

Cricket analogy: Union is like combining the sets of players who scored a century and players who took a five-wicket haul into one 'all-rounders of the match' set — a single '+' replaces a long chain of 'or' checks.

Practical Constraints of Pascal Sets

Standard Pascal sets are typically limited to a small base range (historically often 0..255 elements, implementation-dependent), because the compiler represents a set as a bitmap where each possible element occupies one bit, so 'set of Byte' is common and efficient, while 'set of Integer' with a huge range is usually rejected or impractical. This bitmap representation is exactly why set operations are so fast, but it also means sets are unsuitable for representing large or sparse value ranges.

🏏

Cricket analogy: It's like a bowling figures tracker limited to overs 1 through 50 in an ODI — the bitmap only needs 50 bits, so it's efficient, but you couldn't use the same tiny bitmap to track something with millions of possible values like exact runs scored across a career.

pascal
type
  TCharSet = set of Char;
var
  vowels, letter: TCharSet;
  ch: Char;
begin
  vowels := ['a', 'e', 'i', 'o', 'u'];
  write('Enter a letter: ');
  readln(ch);
  if ch in vowels then
    writeln(ch, ' is a vowel')
  else
    writeln(ch, ' is a consonant');
end.

The 'in' operator combined with a set literal is often the cleanest replacement for long chains of OR comparisons: 'if ch in ['a','e','i','o','u']' is both more readable and, on many compilers, faster than five separate equality checks joined by OR.

  • Pascal sets model mathematical sets of an ordinal base type using bitmaps internally.
  • Set literals use square brackets, e.g. ['A','E','I','O','U'].
  • Union (+), intersection (*), and difference (-) are built-in set operators.
  • The 'in' operator tests membership efficiently, replacing long OR chains.
  • Sets are typically limited to small base ranges due to their bitmap representation.
  • Sets are ideal for compact, fixed-range membership checks, not large or sparse ranges.

Practice what you learned

Was this page helpful?

Topics covered

#Programming#PascalStudyNotes#SetsInPascal#Sets#Pascal#Set#Operations#StudyNotes#SkillVeris#ExamPrep