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! Syntax examples:
Positive and negative examples:
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. Syntax
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.
|