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.

T1Term 1 · Data Types, Variables & Constants

Data Types

Data TypeWhat it storesExamplesQuotes?
IntegerWhole numbers (positive & negative, no decimals)3, -15, 1250No
RealNumbers with decimals (floating point)3.14, -13.24, 125.89No
StringOne or more characters — text'Banana', '23.45', '@!jkl'Single quotes '…'
CharA single character only'S', '!', 'a'Single quotes '…'
BooleanOnly True or FalseTrue, FalseNo
Integer and Real Values Mix — But Not Both Ways

Delphi lets you go one way but not the other: a whole-number result can be poured into a Real variable without any fuss, but a Real value can never be poured into an Integer variable — there is no automatic way to throw away a decimal part. Worth remembering too: the / operator always hands back a Real answer, even when both of the numbers you divide happen to be whole numbers.

Delphi
rTotalStock := iBoxesIn + iBoxesReturned;   // fine — a whole number fits happily into a Real
iBoxesIn    := rTotalStock * 2;             // rejected — a Real value won't fit into an Integer

Variables

A variable is a named storage location that holds a value during program execution. Think of it as a labelled cup — it holds one thing at a time, but you can empty it and refill it whenever you like.

Variables must be: DeclaredPopulatedUsed.

ANALOGY — a variable is like a cup

A variable holds one value at a time, but the value can change. Your cup might hold coffee now, tea after break and hot chocolate tonight — same cup, different contents. In the same way iScore could be 5 now and 12 later.

The data type is the kind of container, and it decides what may go inside — just as a cup is made for liquids and a plate is made for food. Each type is its own kind of container:

  • Integer — holds whole numbers, e.g. 3, 15, −7
  • Real — holds numbers with decimals, e.g. 3.14
  • String — holds text, e.g. 'Banana'
  • Boolean — holds only True or False

And just as you would not serve soup on a plate, you cannot store the word 'Banana' in an Integer — the value has to match the type of container.

Rules for Variable Names

Delphi won't accept just any piece of text as a variable name — a handful of rules apply every time you choose one:

Multi-word Names

When a single word isn't descriptive enough, run several together and capitalise each new one instead of using spaces or an unbroken run of lowercase letters — sFirstName and rTotalCost read far more clearly than sfirstname or rtotalcost.

Naming Convention — Prefixes

PrefixData TypeExample
iIntegeriAge
rRealrPrice
sStringsName
cCharcGrade
bBooleanbFound

Declaring Variables

Declaring a variable means creating a named container that we can populate later, in the var section, using the format: variableName : DataType;

DECLARATION OF A VARIABLE Prefix to indicate datatype Semicolon to end statement s Name : string ; Variable name Colon instead of assignment sign (:=) Datatype of variable

Local Declaration (inside a procedure)

Delphi — Local
procedure TForm1.btnClickClick(Sender: TObject);
var
  sName:   string;
  iNumber: integer;
begin
  // code here
end;

Global Declaration (available to all procedures)

Delphi — Global
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 that has already been declared.

POPULATING (ASSIGNING) A VARIABLE Variable being populated Semicolon to end statement sName := 'Jannie' ; Assignment operator — "becomes equal to" Value being stored — quotes mean it's a String
Delphi
sName   := 'Jannie';
sName   := edtName.Text;        // from a text box
iNumber := 15;
iNumber := sedNum.Value;         // from a spin edit
bFlag   := True;
ANALOGY — why we assign to the LEFT

The := operator always works right → left: it takes the value on the right and stores it in the variable on the left.

Think of pouring coffee. You put the coffee (the value) into the cup (the variable) — you would never put the cup into the coffee! So the container always goes on the left.

In sName := 'Jannie'; the coffee is 'Jannie' (on the right) and the cup is sName (on the left). Read it backwards: "take 'Jannie' and pour it into sName".

It works the same with a calculation: iTotal := iPrice * iQty; — work out the answer (the coffee) on the right first, then pour it into iTotal (the cup) on the left. This is also why 5 := iAge; is impossible — you cannot pour a variable into a plain number.

Initialising Variables

Always Initialise

Set a starting value before using a variable, especially counters and totals — otherwise Delphi may assign a random garbage value.

Delphi
i        := 0;
iTotal   := 0;
sReverse := '';
cLetter  := '';

Using Variables

Delphi
lblFullName.Caption := sName + ' ' + sSurname;
memOut.Lines.Add(sName + ' ' + sSurname);
iAnswer := iNum1 + iNum2;
memOut.Lines.Add(IntToStr(iNum1) + ' + ' + IntToStr(iNum2)
               + ' = ' + IntToStr(iAnswer));
Special Characters — New Lines and Tabs

Two ASCII character codes are handy for laying out text: #13 forces whatever follows onto a new line (it is the code for a carriage return), and #9 inserts a tab space. Join either one into a string with +, exactly like any other piece of text:

