Grade 10 · Decisions · Lesson and coding task · about 12 minutes
Open this task in the Delphi workspace →
Programs make decisions with IF. The condition must be a Boolean — something that is True or False:
if iAge >= 17 then
lblResult.Caption := 'You may apply!'
else
lblResult.Caption := 'Not yet…';
Two rules that catch everyone at first:
- No semicolon before ELSE. The line above the
elsemay not end with; - If a branch has more than one statement, wrap it in
begin … end.
The comparison operators are =, <> (not equal), <, >, <= and >=.
Your task
In South Africa you may apply for a learner's licence at 17. Read an age and tell the user whether they can apply — and if not, exactly how many years they still have to wait.
What you will do
- Read the age from edtAge
- If the age is 17 or more, lblResult shows: You may apply for a learner's licence!
- Otherwise it shows: Only N more year(s) to go (where N = 17 - age)
Hints
- The condition is iAge >= 17
- Inside a string, an apostrophe is typed twice: 'learner''s'
- The waiting years are 17 - iAge. Remember: no semicolon on the line before else!
Model solution
procedure TfrmLicence.btnCheckClick(Sender: TObject);
var
iAge : Integer;
begin
iAge := StrToInt(edtAge.Text);
if iAge >= 17 then
lblResult.Caption := 'You may apply for a learner''s licence!'
else
lblResult.Caption := 'Only ' + IntToStr(17 - iAge) + ' more year(s) to go';
end;