Loops Grade 10

Loops repeat a block of code multiple times. Delphi has three loop types — choose based on whether you know how many times to repeat.

What is a Loop?

A loop is a programming structure that repeats a block of code over and over until a certain point is reached. Instead of writing the same instruction many times, you write it once and tell the computer how often, or until when, to repeat it.

Everyday analogy: Think of doing push-ups. The coach does not shout "do a push-up" fifty separate times. He says "do fifty push-ups." That single instruction, repeated until you reach fifty, is a loop. The action (one push-up) is the loop body, and "fifty" is the condition that tells you when to stop.

Why Do We Use Loops?

Loops save us from repeating ourselves and let programs handle any amount of work with the same short piece of code.

The Three Loop Types in Plain Words

Choosing the right loop comes down to one question: do you know in advance how many times to repeat?

Types of Loops

LoopTypeUse when…
for … doUnconditional (fixed)You know exactly how many times
while … doConditional (pre-test)Repeat WHILE a condition is true — check first
repeat … untilConditional (post-test)Repeat UNTIL a condition is true — always runs at least once

FOR Loop (Fixed / Unconditional)

Runs a set number of times. The counter increments or decrements automatically.

We call it fixed or unconditional because the number of repeats is decided before the loop even starts and cannot change while it runs. You give it a start value and an end value, and Delphi handles the counting for you — you never have to add 1 to the counter yourself.

WHEN TO USE

Reach for a FOR loop whenever you can finish the sentence "do this exactly ___ times" with a number — printing 1 to 10, working through all 30 learners in a class, or drawing 12 rows of a table.

Syntax — counting UP
for counter := lowest to highest do
begin
  // code repeated each iteration
end;
Delphi — print 1 to 5
for i := 1 to 5 do
begin
  memOut.Lines.Add(IntToStr(i));
end;

FOR … DOWNTO (counting down)

Delphi — countdown 5 to 1
for i := 5 downto 1 do
begin
  memOut.Lines.Add(IntToStr(i));
end;

FOR Loop — Running Total

Delphi — sum 1 to 10
var
  i, iTotal: Integer;
begin
  iTotal := 0;  // initialise!
  for i := 1 to 10 do
    iTotal := iTotal + i;
  lblTotal.Caption := 'Sum = ' + IntToStr(iTotal);
end;

WHILE … DO Loop (Pre-Conditional)

Checks the condition before each iteration. If the condition is false from the start, the loop body never runs.

We call it pre-conditional ("pre" = before) because it tests the condition first, and only then decides whether to run the body. This makes it the safe choice when there is a chance the loop should not run at all.

WHEN TO USE

Use WHILE when you do not know how many repeats you need and it might be zero — for example, "keep reading numbers while the user hasn't typed 0". If they type 0 immediately, you correctly do nothing.

Syntax
while condition do
begin
  // code
end;
Avoid Infinite Loops

The counter must be initialised before the loop and incremented inside the loop — otherwise it runs forever and crashes your program.

Delphi — while counter ≤ 5
var
  Counter: Integer;
begin
  Counter := 1;           // initialise BEFORE loop
  while Counter <= 5 do
  begin
    memOut.Lines.Add('Counter: ' + IntToStr(Counter));
    Inc(Counter);          // increment INSIDE loop
  end;
end;

WHILE — Reading user input until sentinel

Delphi — keep adding until user enters 0
var
  iNum, iTotal: Integer;
begin
  iTotal := 0;
  iNum   := StrToInt(InputBox('Input', 'Enter number (0 to stop):', ''));
  while iNum <> 0 do
  begin
    iTotal := iTotal + iNum;
    iNum   := StrToInt(InputBox('Input', 'Enter number (0 to stop):', ''));
  end;
  lblTotal.Caption := 'Total: ' + IntToStr(iTotal);
end;

REPEAT … UNTIL Loop (Post-Conditional)

Runs the body first, then checks the condition. Guaranteed to run at least once. Loop continues while condition is false; stops when it becomes true.

We call it post-conditional ("post" = after) because it does the work first and asks the question afterwards. That is the opposite of the WHILE loop, and it is exactly what you want when the action must happen at least one time no matter what.

WHEN TO USE

Use REPEAT…UNTIL when the body must run at least once — like showing a menu and asking the user to choose, or asking for a valid mark and re-asking until they get it right. You always ask once, then keep asking until the answer is acceptable.

Syntax
repeat
  // code
until condition;
Delphi — repeat until i = 10
var
  i: Integer;
begin
  i := 1;
  repeat
    memOut.Lines.Add(IntToStr(i));
    i := i + 1;
  until i > 10;
end;

REPEAT — Validate input

Delphi — keep asking until valid mark
var
  iMark: Integer;
begin
  repeat
    iMark := StrToInt(InputBox('Mark', 'Enter mark (0-100):', ''));
    if (iMark < 0) OR (iMark > 100) then
      ShowMessage('Invalid! Enter 0-100');
  until (iMark >= 0) AND (iMark <= 100);
end;

Comparison: Which Loop to Use?

SituationBest Loop
Print numbers 1 to 100for
Read until user types "stop"while or repeat
Must run at least once (e.g. menu)repeat
Unknown number of iterationswhile or repeat
Process each element in an arrayfor