Pascal - 算术运算符
下表显示了 Pascal 支持的所有算术运算符。 假设变量 A 为 10,变量 B 为 20,则 −
| 运算符 | 描述 | 示例 |
|---|---|---|
| + | 将两个操作数相加 | A + B 给 30 |
| - | 从第一个操作数中减去第二个操作数 | A - B 将给出 -10 |
| * | 将两个操作数相乘 | A * B 给 200 |
| div | 分子除以分母 | B div A 将给出 2 |
| mod | 模数运算符与整数除法后的余数 | B mod A 将给出 0 |
下面的例子说明了算术运算符 −
program calculator;
var
a,b,c : integer;
d: real;
begin
a:=21;
b:=10;
c := a + b;
writeln(' Line 1 - Value of c is ', c );
c := a - b;
writeln('Line 2 - Value of c is ', c );
c := a * b;
writeln('Line 3 - Value of c is ', c );
d := a / b;
writeln('Line 4 - Value of d is ', d:3:2 );
c := a mod b;
writeln('Line 5 - Value of c is ' , c );
c := a div b;
writeln('Line 6 - Value of c is ', c );
end.
请注意,Pascal 是一种强类型编程语言,因此如果您尝试将除法结果存储在整数类型变量中,则会出现错误。 当上面的代码被编译并执行时,会产生以下结果:
Line 1 - Value of c is 31 Line 2 - Value of c is 11 Line 3 - Value of c is 210 Line 4 - Value of d is 2.10 Line 5 - Value of c is 1 Line 6 - Value of c is 2

