Processing in Delphi
Processing in Delphi follows mathematical rules, like standard algebra. It includes
order of operations (BODMAS/PEMDAS), rules for expressions and calculations, and
handling operators.
1. Order of Operations (BODMAS/PEMDAS)
Delphi follows the standard mathematical precedence:
1. Brackets (Parentheses) → ()
2. Exponents (Not directly supported, use Power() function)
3. Multiplication and Division → * / div mod
4. Addition and Subtraction → + -
Example:
var
result: Integer;
begin
result := 10 + 2 * 5; // Multiplication happens first: 10 + (2 * 5) = 20
Writeln(result);
end.
To control the order, use parentheses:
result := (10 + 2) * 5; // (10 + 2) * 5 = 60
2. Arithmetic Operators in Delphi
Delphi supports standard arithmetic operators:
Operator Meaning Example
+ Addition x := 10 + 5;
- Subtraction y := 15 - 3;
* Multiplication z := 4 * 2;
/ Real Division (Float) w := 10 / 4; // 2.5
div Integer Division a := 10 div 4; // 2
mod Modulus (Remainder) b := 10 mod 4; // 2
Example: Integer and Real Division
var
a, b: Integer;
c: Real;
begin
a := 10 div 3; // Integer division (3)
b := 10 mod 3; // Remainder (1)
c := 10 / 3; // Real division (3.3333)
showmessage(a, ' ', b, ' ', c:0:2);
end.
Note: div only works with integers, while / produces a real number.
3. Relational Operators (Comparison)
Used in conditions (if statements, loops).
Operator Meaning Example
= Equal to if x = 5 then
<> Not equal to if x <> 5 then
< Less than if x < 10 then
> Greater than if x > 10 then
<= Less than or equal if x <= 10 then
>= Greater or equal if x >= 10 then
4. Logical Operators (Boolean)
Used in if, while, and repeat statements.
Operator Description Example
and Both conditions must be True if (x > 5) and (y < 10) then
or At least one condition is True if (x = 5) or (y = 10) then
not Negates condition if not (x = 5) then
5. Mathematical Functions
Delphi provides built-in math functions from the Math unit.
Function Description Example
Sqrt(x) Square root y := Sqrt(25); // 5
Abs(x) Absolute value z := Abs(-10); // 10
Round(x) Rounds to nearest whole number a := Round(4.6); // 5
Trunc(x) Removes decimal part b := Trunc(4.9); // 4
Power(x,y) Raises x to power y c := Power(2,3); // 8
Example: Using Math Functions
uses Math;
var
x, y: Real;
begin
x := Sqrt(16); // 4
y := Power(2, 3); // 8
Writeln(x:0:2, ' ', y:0:2);
end.
6. Example: Complex Calculation in Delphi
uses Math;
var
a, b, c: Integer;
result: Real;
begin
a := 10;
b := 3;
c := 2;
result := (a + b) * c / Power(2, c) - Sqrt(16);
Writeln('Result: ', result:0:2);
end.
Explanation:
1. (a + b) * c → (10 + 3) * 2 = 26
2. / Power(2, c) → 26 / 4 = 6.5
3. - Sqrt(16) → 6.5 - 4 = 2.5
7. Summary
Concept Description
Order of Operations () → * / div mod → + -
Arithmetic Operators +, -, *, /, div, mod
Comparison Operators =, <>, <, >, <=, >=
Logical Operators and, or, not
Math Functions Sqrt(), Power(), Round(), Abs()