Grade 10 · Data & calculations · Lesson and coding task · about 12 minutes
Open this task in the Delphi workspace →
A variable is a named space in memory that holds one value. Before you use a variable in Delphi you must declare it in a var section, choosing a data type:
| Type | Holds | Prefix | Example value |
|---|---|---|---|
| Integer | whole numbers | i | 16 |
| Real | decimal numbers | r | 1.62 |
| String | text | s | 'Amahle' |
| Char | one character | c | 'A' |
| Boolean | True / False | b | True |
var
sName : String;
iAge : Integer;
rHeight : Real;
begin
sName := 'Amahle'; // := means "becomes"
iAge := 16;
rHeight := 1.62;
The := is the assignment operator — "sName becomes Amahle". A single = is only used for comparing.
Joining text and numbers
A memo line must be a String. To include a number you must convert it first:
memOutput.Lines.Add('Age: ' + IntToStr(iAge));
memOutput.Lines.Add('Height: ' + FloatToStr(rHeight) + ' m');
What you will do
- Declare sName (String), iAge (Integer) and rHeight (Real) in the var section
- Assign: sName := 'Amahle'; iAge := 16; rHeight := 1.62;
- Output three memo lines exactly: Name: Amahle / Age: 16 / Height: 1.62 m
Hints
- The var section goes between the procedure header and begin. Each declaration looks like name : Type;
- Assignment uses := , e.g. iAge := 16;
- To join a number into the text use IntToStr(iAge) and FloatToStr(rHeight). Note the space in ' m'.
Model solution
procedure TfrmProfile.btnShowClick(Sender: TObject);
var
sName : String;
iAge : Integer;
rHeight : Real;
begin
sName := 'Amahle';
iAge := 16;
rHeight := 1.62;
memOutput.Lines.Add('Name: ' + sName);
memOutput.Lines.Add('Age: ' + IntToStr(iAge));
memOutput.Lines.Add('Height: ' + FloatToStr(rHeight) + ' m');
end;