A set is a collection of elements of the same type. The differences between arrays and sets are that a set can only hold up to 256 elements and that sets and arrays can hold different types. Sets can hold characters, enumerated types or subranges. Integers are not allowed because they exceed the cardinality of 256, but a subrange of integers can be used.

Syntax

type identifier = set of base_type_identifier;

  • A set does not have the values stated in its declaration, it only is allowed to have them. Therefore, before the first use of the set, the set should have been initialized.
  • A set is unordered and each element of a set can only be included once.
  • Removing an element from an empty set does not raise an error, nothing occurs.
Examples: 
type
characterset = set of char;
weekdays = (monday,tuesday,wednesday,thursday,friday);
days = set of weekdays;
byte = 0..255;
byteset = set of byte;
Operators :
+ union
- difference
* intersection
= equality
<> inequality
>= contains (subset of)
<= is contained by
in membership
 
 
Example :
type
digit = set of 0..9;
var
odd,even : digit;
i : integer;
begin
odd := [1.3.5.7.9];
even = [2,4,6,8];
read(i);
if i in odd then
writeln('i is odd')
else writeln('i is even');
even:=even + [0];
even:=even - [0];
end.
To illustrate intersection : even * odd returns an empty set;
 
A First Look A First Look at Pascal Basic Input/Output Identifiers & Types
Constants Standard Functions If..Then Statement Case Statement
Repetitions Procedures and Functions Enumerated /Subrange Types Sets
I/O, Text Files Arrays Records Recursion
Pointer How to Debug Previous Exam Questions Advanced
ASCII Code Table Homepage University Homepage Computer Engineering Dept.