Grade 12 — Programming Recap

SQL queries & joins · 2D arrays · object-oriented programming · recursion.

Delphi · Paper 1

SQL

SELECT queries (run with ADOQuery → Open)

All / specific fields
SELECT * FROM tblStudents SELECT Name, Mark FROM tblStudents
Filter rows
SELECT * FROM tblStudents WHERE Mark >= 50 AND Grade = 12
Sort the result
ORDER BY Surname ASC ORDER BY Mark DESC
Partial text match
WHERE Name LIKE 'R%' -- starts with R WHERE Name LIKE '%an%' -- contains an
Aggregate functions
SELECT COUNT(*) FROM tblStudents SELECT AVG(Mark) FROM tblStudents SUM(Mark) MAX(Mark) MIN(Mark)
Group & filter groups
SELECT Grade, AVG(Mark) FROM tblStudents GROUP BY Grade HAVING AVG(Mark) > 60
Number range / unique values
WHERE Mark BETWEEN 40 AND 60 SELECT DISTINCT Grade FROM tblStudents
Order matters: SELECT → FROM → WHERE → GROUP BY → HAVING → ORDER BY. Wrap text values in single quotes: WHERE Name = 'Ana'.

Joining two tables

SELECT tblStudents.Name, tblClass.Teacher FROM tblStudents, tblClass WHERE tblStudents.ClassID = tblClass.ClassID
Always join on the matching key in the WHERE clause — leave it out and you get every combination (a Cartesian product).

Action queries & running SQL

Insert / update / delete
INSERT INTO tblStudents (Name, Mark) VALUES ('Ana', 85) UPDATE tblStudents SET Mark = 90 WHERE Name = 'Ana' DELETE FROM tblStudents WHERE Mark < 30
In Delphi code
qry.Close; qry.SQL.Clear; qry.SQL.Add('SELECT * FROM tblStudents'); qry.Open; // SELECT → Open qry.ExecSQL; // INSERT/UPDATE/DELETE → ExecSQL
Always use WHERE in UPDATE / DELETE — without it every record is changed.

2D Arrays

Declare & fill

Declare (rows × columns)
var grid : array[1..3,1..4] of Integer;
Assign one cell (row, col)
grid[2,3] := 99;
Fill with nested loops
for r := 1 to 3 do for c := 1 to 4 do grid[r,c] := 0;
Always: first index = row, second = column. Outer loop = rows, inner loop = columns.

Display & totals

Display row by row
for r := 1 to 3 do begin sLine := ''; for c := 1 to 4 do sLine := sLine + IntToStr(grid[r,c]) + #9; redOut.Lines.Add(sLine); end;
Total of one column
iSum := 0; for r := 1 to 3 do iSum := iSum + grid[r,2];

Object-Oriented Programming

Class structure

Define the class (type section)
type TStudent = class private fName : String; fMark : Integer; public constructor Create(sN: String; iM: Integer); function GetName: String; procedure SetMark(iM: Integer); function ToString: String; end;
Implement the methods
constructor TStudent.Create(sN: String; iM: Integer); begin fName := sN; fMark := iM; end; function TStudent.GetName: String; begin Result := fName; end;

Create & use an object

var objStud : TStudent; begin objStud := TStudent.Create('Ana', 80); objStud.SetMark(90); sName := objStud.GetName; objStud.Free; // free what you Create
Always Free an object you Create. Declare it at form level if it must survive between button clicks.

Access modifiers

privateInside the class only — fields go here
publicAnywhere — constructor & methods here
constructorCreate — sets up the object
field (f…)Data belonging to the object
accessor / getterFunction returning a private field
mutator / setterProcedure changing a private field
Encapsulation: fields are private; the outside world reads/writes them only through public methods.

Key terms

ClassBlueprint — fields + methods
ObjectAn instance made from a class
MethodProcedure/function in a class
InheritanceChild class extends a parent
inheritedCalls the parent's version
TLearner = class(TStudent) // inherits all of TStudent

Recursion

What it is

A recursive method calls itself on a smaller version of the problem until it reaches a base case that stops it.

Base caseThe stopping condition (no more calls)
Recursive caseCalls itself with a smaller value
Without a base case the method never stops — a stack overflow crash.

Factorial & Fibonacci

Factorial — 5! = 120
function Factorial(n: Integer): Integer; begin if n <= 1 then Result := 1 // base case else Result := n * Factorial(n-1); end;
Fibonacci term
if n <= 2 then Result := 1 else Result := Fib(n-1) + Fib(n-2);
Result := ... and the older FunctionName := ... style (e.g. Factorial := ...) both return a value — they're interchangeable, but don't mix them in the same function.