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.
- Less code – printing the numbers 1 to 100 takes three lines with a loop instead of one hundred lines without one.
- Fewer mistakes – you write the instruction once, so there is only one place where a typo could hide.
- Flexibility – the same loop can run 5 times or 5 000 times just by changing one number, so your program copes with any size of data.
- Processing collections – loops let you visit every item in a list, every learner in a class, or every character in a string, one at a time.
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?
- FOR – use it when you know the exact number of repeats. Analogy: climbing a staircase with a known number of steps — you count "step 1, step 2…" up to the top.
- WHILE – use it when you do not know how many times, and you must check first before doing anything. Analogy: filling a glass of water — you keep checking "is it full yet?" before each pour, and if the glass was already full you never pour at all.
- REPEAT…UNTIL – use it when you do not know how many times, but you must do the action at least once and check afterwards. Analogy: asking someone for their PIN — you always ask once, then check if it was right, and ask again only if it was wrong.
Types of Loops
| Loop | Type | Use when… |
|---|---|---|
for … do | Unconditional (fixed) | You know exactly how many times |
while … do | Conditional (pre-test) | Repeat WHILE a condition is true — check first |
repeat … until | Conditional (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.
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.
for counter := lowest to highest do
begin
// code repeated each iteration
end;for i := 1 to 5 do
begin
memOut.Lines.Add(IntToStr(i));
end;FOR … DOWNTO (counting down)
for i := 5 downto 1 do
begin
memOut.Lines.Add(IntToStr(i));
end;FOR Loop — Running Total
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.
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.
while condition do
begin
// code
end;The counter must be initialised before the loop and incremented inside the loop — otherwise it runs forever and crashes your program.
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
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.
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.
repeat
// code
until condition;var
i: Integer;
begin
i := 1;
repeat
memOut.Lines.Add(IntToStr(i));
i := i + 1;
until i > 10;
end;REPEAT — Validate input
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?
| Situation | Best Loop |
|---|---|
| Print numbers 1 to 100 | for |
| Read until user types "stop" | while or repeat |
| Must run at least once (e.g. menu) | repeat |
| Unknown number of iterations | while or repeat |
| Process each element in an array | for |