Trusted answers to developer questions
Trusted Answers to Developer Questions

Related Tags

pascal

What are set operators in Pascal?

Kainat Asif

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.

Example

Consider the code snippet below, which demonstrates how a set is declared in Pascal:

program SetDeclaration;
     
type
  charSet = set of Char;

var
  s1: charSet;
  ch: Char;

  function printSet(s1: charSet): integer;
  var
  ch: Char;

  begin
    
    write('[');
    for ch in s1 do
    begin
        write(ch);
        write(', ')
    end;
    writeln(']');
    printSet := 0;
  end;

begin
  s1 := ['a', 'b', 'c'];

  //printing s1
  write('s1: ');
  printSet(s1);
end.

Explanation

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

Set operators are used to perform operations on sets. The set operators in Pascal are as follows:

1. Union

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.

Example

Consider the code snippet below, which demonstrates the use of the + operator:

program SetOperator;
     
type
  charSet = set of Char;

var
  s1: charSet;
  s2: charSet;
  s3: charSet;
  ch: Char;

  function printSet(s1: charSet): integer;
  var
  ch: Char;

  begin
    
    write('[');
    for ch in s1 do
    begin
        write(ch);
        write(', ')
    end;
    writeln(']');
    printSet := 0;
  end;

begin
  s1 := ['a', 'b', 'c'];
  s2 := ['c', 'd', 'e', 'f'];
  s3 := s1 + s2;

  //printing s3
  write('s3: ');
  printSet(s3);
end.

2. Intersection

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.

Example

Consider the code snippet below, which demonstrates the use of the * operator:

begin
   s1 := ['a', 'b', 'c', 'd'];
   s2 := ['c', 'd', 'e', 'f'];
   s3 := s1 * s2;

    //printing s3
    write('s3: ');
    printSet(s3);
end.

3. Difference

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.

Example

Consider the code snippet below, which demonstrates the use of the - operator:

begin
   s1 := ['a', 'b', 'c'];
   s2 := ['c', 'd', 'e', 'f'];
   s3 := s1 - s2;

    //printing s3
    write('s3: ');
    printSet(s3);
end.

4. Symmetric difference

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.

Example

Consider the code snippet below, which demonstrates the use of the >< operator:

begin
   s1 := ['a', 'b', 'c', 'd'];
   s2 := ['c', 'd', 'e', 'f'];
   s3 := s1 >< s2;

    //printing s3
    write('s3: ');
    printSet(s3);
end.

5. Equality

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.

Example

Consider the code snippet below, which demonstrates the use of the = operator:

begin
   s1 := ['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.

6. Non-equality

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.

Example

Consider the code snippet below, which demonstrates the use of the <> operator:

begin
   s1 := ['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.

7. Subset

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.

Example

Consider the code snippet below, which demonstrates the use of the <= operator:

begin
   s1 := ['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.

8. Membership

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.

Example

Consider the code snippet below, which demonstrates the use of the in operator:

begin
   s1 := ['a', 'b', 'c', 'd'];
   s3 := 'd' in s1;
   writeln('s3: ', s3);

   s1 := ['a', 'b', 'c', 'd'];
   s3 := 'f' in s1;
   writeln('s3: ', s3);
end.

9. Include and exclude

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'].

Example

Consider the code snippet below, which demonstrates the use of the Include() and Exclude() operators:

begin
   s1 := ['a', 'b', 'c', 'd'];
   Include(s1, 'e');
   write('s1: ');
   printSet(s1);

   s1 := ['a', 'b', 'c', 'd'];
   Exclude(s1, 'b');
   write('s1: ');
   printSet(s1);
end.

RELATED TAGS

pascal

CONTRIBUTOR

Kainat Asif
Copyright ©2022 Educative, Inc. All rights reserved
RELATED COURSES

View all Courses

Keep Exploring