Subrange Types
Enumerated Types

Subrange Types

A subrange type defines a subset of the values of a particular type. By using subrange types, you can easily detect errors occuring due to unreasonable values of a variable which shouldn't take values outside a given boundary. Subrange types can be defined of type character, integer, in no case real!




  • Any operator which may be used with a variable of a particular type may also be used with its subrange type.
  • The range of values of a variable can represent may be specified when a variable is declared - but not when a parameter or a function is declared.
  • Two variable are of the same type only if they are declared as the same type. So if you pass a variable parameter, be sure to pass a variable of the same type as the parameter.
  • Variables are compatible, if they have the same underlying type. A parameter passed to a value parameter must have a compatible type with the value parameter.
  • A function can only return an ordinal data type, no subrange type !
  • Be sure to have the value of the lowerbound smaller than the upperbound, that is ord(lowerbound)<ord(upperbound).
  • A subrange type can also be used in case statements.

Positive and negative examples:

positive = 1..32500; +
gradepoints = 0.00 .. 4.00; -
numbers = integer; +
scientific = real; +
alphabet = 'Z'..'A'; -
range = '1'..9; -
count = -15..15; +

Enumerated Types

One of the most powerful features of Pascal is that it allows the definition of new types. Need may arise to use a type which could not be represented by another type or it may be much more meaningful to use values with more meaning. For example a program which uses the days of a week might make use of this feature. A possibility is to use integer values for each day, like 1 for Monday, 2 for Tuesday,.. Another possibility is to define a new type especially for weekdays, like

Weekdays = (Monday, Tuesday, Wednesday, Thursday, Friday, Saturday, Sunday);

This new type can now be used to define new variables.


Enumerated Type Operators

Enumerated types are considered ordinal types. So they also have a special order among themselves. e.g. you can test whether one day comes before the other by applying if (day1<day2) ... where day1 and day2 are variables of type Weekdays and assigned days. If for example day1 was assigned Monday and day2 was assigned Tuesday before, this boolean expression returns true, as Monday < Tuesday.

Functions also applicable to enumerated types are ord, succ and pred defines in the section standard functions. Examples illustrating their use could be as follows :


Reading and Writing Enumerated Types

Read and Write can not read/write enumerated types. For this reason, it would be helpful for you to write your own procedure to display values of enumerated types. A solution for this could be to use a case statement.

case today of
Monday: writeln('Monday');
Tuesday: writeln('Tuesday');
A First Look A First Look at Pascal Basic Input/Output Identifiers & Types
Constants Standard Functions If..Then Statement Case Statement
Repetitions Procedures and Functions Enumerated /Subrange Types Sets
I/O, Text Files Arrays Records Recursion
Pointer How to Debug Previous Exam Questions Advanced
ASCII Code Table Homepage University Homepage Computer Engineering Dept.