Basic Pascal Tutorial/Chapter 1/Constants/zh CN
한국어 · Deutsch · English · Français · Italiano · Български · Русский · 中文(中国) · 日本語
1C - 常量 (原作者: Tao Yue, 状态: 未更改)
常量通过标识符来引用,它只能在程序开始的时候被初始化为某个确定的值。常量中存储的值是无法更改的。
常量只能在程序的常量说明部分定义:
const
Identifier1 = value;
Identifier2 = value;
Identifier3 = value;例如,我们来定义一些各种类型的常量:字符串类型,字符类型,整数类型,实数类型和布尔类型。这些数据类型在下一页会解释。
const
Name = 'Tao Yue';
FirstLetter = 'a';
Year = 1997;
pi = 3.1415926535897932;
UsingNCSAMosaic = TRUE;注意,在Pascal中字符是用单引号(')括起来的!这跟那些允许使用双引号或Heredoc符号的新语言不同。标准Pascal并不允许使用双引号来标记字符或字符串。
如果你要定义一个在程序中到处都会用到,而且以后可能还需要更改的值,常量就非常有用。需要更改值的时候,你只需要在常量的定义处修改就行了,不用把程序中所有引用都一一更改。
强类型常量会强制成为指定数据类型的常量。例如:
const
a : real = 12;这将会产生实数值12.0的标识符,而不是整数值12的标识符。
| 上一页 | 目录 | 下一页 |
From the Free Pascal wiki, page "Basic Pascal Tutorial/Chapter 1/Constants/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.