Pascal-arithmetic-operators

提供:Dev Guides
移動先:案内検索

パスカル-算術演算子

次の表は、Pascalでサポートされているすべての算術演算子を示しています。 変数 A が10を保持し、変数 B が20を保持すると仮定します-

Operator Description Example
+ Adds two operands A + B will give 30
- Subtracts second operand from the first A - B will give -10
* Multiplies both operands A* B will give 200
div Divides numerator by denominator B div A will give 2
mod Modulus Operator AND remainder of after an integer division B mod A will give 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