Grade 10 · Loops & first strings · Lesson and coding task · about 12 minutes
Open this task in the Delphi workspace →
A FOR loop repeats code a known number of times. The loop variable must be an Integer declared in var:
for i := 1 to 5 do
memOutput.Lines.Add('Lap ' + IntToStr(i));
for i := 10 downto 1 do // counts backwards
memOutput.Lines.Add(IntToStr(i));
If the body has more than one statement, wrap it in begin … end;. The loop variable takes each value in turn — that is what makes times tables, totals and patterns possible.
Your task
Print the full times table (1× to 12×) of any number the user enters, like the poster on your primary school wall:
1 x 7 = 7
2 x 7 = 14
…
12 x 7 = 84What you will do
- Read the number from edtNumber
- Loop i from 1 to 12
- Each memo line looks exactly like: 3 x 7 = 21
Hints
- for i := 1 to 12 do
- Each line joins three numbers: IntToStr(i) + ' x ' + IntToStr(iNum) + ' = ' + IntToStr(i * iNum)
- Watch the spaces around x and = — the test checks them exactly.
Model solution
procedure TfrmTables.btnShowClick(Sender: TObject);
var
i, iNum : Integer;
begin
iNum := StrToInt(edtNumber.Text);
for i := 1 to 12 do
memOutput.Lines.Add(IntToStr(i) + ' x ' + IntToStr(iNum) + ' = ' + IntToStr(i * iNum));
end;