Contents | Prev | Next

6.10 Set variables

Description

Set variables are used to store values of set types (i.e. combinations of values of the set's base type).

Example

The following simple example program shows a typical use of set variables to classify values. The program operates as follows:

First the set variable letters is initialized with the set of characters containing all lowercase and uppercase characters. Next the set variable digits is initialized with the set of characters containing all the digits. Next the user is prompted to enter a character, and this character is read in. Finally the character is classified by checking whether it is a member of letters, digits or neither. NOTE: Sets are often used like this as an alternative to using a big case statement.

program classify(input, output);
var
   letters, digits : set of char;
   c : char;
begin
   letters := ['a'..'z', 'A'..'Z'];
   digits := ['0'..'9'];
   write('Enter a character: ');
   read(c);
   if c in letters then
      writeln('You entered a letter')
   else if c in digits then
      writeln('You entered a digit')
   else
      writeln('You entered a symbol')
end.

Contents | Prev | Next