HexLibraryHexLibrary
</> HexCodeSign inSign up
PythonJavaScriptTypeScriptCC++JavaPHPGoRustRubyBashSQLLuaPerlCOBOLPascalDelphiFortranDartSwiftC#JuliaGroovyRElixirHaskellKotlinScalaClassic ASPClipperMS-DOS
LESSONS
Introduction to DelphiVariablesData Types & ConversionOperatorsStringsConditions (if)Loops (for / while)Functions
REFERENCE
📖 Overview
🖥️ Output
📦 Variables
🔄 Data Types & Conversion
✍️ String
🔁 Condition & Loop
🧩 Function & Procedure
Delphi›Loops (for / while)

Loops (for / while)

for (includes the end value), while, and multiline body with begin..end.

Simple enough for a 5-year-old to understand

for loop

for i := 1 to 5 → from 1 to 5 (5 inclusive).

Example code
1
program H;
2
{$MODE DELPHI}
3
var i: Integer;
4
begin
5
for i := 1 to 3 do
1, 2, 3 (3 included).
Loop1, 2, 3 (3 included).
6
writeln(i);
7
end.
Loop
💡 Hint: for i := 1 to 5 includes the end value. Use downto for counting down.

🎯 Exercise & Practice

Test what you've learned — answer, write code, check instantly. (1 left)

1.Print the numbers from 1 to 5 (inclusive) one below the other using a for loop.

🎯 Expected output: 1 2 3 4 5

Loading editor…

About this topic

Delphi offers three loop forms: counter-controlled, pre-test, and post-test. There is also an element loop that allows iterating over collections, and it is the most readable choice for arrays and lists.

The boundaries of a counter-controlled loop are inclusive, and the loop variable cannot be modified inside the body. After the loop finishes, the value of the counter is considered undefined; writing logic dependent on it may yield different results depending on the compiler version.

The logic of a post-test loop is reversed: it stops when the condition becomes true. This guarantees that the body runs at least once, but writing the condition incorrectly leads to an infinite loop.

What you will learn

  • The counter-based loop
  • The element loop
  • Conditional loops
  • Early exit and skipping
  • Traversing an array

Who is this for?

  • Developers processing collections
  • Those falling into an infinite loop
  • Those confusing array bounds

When to use?

If all elements of a collection need to be traversed, the element loop completely eliminates index errors.

If an index is needed, a counter-controlled loop is used; deriving the upper bound from the length instead of hardcoding it prevents the code from breaking when the array changes.

Common mistakes

Wrong

var
  dizi: array[0..4] of Integer;
  i: Integer;
begin
  for i := 0 to 5 do
    dizi[i] := i;
end.

An array holds five elements from zero to four; writing to the fifth index is out of bounds. If checks are enabled, it throws a range error; if disabled, it corrupts neighboring memory. Instead of hardcoding the upper bound, you should query the high index of the array.

Correct

var
  dizi: array[0..4] of Integer;
  i: Integer;
begin
  for i := Low(dizi) to High(dizi) do
    dizi[i] := i;
end.

Wrong

var
  i: Integer;
begin
  i := 1;
  while i <= 5 do
    writeln(i);
end.

Since the counter is not incremented, the condition remains true forever and the program never ends. In conditional loops, you have to ensure the progression yourself; if the number of repetitions is known, a counter-controlled loop eliminates this risk.

Correct

var
  i: Integer;
begin
  i := 1;
  while i <= 5 do
  begin
    writeln(i);
    Inc(i);
  end;
end.

Wrong

var
  liste: TStringList;
  s: string;
begin
  liste := TStringList.Create;
  liste.Add('a');
  liste.Add('b');
  for s in liste do
    liste.Add(s);
end.

Because the collection being traversed is modified inside the loop, the behavior becomes unpredictable; as additions to the list continue, the loop grows and memory runs out. Do not modify while traversing; collect the results in a separate list and combine them after the loop.

Correct

var
  liste, yeniler: TStringList;
  s: string;
begin
  liste := TStringList.Create;
  yeniler := TStringList.Create;
  liste.Add('a');
  liste.Add('b');
  for s in liste do
    yeniler.Add(s);
  liste.AddStrings(yeniler);
end.

Frequently asked questions

How do I break out of the loop?

The exit routine ends the loop completely, while the skip routine merely skips the current iteration and moves on to the next one. Both work in all three loop forms and reduce the need for flag variables.

What happens to the counter value after the loop?

The standard considers the counter value undefined after the loop finishes. If you need the value, copy it to another variable inside the loop; otherwise, you are relying on behavior dependent on the compiler version.

Where can the element loop be used?

In arrays, strings, sets, and classes that provide enumerators. It should be preferred anywhere you don't need an index; it reduces the probability of out-of-bounds errors to zero.

Summary

Boundaries are inclusive in the counter-based loop.

Array bounds are written by querying rather than hardcoding them.

In a conditional loop, you must ensure the progression yourself.

The collection being traversed must not be modified inside the loop.

Exit and skip routines manage the flow.

HexLibraryHexLibrary

Learn programming more clearly. Every command is explained line by line with colors, reinforced with runnable exercises, test your code instantly — all in one place, free.

Learn

  • Home
  • Languages & Lessons
  • HexCode

Company

  • About
  • Contact

Legal

  • Privacy Policy
  • Terms of Use

Other Projects

  • HexToolz
  • Free Service Manuals
© 2026 HexLibrary · The clearest way to learn code