Operators & Functions Grade 10
Operators perform calculations. Delphi also has a rich library of built-in functions for maths, string handling, and more.
Arithmetic Operators
| Operator | Symbol | Description | Example | Result |
|---|---|---|---|---|
| Addition | + | Add values | 5 + 3 | 8 |
| Subtraction | - | Subtract | 10 - 4 | 6 |
| Multiplication | * | Multiply | 4 * 3 | 12 |
| Division | / | Divide (returns Real) | 7 / 2 | 3.5 |
| Integer Division | DIV | Whole part only (no remainder) | 7 DIV 2 | 3 |
| Modulus | MOD | Remainder only | 7 MOD 2 | 1 |
MOD and DIV Examples
| Calculation | Working | MOD result | DIV result |
|---|---|---|---|
5 MOD/DIV 2 | 5 ÷ 2 = 2 remainder 1 | 1 | 2 |
25 MOD/DIV 5 | 25 ÷ 5 = 5 remainder 0 | 0 | 5 |
17 MOD/DIV 3 | 17 ÷ 3 = 5 remainder 2 | 2 | 5 |
Common Uses of MOD
Even/Odd check: n MOD 2 = 0 → even
Factor check: b MOD a = 0 → a is a factor of b
Last digit: n MOD 10 → last digit of n
Operator Precedence (Order of Operations)
| Priority | Operators |
|---|---|
| 1st (highest) | Brackets ( ) |
| 2nd | * / DIV MOD (left to right) |
| 3rd (lowest) | + - (left to right) |
Example
result := 2 + 3 * 4; // = 14 (3*4 first)
result := (2 + 3) * 4; // = 20 (brackets first)
result := 10 DIV 2 + 3; // = 8 (DIV first)Mathematical Functions
Add Math to your uses section to access power, floor, ceil etc.
| Function | Description | Example | Result |
|---|---|---|---|
sqr(x) | x squared (x²) | sqr(4) | 16 |
sqrt(x) | Square root | sqrt(16) | 4.0 |
power(x,n) | x to the power of n | power(5,3) | 125 |
round(x) | Round to nearest integer | round(5.68) | 6 |
trunc(x) | Remove decimal (truncate) | trunc(5.68) | 5 |
floor(x) | Round DOWN | floor(3.76) | 3 |
ceil(x) | Round UP | ceil(3.76) | 4 |
abs(x) | Absolute value (remove negative) | abs(-12) | 12 |
frac(x) | Fractional part only | frac(12.75) | 0.75 |
pi | Value of π | pi | 3.14159… |
random(n) | Random number from 0 to n-1 | random(10) | 0–9 |
RoundTo(x,-n) | Round to n decimal places | RoundTo(12.3456,-2) | 12.35 |
Inc and Dec
| Procedure | Effect | Example | Result |
|---|---|---|---|
Inc(x) | Increment by 1 | Inc(5) | 6 |
Inc(x, n) | Increment by n | Inc(5, 3) | 8 |
Dec(x) | Decrement by 1 | Dec(5) | 4 |
Dec(x, n) | Decrement by n | Dec(6, 4) | 2 |
Ord and Chr
Ord and Chr convert between characters and their ASCII numeric codes.
| Function | Description | Example | Result |
|---|---|---|---|
Ord(c) | Character → ASCII integer | Ord('A') | 65 |
Chr(n) | ASCII integer → Character | Chr(65) | 'A' |
Caesar Cipher Example
Delphi — Shift characters by a number
var
sInput, sOutput: string;
i, iShift: Integer;
begin
sInput := InputBox('Text', 'Enter text:', 'ABC');
iShift := StrToInt(InputBox('Shift', 'Enter shift (1-25):', '3'));
sOutput := '';
for i := 1 to Length(sInput) do
sOutput := sOutput + Chr(Ord(sInput[i]) + iShift);
ShowMessage('Shifted: ' + sOutput); // ABC → DEF
end;Upcase and Trim
| Function | Description | Example | Result |
|---|---|---|---|
UpCase(c) | Convert single char to uppercase | UpCase('a') | 'A' |
Trim(s) | Remove spaces from both ends | Trim(' Hello ') | 'Hello' |
TrimLeft(s) | Remove leading spaces | TrimLeft(' Hi') | 'Hi' |
TrimRight(s) | Remove trailing spaces | TrimRight('Hi ') | 'Hi' |