LearnStanmorephysics

Grade 10 · Delphi Foundations

Loops with running totals

Grade 10 · Loops & first strings · Lesson and coding task · about 14 minutes

Open this task in the Delphi workspace →

The running total is the single most useful loop pattern you will ever learn. Start a total at 0 before the loop, then add to it inside the loop:

rTotal := 0;                        // initialise BEFORE the loop
for i := 1 to iMonths do
begin
  rTotal := rTotal + rMonthly;      // accumulate
  memOutput.Lines.Add('Month ' + IntToStr(i) + ': R ' +
      FloatToStrF(rTotal, ffFixed, 10, 2));
end;

rTotal := rTotal + rMonthly reads strangely at first — "the new total becomes the old total plus this month's money". That one line powers every sum, average and counter you will write.

Your task: stokvel savings

A stokvel member saves a fixed amount every month. Show the balance month by month, and the final amount on the label.

Exam tip. Forgetting to initialise the total to 0 is the classic paper-killer — on the second button click your total carries on growing!

What you will do

Hints
  1. rTotal := 0; must happen before the loop starts.
  2. Inside the loop: rTotal := rTotal + rMonthly; then add the memo line.
  3. The loop body has two statements, so it needs begin … end.
Model solution
procedure TfrmStokvel.btnCalcClick(Sender: TObject);
var
  rMonthly, rTotal : Real;
  i, iMonths : Integer;
begin
  rMonthly := StrToFloat(edtMonthly.Text);
  iMonths := StrToInt(edtMonths.Text);
  rTotal := 0;
  for i := 1 to iMonths do
  begin
    rTotal := rTotal + rMonthly;
    memOutput.Lines.Add('Month ' + IntToStr(i) + ': R ' + FloatToStrF(rTotal, ffFixed, 10, 2));
  end;
  lblTotal.Caption := 'Saved: R ' + FloatToStrF(rTotal, ffFixed, 10, 2);
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.