Delphi
lblResult.Caption := 'Test 1: 78%' + #13 + 'Test 2: 84%';
memOut.Lines.Add('Player' + #9 + 'Score');

Incrementing and Decrementing Variables

Instead of writing out a full assignment every time a counter needs to move up or down by one, Delphi gives you two ready-made procedures: Inc and Dec. Call one with just the variable name and it steps by one; give it a second number and it steps by that amount instead.

StatementEquivalent to
Inc(iVisitors);iVisitors := iVisitors + 1;
Inc(iVisitors, 5);iVisitors := iVisitors + 5;
Dec(iStock);iStock := iStock - 1;
Dec(iStock, 5);iStock := iStock - 5;
Handy for Counters

You'll reach for Inc most often inside a loop, nudging a counter forward one step per repetition — Inc(iCount); reads more naturally than repeating the full assignment statement every time.

Getting Input with InputBox

Not every value needs its own Edit box sitting on the form. The InputBox function opens a small dialog of its own, waits for the user to type something, and hands back whatever they entered — always packaged as a string, no matter what kind of data it looks like.

Delphi — InputBox(title, prompt, default)
sTeam := InputBox('House Team', 'Which house are you in?', '');

Since the result is always text, wrap it in a conversion function whenever you actually need a number or a single character:

Required typeExample
stringsTeam := InputBox('House Team', 'Which house are you in?', '');
integeriAge := StrToInt(InputBox('Age', 'How old are you?', ''));
realrHeight := StrToFloat(InputBox('Height', 'What is your height in metres?', ''));
charcGrade := InputBox('Grade', 'Enter your grade symbol:', '')[1]; — the [1] keeps only the first character typed

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 :=.

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

FunctionConvertsExample
StrToInt()String → IntegeriAge := StrToInt(edtAge.Text);
StrToFloat()String → RealrPrice := StrToFloat(edtPrice.Text);
IntToStr()Integer → StringlblOut.Caption := IntToStr(iTotal);
FloatToStr()Real → StringlblOut.Caption := FloatToStr(rAverage);
The Val procedure — converting safely

StrToInt 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).

Delphi — Val(sourceString, numberResult, errorPosition)
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

Delphi
// FloatToStrF(value, format, totalDigits, decimals)
lblPrice.Caption := FloatToStrF(rPrice, ffCurrency, 4, 2);  // → R24.50
lblPrice.Caption := FloatToStrF(rPrice, ffFixed,    4, 2);  // → 24.50
FormatDescription
ffCurrencyAdds a currency symbol to the formatted value
ffExponentWrites the value out in scientific notation
ffFixedRounds the value to however many decimal places you ask for
ffGeneralPlain number formatting — decimals are shown only when the value actually has them
ffNumberWorks like ffFixed, but breaks large numbers up with thousands separators

Types of Errors

Error TypeWhen it occursExample
Syntax ErrorCode is typed incorrectly — won't compileMissing semicolon, missing end
Runtime ErrorCrashes while the program is runningDividing by zero
Logical ErrorRuns, but gives wrong answerCalculating average before total, infinite loop

Only a syntax error stops the program from compiling at all — Delphi catches it immediately. Runtime and logical errors both let the program compile and run, which makes them harder to catch: a runtime error only shows itself when the exact conditions that break it occur (e.g. the user finally types a letter instead of a number), and a logical error may never show itself at all — the program keeps running and simply produces the wrong answer.

Common causes of runtime errorsCommon causes of logical errors
Converting text that isn't a valid number (StrToInt/StrToFloat)Using the wrong variable in a calculation (e.g. a property was never transferred into it)
Dividing by zeroGetting the sequence of instructions wrong (e.g. calculating a total before all the values were added)
Calculating the square root of a negative numberUsing AND/OR/NOT incorrectly in a compound condition
A calculated answer too big for its variable type (overflow)Ignoring the order of precedence of operators
Accessing an array index that is out of rangeA mistake with the variable controlling a loop, causing it to run forever
GIGO — the root cause of many errors

Many runtime and logical errors trace back to invalid data: Garbage In, Garbage Out. A program is only as reliable as the checks it makes on the data it receives — see Input Validation on the Decision Making page.

Preventing and Handling Runtime Errors with Try..Except

A try..except block protects against any runtime error, not just a bad conversion. Code that might fail — a conversion, a calculation, anything risky — goes inside the try section. The moment something in there raises an error, Delphi abandons whatever was left in that section and jumps straight to except, running the recovery code written there instead of letting the program crash.

Syntax
try
  // risky code that might raise a runtime error
except
  // code that runs only if an error occurred above
end;
Delphi — protecting a StrToFloat conversion
var
  rAmount : real;
begin
  try
    rAmount := StrToFloat(edtAmount.Text);
  except
    ShowMessage('Invalid amount entered');
  end;
end;
Val vs try..except

The Val procedure above and a try..except block solve the same problem in different ways. Val checks a conversion specifically and gives you an error code to test. try..except is more general — it protects any block of code (a conversion, a calculation, a file operation) and reacts only if something actually goes wrong.

Finding Logical Errors — Debugging Tools

Logical errors are the hardest to find because the program runs without crashing. Besides tracing the code by hand with a paper-based trace table, Delphi's built-in debugger lets you watch a program execute one line at a time and see exactly what is happening inside it.

ToolWhat it does
BreakpointClick in the grey margin next to a line of code to mark where execution must pause. When the program reaches that line, Delphi stops and lets you take control instead of running straight through.
Step (F8)Executes only the highlighted line, then pauses again on the next one — lets you watch the program run one instruction at a time.
Watch List (Run → Add Watch)Displays the current value of one or more variables, updated live as each line executes — so you can see exactly where a variable gets the wrong value.
Stop guessing, start watching

It is tempting to change lines of code at random when the output looks wrong, hoping something sticks. That approach wastes time and can introduce new bugs on top of the original one. Watching the variables change value one line at a time pinpoints the exact instruction responsible, so the fix is targeted instead of accidental.