Register for EarthWeb's Million Dollar Sweepstakes!
home account info subscribe login search My ITKnowledge FAQ/help site map contact us


 
Brief Full
 Advanced
      Search
 Search Tips
To access the contents, click the chapter and section titles.

Learn Pascal in a Three Days (2nd Ed.)
(Publisher: Wordware Publishing, Inc.)
Author(s):
ISBN: 1556225679
Publication Date: 07/01/97

Bookmark It

Search this book:
 
Previous Table of Contents Next


Intersection

The intersection of two sets results in a set whose members are the elements common to both sets. For example, the statement:

    MLL:= [C,CPP,Cobol] * [Basic,Fortran,C,CPP]

results in the set MLL, which contains “C” and “CPP.”

Difference

The difference of two sets “S1” and “S2” is the set whose members are in “S1” but not in “S2.” For example, the statement:

    HLL:= ProgCodes - LLL;

results in the set HLL, which contains all the elements of the set “ProgCodes” except “Assembly.”

Drill 8-1

Evaluate the following expressions:

(See the answers in the file DRL8-1.TXT on the distribution disk.)

1.  ['A','B','C','D'] + ['E','F']
2.  ['A','B','C','D'] + ['B','C','E','F']
3.  [1,3,7] + []
4.  ['A','D','F'] * ['O','F']
5.  [1,2,3,4] * [5,6,7]
6.  [1,2,3,4] - [5,6,7]
7.  [5,6,7] - []
8.  [Able, Baker, Charlie] - [Able, Charlie]

You can make use of the union operation to construct a new set (read a set) by reading one element at a time and adding it to the set, for example:

    Read(NewElement);
    Set1:= Set1 + [NewElement];

You can also make use of the difference operation to display the elements of a set. This is done by testing the membership of a variable of the same base type as that of the set. If the element is a set member, it is displayed, subtracted from the set, and replaced by its successor. This continues until the set is empty.

Relational Operators

The relational operators =, >=, <=, and <> can be used with sets of compatible types.

The meanings of the set relational operators are indicated in Table 8-1 by comparing two sets “S1” and “S2.” The table contains TRUE expressions as examples of each operation.

The operators > and < are not mentioned in the table as they may not be used with sets.

Table 8-1 Sets Relational Operators

Expression Meaning Example

S1 = S2 Both S1 and S2 contain the same elements. [1,0] = [1,0]
S1 <> S2 S1 and S2 do not contain the same elements. [1,0] <> [1,4]
S1 >= S2 All elements of S2 are in S1. [1,2,3,4] >= [1,2]
[1,2,3] >= [1,2,3]
S1 <= S2 All elements of S1 are in S2. [] <= [1,2,3]
[1,2,3] <= [1,2,3]

The relative precedence of Pascal operators (including the new operator “IN ”) is shown in Table 8-2. Notice that the set operators (+, -, *) use the same symbols as the arithmetic operators. Also, the relational operators are used with either simple data types or sets.>

Table 8-2 Precedence of Pascal Operators

Operator Precedence

NOT Priority 1 (highest)
* / DIV MOD AND Priority 2
+ - OR (XOR in Turbo Pascal) Priority 3
> < >= <= <> IN { Priority 4 (lowest)

You may combine relational expressions using the boolean operators AND, OR, and NOT, but you must watch the precedence of operators, for example:

    IF (Ch IN Small) AND (Ch IN Capital) THEN...

The parentheses are necessary in this expression because the IN operator has a lower precedence than the AND operator.

Drill 8-2

Write a program to test the expressions in Table 8-1.


Previous Table of Contents Next


Products |  Contact Us |  About Us |  Privacy  |  Ad Info  |  Home

Use of this site is subject to certain Terms & Conditions, Copyright © 1996-2000 EarthWeb Inc.
All rights reserved. Reproduction whole or in part in any form or medium without express written permission of EarthWeb is prohibited. Read EarthWeb's privacy statement.