Sign in Join
Wiki copytemporary

Basic Pascal Tutorial/Chapter 5/Subranges/zh CN

English · Français · Български · 中文(中国) · 日本語

5B - 子界类型 (原作者: Tao Yue, 状态: 未更改)

如果我们定义一个变量i为integer类型,那么i的值在微型机系统的pascal中,使用2字节的定义表示法,取值范围为-32768~32767;而事实上,每个程序中所用的变量的值都有一个确定的范围;例如:人的年龄一般不超过150;一个班级的学生不超过100人;一年中的月数不超过12;一月中的天数不超过31,等等。

如果我们能在程序中对所用的变量的值域作具体规定的话,就便于检查出那些不合法的数据,这就能更好地保证程序运行的正确性;而且在一定程度上还会节省内存空间。   

pascal
????????? .. ?????????
text
其中最低值称为子界的下界,最高值称为子界的上界。
text
下界和上界必须是同一顺序类型(该类型称为子界类型的基类型),且上界的序号必须大于下界的序号。

如,你声明一周以及一周天数:

pascal
type
  DaysOfWeek = (Sunday, Monday, Tuesday, Wednesday,
                Thursday, Friday, Saturday);
  DaysOfWorkWeek = Monday..Friday;

你也可以使用字符或整数在子界类型中。

参考

算法与程序设计:PASCAL枚举、子界类型

上一页 目录 下一页

From the Free Pascal wiki, page "Basic Pascal Tutorial/Chapter 5/Subranges/zh CN", as it stood on 2022-08-20 after a change by Kai Burghardt, out of the wiki's dump of 2024-05-26. Copyright stays with the author who wrote it, according to the old wiki's Licensing page. If you have concerns about this material, message the admin.

Revision 3, last changed by WikiArchive .

Edited on wiki.freepascal.org by 5 people, 2013–2022

From the official wiki's backup of 26 May 2024: who edited this page there, and when. Edits made there after that date were not recorded, and the earlier versions are not shown here; the main page says why.

WhenWhoWhat changed
WangyouworldCreated page
Wangyouworld (minor)
Swen (minor)
FTurtle
TrevFixed syntax highlighting; deleted category included in page template (minor)
Kai BurghardtKai Burghardt moved page Subranges/zh CN to Basic Pascal Tutorial/Chapter 5/Subranges/zh CN: tidy up main name space: create subpage hierarchy for basic Pascal tutorial [cf. [[Special: PermaLink/14... (minor)
Kai Burghardtbypass redirects [cf. discussion]
Kai Burghardtbypass [previously missed] redirect [cf. discussion]
Kai Burghardtbypass language bar/categorization template redirect [cf. discussion] (minor)

Talk about this page

Nobody has opened a bench for this page yet. Sign in to open one.