|
To access the contents, click the chapter and section titles.
Learn Pascal in a Three Days (2nd Ed.)
SubrangesThe subrange, another user-defined ordinal type, helps to eliminate out-of-range data. For example, instead of using the INTEGER type to represent the month numbers, you can declare the variable Month as a subrange like this: VAR Month: 1..12; As such, any value outside the range 1 to 12 will be considered an error either in compilation or at runtime. In other words, you cannot, after this declaration, write a statement like this in your program: Month:= 13; ---> illegal statement Also, if a user responds to an input statement by entering an out-of-range number, the program will issue the proper error message, though with some compilers you have to set a switch to make the compiler detect out-of-range errors. The type used to represent month values in this example is INTEGER. It is called the base type of the subrange. You may use any ordinal type as the base type. For example, you can declare the uppercase letters as a subrange using the base type CHAR as follows: VAR Uppercase: 'A'..'Z' In this case, only the uppercase letters will be permitted as data for the subrange Uppercase. The following example demonstrates the use of a subrange to represent months, followed by a CASE statement to classify months as seasons. The program prompts you to enter the month number, and displays the season this month belongs to. { -------------------- figure 5-2 -------------------- } PROGRAM Subrange1(INPUT,OUTPUT); VAR MonthNumber:1..12; BEGIN WRITE('Please enter the number of the month: '); READLN(MonthNumber); CASE MonthNumber OF 12, 1,2 :WRITELN('This is wintertime.'); 3, 4, 5 :WRITELN('This is springtime.'); 6, 7, 8 :WRITELN('This is summertime.'); 9, 10, 11:WRITELN('This is autumn.') END END. The following are two sample runs. The second one gave a runtime error message because the number 14 was entered as a month number: Run 1: Please enter the number of the month:2 This is wintertime. Run 2: Please enter the number of the month: 14 Runtime error 201 at 0000:00BE. The subrange, in general, can be a subset of any previously defined sequence (of the ordinal type). So, if the enumeration Day has already been defined in your program, you may then define a subrange like this: VAR WorkingDay: Monday..Friday; This is valid because the words Monday and Friday are already known to the compiler. There are some restrictions on using enumerations and subranges:
5-2 The TYPE SectionThe enumerations and subranges are usually associated with the TYPE statement, which is used to declare new user-defined types or to rename predefined types. The TYPE statement comes in the TYPE section of the declaration part. It takes the form: TYPE type-name = type-definition; where type-name is the type identifier, and type-definition is a predefined type or new type definition. Renaming TYPESIt is possible to rename any data type, even the simple types such as INTEGER, as in this example: TYPE Day = INTEGER; In this declaration the type INTEGER is given a new name (Day). Thus, in the VAR section, you can declare some other variables of the type Day like this: VAR Holiday, Yesterday, Tomorrow: Day; The type Day is actually the type INTEGER, but given another name (a synonym). In your program, you may use either one of the two names (INTEGER or Day) because the type INTEGER is still recognized by the compiler. This is not, however, the proper use of the TYPE statement. It is meant to be used for naming types such as enumerations and subranges. Naming User-Defined TypesInstead of declaring enumerations and subranges in the VAR section, it would be better to declare them as types. Look at these declarations: TYPE Day = (Monday,Tuesday,Wednesday,Thursday,Friday,Saturday,Sunday); WorkingDay = Monday..Friday; Here, two new types are declared: the enumerated type Day, and the subrange WorkingDay. Notice that the subrange is defined as a subset of the enumeration Day. Needless to say, the enumeration declaration must come first in this case. You can use these new types in the VAR section to declare variables in the same way you use the predefined types of the language. Thus: VAR Today, Yesterday, Tomorrow, Holiday:Day; DayOff:WorkingDay; The use of the TYPE statement saves you the effort of writing long declarations for the enumeration variables Today, Yesterday, Tomorrow, and Holiday. They are all simply of the type Day. Now in your program you may write assignment statements like the following: Holiday:= Friday; DayOff:= Tuesday; Tomorrow:= Sunday; In order to see the values contained in your variables, use an output statement such as: WRITELN(ORD(Holiday), ', ',ORD(DayOff),', ', ORD(Tomorrow)); In this case, the statement will give you the values 4, 1, and 6 respectively. In standard Pascal the TYPE section should come in the following sequence relative to the other sections:
In Turbo Pascal, as mentioned before, the order is not important, but the TYPE section should still precede the VAR section because it contains the definitions of the user-defined types.
|
Products | Contact Us | About Us | Privacy | Ad Info | Home
Use of this site is subject to certain Terms & Conditions, Copyright © 1996-2000 EarthWeb Inc. All rights reserved. Reproduction whole or in part in any form or medium without express written permission of EarthWeb is prohibited. Read EarthWeb's privacy statement. |