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!
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.
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.
|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.|