Data Types & Variables Grade 10
Every value in Delphi has a data type. Variables are the containers that hold these values while your program runs. Constants hold values that never change.
Data Types
| Data Type | What it stores | Examples | Quotes? |
|---|---|---|---|
Integer | Whole numbers (positive & negative, no decimals) | 3, -15, 1250 | No |
Real | Numbers with decimals (floating point) | 3.14, -13.24, 125.89 | No |
String | One or more characters — text | 'Banana', '23.45', '@!jkl' | Single quotes '…' |
Char | A single character only | 'S', '!', 'a' | Single quotes '…' |
Boolean | Only True or False | True, False | No |
Variables
A variable is a named storage location that holds a value during program execution. Think of it as a labelled box — you can put something in, use it, and swap it out.
Variables must be: Declared → Populated → Used.
Naming Convention — Prefixes
| Prefix | Data Type | Example |
|---|---|---|
i | Integer | iAge |
r | Real | rPrice |
s | String | sName |
c | Char | cGrade |
b | Boolean | bFound |
Declaring Variables
Declared in the var section using: variableName : DataType;
Local Declaration (inside a procedure)
procedure TForm1.btnClickClick(Sender: TObject);
var
sName: string;
iNumber: integer;
begin
// code here
end;
Global Declaration (available to all procedures)
var
Form1: TForm1;
sName: string; // declared here, below existing var block
iNumber: integer;
Populating (Assigning) Variables
Use the assignment operator := to put a value into a variable.
sName := 'Jannie';
sName := edtName.Text; // from a text box
iNumber := 15;
iNumber := sedNum.Value; // from a spin edit
bFlag := True;
Initialising Variables
Set a starting value before using a variable, especially counters and totals — otherwise Delphi may assign a random garbage value.
i := 0;
iTotal := 0;
sReverse := '';
cLetter := '';
Using Variables
lblFullName.Caption := sName + ' ' + sSurname;
memOut.Lines.Add(sName + ' ' + sSurname);
iAnswer := iNum1 + iNum2;
memOut.Lines.Add(IntToStr(iNum1) + ' + ' + IntToStr(iNum2)
+ ' = ' + IntToStr(iAnswer));
Constants
A constant holds a value that is known before the program runs and cannot change while it runs. Use const instead of var. No data type is needed, and use = not :=.
const
VAT = 0.15;
MAX_ITEMS = 100;
// Using a constant — exactly like a variable
rVatAmount := rCostPrice * VAT;
rTotal := rCostPrice + rVatAmount;
Type Conversion
Components like TEdit only hold strings. You must convert to the right type before using in calculations.
| Function | Converts | Example |
|---|---|---|
StrToInt() | String → Integer | iAge := StrToInt(edtAge.Text); |
StrToFloat() | String → Real | rPrice := StrToFloat(edtPrice.Text); |
IntToStr() | Integer → String | lblOut.Caption := IntToStr(iTotal); |
FloatToStr() | Real → String | lblOut.Caption := FloatToStr(rAverage); |
Val procedure — converting safelyStrToInt crashes (runtime error) if the text isn't a valid number. The Val procedure converts a string to a number without crashing: it gives back the converted value AND an error code that tells you whether the conversion worked (0 = success, otherwise the position of the first bad character).
var
iValue, iCode : Integer;
begin
Val(edtAge.Text, iValue, iCode);
if iCode = 0 then
ShowMessage('Valid number: ' + IntToStr(iValue))
else
ShowMessage('Not a valid number!'); // iCode = position of the bad character
end;Formatting Real Numbers
// FloatToStrF(value, format, totalDigits, decimals)
lblPrice.Caption := FloatToStrF(rPrice, ffCurrency, 4, 2); // → R24.50
lblPrice.Caption := FloatToStrF(rPrice, ffFixed, 4, 2); // → 24.50
Types of Errors
| Error Type | When it occurs | Example |
|---|---|---|
| Syntax Error | Code is typed incorrectly — won't compile | Missing semicolon, missing end |
| Runtime Error | Crashes while the program is running | Dividing by zero |
| Logical Error | Runs, but gives wrong answer | Calculating average before total, infinite loop |