Enumerated and interval data types

Enumerated and interval data types


Questions

 


Answers

1. What benefits of using of enumerated and interval data types in program?

Enumerated type is a limited ordered sequence of scalar constants, which constitute this type. The value of every constant is given by it’s name. The names of separated constants are separated from one another by commas.

The advantage of using this type is that the programmer brings together in one group all given values, respectively some signs. These values are the values of enumerated type. This in turn improves the readability of programs.

The value of enumerated type must be the ordinal type.

Interval or limited range data type is set from minimum to maximum value of the constants, which are separated by two dots ‘..’.


2. An example of describing and using of enumerated type in program (in the section “type“).

The enumerated type can be described into the sections of types definition (type) and variables definition (var).

An example of describing of enumerated type by using the “type” section:

type
  Rainbow = (RED, ORANGE, YELLOW, GREEN, LIGHT_BLUE, BLUE, VIOLET);

var
  r1,r2:Rainbow;

After such describing you can use the variables of enumerated type:

...

r1:=YELLOW;
r2:=r1;

...


3. What functions and operations can be applyed to the variables of enumerated type?

To the variables of enumerated type are allowed three basic functions:

Pred – determining the preceding item in the set;

Succ – determining the next item in the set;

Ord – determining the position (index) of element in enumerated type.

An example of using the functions Pred, Succ and Ord.

type
  Rainbow = (RED, ORANGE, YELLOW, GREEN, LIGHT_BLUE, BLUE, VIOLET);

var
  r1,r2:Rainbow;

begin
  ...

  r1 := GREEN;
  r2 := Pred(r1); // r2 = YELLOW
  pos := Ord(r1); // pos = 3
  pos := Ord(RED); // pos = 0

  ...
end;

As you can see, the indexes of enumerated type are begun from 0.

The variables and values of enumerated type can be used in the logical comparison operations. For example:

type
  Rainbow = (RED, ORANGE, YELLOW, GREEN, LIGHT_BLUE, BLUE, VIOLET);

var
  r1,r2:Rainbow;

begin

  ...

  r1 := GREEN;
  r2 := Pred(r1); // r2 = YELLOW

  ...

  if r2>r1 then
    Label1.Caption := 'r2 is more than r1'
  else
    Label1.Caption := 'r2 is less of equal than r1';

...

end;


4. An example of determining and using variables of enumerated type in the program (in the section “var“).

 

var
  r1,r2:(RED, ORANGE, YELLOW, GREEN, LIGHT_BLUE, BLUE, VIOLET);

After this describing, you can use the variables of enumerated type:

...

r1:=BLUE;

r2:=r1; // r2 = BLUE

...


5. What the standard data type of Pascal is compatible with the values of enumerated and interval data types?

The ordinal types are compatible with the values of enumerated and interval data types. These types include standard data types integer, boolean, char.

To the variables of ordinal types are applied functions Pred and Succ.


6. An example of defining and using the interval data type.

Interval data type can be described in two ways, which can be interconnected.

Way 1. In the section of definition of types by using the key word “type“.

type
  TYear = 1900..2000;
  TCharacter = 'A'..'Z';

var
  y,y2:TYear;
  ch,ch2:TCharacter;

begin

  ...

  y := 1905;   // correctly
  y2:=Succ(y); // y2 = 1906  
  y2:=y+10;    // y2 = 1915

  // compilation error
  // year := 1800;  

  ch := 'D';
  ch2:=Succ(ch); // ch2 = 'E'
  ch2:=Pred(ch); // ch2 = 'C'

  ...
end;

Way 2. The definition in the section “var“.

var
  ch, ch2: 'a'..'z';
  y:1900..2100;

begin

  ...

  ch := 'h';
  ch2 := Pred(ch); // ch2 = 'g'

  y := 1905;

  // error - incompatible types
  // ch2 := ch + 2;

  ...

end;