Sign in Join
Wiki copytemporary

Basic Pascal Tutorial/Chapter 1/Assignment and Operations/zh CN

Sent here from Assignment and Operations/zh CN.

한국어 · Deutsch · English · Español · Français · Български · Русский · 中文(中国) · 日本語

1E - 赋值和运算符 (原作者: Tao Yue, 状态: 未更改)

一旦你声明了一个变量,你就可以往里面存储值。这就叫做赋值。

把一个值赋给一个变量的语法如下:

pascal
variable_name := expression;

注意,Pascal跟其它一些直接用等号作为赋值符号的语言不同,Pascal用冒号后面跟一个等号来表示赋值,这跟大多数计算机代数系统相似。

上面的expression(表达式)既可以是单独一个值:

pascal
some_real := 385.385837;

也可以是一个算式:

pascal
some_real := 37573.5 * 37593 + 385.8 / 367.1;

Pascal中的数学运算符有:

运算符 运算功能 操作数类型 结果类型
+ 加法或一元正号 real或integer real或integer
- 减法或一元负号 real或integer real或integer
* 乘法 real或integer real或integer
/ 实数除法 real或integer real
div 整除 integer integer
mod 求模(除法取余) integer integer

div和mod只能用于整数。/对实数和整数都可以用,但是结果都是real类型的。其他的运算符都可以同时用于实数和整数。当出现实数和整数的混合运算时,结果都是real类型的,不然结果可能会丢失部分数据。这也是为什么Pascal要分别为普通除法和整数除法使用不同的运算符。7 / 2 = 3.5 (real),但是7 div 2 = 3(作为余数,7 mod 2 = 1)。

每个变量都只能被赋予同一类型的值。因此,你不能把一个real类型的值赋给一个integer类型的变量。然而,某些特定类型的值可以转换为更高的数据类型。最常见的就是把一个integer类型的值赋给一个real类型的变量。假设有下面的变量定义:

pascal
var
  some_int : integer;
  some_real : real;

执行下面的语句:

pascal
some_int := 375;
some_real := some_int;

some_real中存储的值将会是375.0

把一种数据类型变为另外一种数据类型涉及到类型转换。现代Pascal编译器支持类似于C语言的显示类型转换,但语法稍微有些不同。不过类型转换通常用于低级别的操作或是面对对象编程,所以初学者不太需要用到。

在Pascal语言中,减号可以用作负号,同样地加号也可以用作正号,但由于正号是默认的,所以一般不用写。

不要把两个运算符挨着写:

pascal
some_real := 37.5 * -2;

你可能感觉直接这样乘以-2比较好,但是Pascal编译器就会搞不清楚到底该乘还是该减。你可以用圆括号来避免这种问题:

pascal
some_real := 37.5 * (-2);

计算机里的运算顺序和优先级跟数学中一样。乘法和除法(* / div mod)运算先于加法和减法(+ -),而括号总是最优先的。所以3.5*(2+3)的结果是17.5.。

Pascal中不能对布尔类型的值做标准算术运算。布尔类型自己有一套特殊的运算。同样,你也不能对字符类型做算术运算。

上一页 目录 下一页

From the Free Pascal wiki, page "Basic Pascal Tutorial/Chapter 1/Assignment and Operations/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 1, last changed by WikiArchive .

Edited on wiki.freepascal.org by 4 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
MemoTranslated this page into simplified Chinese.
FTurtle
TrevFixed syntax highlighting; deleted category already in page template (minor)
Kai BurghardtKai Burghardt moved page Assignment and Operations/zh CN to Basic Pascal Tutorial/Chapter 1/Assignment and Operations/zh CN: tidy up main name space: create subpage hierarchy for [[:Category: Basic Pascal Introduction/zh CN|basic Pascal tutoria... (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.