Operators & Functions Grade 10

Operators perform calculations. Delphi also has a rich library of built-in functions for maths, string handling, and more.

T1Term 1 · Operators, Precedence & Functions

Arithmetic Operators

OperatorSymbolDescriptionExampleResult
Addition+Add values5 + 38
Subtraction-Subtract10 - 46
Multiplication*Multiply4 * 312
Division/Divide (returns Real)7 / 23.5
Integer DivisionDIVWhole part only (no remainder)7 DIV 23
ModulusMODRemainder only7 MOD 21

MOD and DIV Examples

CalculationWorkingMOD resultDIV result
5 MOD/DIV 25 ÷ 2 = 2 remainder 112
25 MOD/DIV 525 ÷ 5 = 5 remainder 005
17 MOD/DIV 317 ÷ 3 = 5 remainder 225
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

Worked Example — Isolating the Digits of a Number

A favourite exam task: pull a number apart digit by digit. The trick is the pair MOD 10 (gives the last digit) and DIV 10 (chops the last digit off). Repeat in a loop until nothing is left. Here we add up the digits, e.g. 1234 → 1+2+3+4 = 10.

Delphi — sum the digits
var
  iNum, iDigit, iSum : Integer;
begin
  iNum := StrToInt(edtNum.Text);   // e.g. 1234
  iSum := 0;
  while iNum > 0 do
  begin
    iDigit := iNum mod 10;   // MOD 10 grabs the LAST digit  (1234 -> 4)
    iSum   := iSum + iDigit;
    iNum   := iNum div 10;   // DIV 10 chops it off          (1234 -> 123)
  end;
  lblSum.Caption := 'Digit sum: ' + IntToStr(iSum);
end;
Same trick, many questions

The MOD 10 / DIV 10 loop is the key to counting digits, reversing a number, checking for a palindrome, or building the Luhn check digit of an ID. Swap what you do with iDigit and you have solved a whole family of questions.

Operator Precedence (Order of Operations)

PriorityOperators
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.

FunctionDescriptionExampleResult
sqr(x)x squared (x²)sqr(4)16
sqrt(x)Square rootsqrt(16)4.0
power(x,n)x to the power of npower(5,3)125
round(x)Round to nearest integerround(5.68)6
trunc(x)Remove decimal (truncate)trunc(5.68)5
floor(x)Round DOWNfloor(3.76)3
ceil(x)Round UPceil(3.76)4
abs(x)Absolute value (remove negative)abs(-12)12
frac(x)Fractional part onlyfrac(12.75)0.75
piValue of πpi3.14159…
random(n)Random number from 0 to n-1random(10)0–9
RoundTo(x,-n)Round to n decimal placesRoundTo(12.3456,-2)12.35

Inc and Dec

ProcedureEffectExampleResult
Inc(x)Increment by 1Inc(5)6
Inc(x, n)Increment by nInc(5, 3)8
Dec(x)Decrement by 1Dec(5)4
Dec(x, n)Decrement by nDec(6, 4)2

Upcase and Trim

FunctionDescriptionExampleResult
UpCase(c)Convert single char to uppercaseUpCase('a')'A'
Trim(s)Remove spaces from both endsTrim(' Hello ')'Hello'
TrimLeft(s)Remove leading spacesTrimLeft(' Hi')'Hi'
TrimRight(s)Remove trailing spacesTrimRight('Hi ')'Hi'
T3Term 3 · Ord & Chr Functions

Ord and Chr

Ord and Chr convert between characters and their ASCII numeric codes.

FunctionDescriptionExampleResult
Ord(c)Character → ASCII integerOrd('A')65
Chr(n)ASCII integer → CharacterChr(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
    // wrap back to 'A' after 'Z' using MOD 26
    sOutput := sOutput + Chr(Ord('A') + (Ord(sInput[i]) - Ord('A') + iShift) mod 26);
  ShowMessage('Shifted: ' + sOutput);  // ABC → DEF, XYZ → ABC
end;
Why the MOD 26?

Without it, shifting a letter near the end of the alphabet (like 'X', 'Y' or 'Z') pushes past 'Z' into punctuation characters instead of wrapping back to 'A'. Subtracting Ord('A') first converts the letter to a 0–25 range, MOD 26 wraps it, and adding Ord('A') back converts it to a real letter again.