LearnStanmorephysics

Grade 10 · Delphi Foundations

IF … THEN … ELSE

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 else may 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

Hints
  1. The condition is iAge >= 17
  2. Inside a string, an apostrophe is typed twice: 'learner''s'
  3. 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;

About this material

This platform provides original CAPS-aligned practice material and study tools. Content is machine-verified and has not been reviewed by subject specialists. It is not affiliated with or endorsed by the Department of Basic Education. Learners should also use official past papers and consult their teachers where uncertain.