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
pascal_operators.htm
廣告
© . All rights reserved.