A set
is a collection of distinct objects. In Pascal, a set
can be declared as:
type
_setidf_ = set of _datatype_;
var
_setName_: _setidf_;
_setidf_
: The Pascal type of the set._datatype_
: The data type of the elements of the set._setName_
: The variable of the set.Consider the code snippet below, which demonstrates how a set
is declared in Pascal:
program SetDeclaration;typecharSet = set of Char;vars1: charSet;ch: Char;function printSet(s1: charSet): integer;varch: Char;beginwrite('[');for ch in s1 dobeginwrite(ch);write(', ')end;writeln(']');printSet := 0;end;begins1 := ['a', 'b', 'c'];//printing s1write('s1: ');printSet(s1);end.
A set type charSet
, which is a set of characters, is declared in line 4. A set s1
is declared in line 7 of type charSet
. The printSet
function declared in line 10 prints the elements of a set. The elements of the set s1
are defined in line 27.
Set operators are used to perform operations on sets. The set operators in Pascal are as follows:
The union of two sets, A
and B
, contains the elements that are in set A
and set B
, or in both sets A
and B
. For example:
The set union operator in Pascal is denoted by the +
operator.
Consider the code snippet below, which demonstrates the use of the +
operator:
program SetOperator;typecharSet = set of Char;vars1: charSet;s2: charSet;s3: charSet;ch: Char;function printSet(s1: charSet): integer;varch: Char;beginwrite('[');for ch in s1 dobeginwrite(ch);write(', ')end;writeln(']');printSet := 0;end;begins1 := ['a', 'b', 'c'];s2 := ['c', 'd', 'e', 'f'];s3 := s1 + s2;//printing s3write('s3: ');printSet(s3);end.
The intersection of two sets, A
and B
, contains the elements that are in both set A
and set B
. For example:
The set intersection operator in Pascal is denoted by the *
operator.
Consider the code snippet below, which demonstrates the use of the *
operator:
begins1 := ['a', 'b', 'c', 'd'];s2 := ['c', 'd', 'e', 'f'];s3 := s1 * s2;//printing s3write('s3: ');printSet(s3);end.
The difference of two sets, A
and B
, contains the elements that are in set A
but are not in set B
. For example:
The set difference operator in Pascal is denoted by the -
operator.
Consider the code snippet below, which demonstrates the use of the -
operator:
begins1 := ['a', 'b', 'c'];s2 := ['c', 'd', 'e', 'f'];s3 := s1 - s2;//printing s3write('s3: ');printSet(s3);end.
The symmetric difference of two sets, A
and B
, contains the elements that are either in set A
or in set B
, but not in both set A
and set B
. For example:
The set symmetric difference operator in Pascal is denoted by the ><
operator.
Consider the code snippet below, which demonstrates the use of the ><
operator:
begins1 := ['a', 'b', 'c', 'd'];s2 := ['c', 'd', 'e', 'f'];s3 := s1 >< s2;//printing s3write('s3: ');printSet(s3);end.
The equality of two sets, A
and B
, means that all elements of set A
and set B
are the same. For example:
The set equality operator in Pascal is denoted by the =
operator.
Consider the code snippet below, which demonstrates the use of the =
operator:
begins1 := ['a', 'b', 'c', 'd'];s2 := ['c', 'd', 'e', 'f'];s3 := s1 = s2;writeln('s3: ', s3);s1 := ['a', 'b', 'c', 'd'];s2 := ['a', 'b', 'c', 'd'];s3 := s1 = s2;writeln('s3: ', s3);end.
The non-equality of two sets, A
and B
, means that at least one element of set A
and set B
is not the same. For example:
The set not-equality operator in Pascal is denoted by the <>
operator.
Consider the code snippet below, which demonstrates the use of the <>
operator:
begins1 := ['a', 'b', 'c', 'd'];s2 := ['a', 'b', 'c', 'd'];s3 := s1 <> s2;writeln('s3: ', s3);s1 := ['a', 'b', 'c', 'd'];s2 := ['a', 'b', 'c', 'd', 'e'];s3 := s1 <> s2;writeln('s3: ', s3);end.
A set A
is said to be the subset of set B
if all the elements of set A
are also in set B
. For example:
The set subset operator in Pascal is denoted by the <=
operator.
Consider the code snippet below, which demonstrates the use of the <=
operator:
begins1 := ['a', 'b', 'c', 'd'];s2 := ['a', 'b'];s3 := s1 <= s2;writeln('s3: ', s3);s1 := ['a', 'b', 'c', 'd'];s2 := ['a', 'b', 'c', 'd', 'e'];s3 := s1 <= s2;writeln('s3: ', s3);end.
An object is said to be a member of set A
if it is an element of set A
. For example:
The set membership operator in Pascal is denoted by the in
.
Consider the code snippet below, which demonstrates the use of the in
operator:
begins1 := ['a', 'b', 'c', 'd'];s3 := 'd' in s1;writeln('s3: ', s3);s1 := ['a', 'b', 'c', 'd'];s3 := 'f' in s1;writeln('s3: ', s3);end.
Including an object x
in set A
means adding x
as an element of set A
. It is equivalent to the union of set A
with x
.
Excluding an object x
from set A
means removing x
as an element of set A
. It is equivalent to the difference of set A
and x
.
For example:
The set include operator in Pascal is denoted by the Include(set, ['element']
.
The set exclude operator in Pascal is denoted by the Exclude(set, ['element']
.
Consider the code snippet below, which demonstrates the use of the Include()
and Exclude()
operators:
begins1 := ['a', 'b', 'c', 'd'];Include(s1, 'e');write('s1: ');printSet(s1);s1 := ['a', 'b', 'c', 'd'];Exclude(s1, 'b');write('s1: ');printSet(s1);end.
Free Resources