Delphi Programming/Arithmetic expression
An arithmetic expression returns a numerical value that can be used directly or stored in a numerical variable.
Addition operator "+"
[edit | edit source]The operator allowing the addition is the + operator. It allows to add a number or the result of an arithmetic expression to a number or an expression.
var
x, y, z: integer;
begin
x := 5; // Value of x is 5
y := 3; // Value of y is 3
z := x + y; // Z now has the value 8 (value of x plus y)
end.
Subtraction operator "-"
[edit | edit source]The operator allowing the subtraction is the - operator. It allows to subtract a number or the result of an arithmetic expression to a number or an expression.
var
x, y, z: integer;
begin
x := 5; // x = 5
y := 3; // y = 3
z = x - y; // Z = 2 (value of x minus y)
end.
Multiplication operator "*"
[edit | edit source]The operator allowing the multiplication is the * operator. It allows to multiply a number or the result of an arithmetic expression with a number or an expression.
var
x, y, z: integer;
begin
x := 5; // x = 5
y := 3; // y = 3
z := x * y; // Z = 15 (value of x times y)
end.
Division operator "/"
[edit | edit source]The operator allowing the division is the / operator. It allows to divide a number or the result of an arithmetic expression by a number or an expression. The result is a float.
var
x, y: integer;
z: real;
begin
x := 5; // x = 5
y := 3; // y = 3
z := x / y; // Z = 1.666 (value of x divided by y)
end;
Euclidean division operator "div"
[edit | edit source]The operator allowing the euclidean division is the div operator. It allows to divide a number or the result of an arithmetic expression by a number or an expression. The result is a rounded integer.
var
x, y, z: integer;
begin
x := 5; // x = 5
y := 3; // y = 3
z := x div y; // Z = 1 (value of x divided by y)
end;
Modulo operator "mod"
[edit | edit source]The operator allowing the modulo is the mod operator. It allows to get the remainder of a number or the result of an arithmetic expression divided by a number or an expression.
var
x, y, z: integer;
begin
x := 5; // x = 5
y := 3; // y = 3
z := x mod y; // Z = 2 (remainder of x divided by y)
end;
Multiple expression
[edit | edit source]You can nest expressions but don't forget the brackets.
var
w, x, y, z: integer;
begin
w := 10; // w = 10
x := 5; // x = 5
y := 3; // y = 3
z := (w - x) * y; // Y = 15 (w subtracted by x and then multiplied by y)
end;