HexLibraryHexLibrary
</> HexCodeSign inSign up
PythonJavaScriptTypeScriptCC++JavaPHPGoRustRubyBashSQLLuaPerlCOBOLPascalDelphiFortranDartSwiftC#JuliaGroovyRElixirHaskellKotlinScalaClassic ASPClipperMS-DOS
LESSONS
Introduction to PascalVariablesData Types & ConversionOperatorsStringsConditions (if)Loops (for / while)Functions
REFERENCE
📖 Overview
🖥️ Output
📦 Variables
✍️ String
🔁 Condition & Loop
🧩 Function & Procedure
Pascal›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

Example code
1
program H;
2
var i: integer;
3
begin
4
for i := 1 to 3 do
1, 2, 3 (3 included).
Loop1, 2, 3 (3 included).
5
writeln(i);
6
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 for.

🎯 Expected output: 1 2 3 4 5

Loading editor…

About this topic

There are three loop forms in Pascal: the counted for loop, the pre-test while loop, and the post-test repeat loop. Each serves a different need.

The bounds of a counted loop are inclusive: when you write from one to ten, ten runs as well. Also, you cannot modify the loop variable inside the body; the compiler does not allow this, and it is a deliberate safety measure.

The logic of a post-test loop is inverted: it stops when the condition becomes true, whereas similar loops in other languages continue as long as the condition is true. If this difference is missed, an infinite loop occurs.

What you will learn

  • The counter-based loop
  • The inclusion of bounds
  • the while loop
  • the post-test loop
  • Avoiding infinite loops

Who is this for?

  • Students learning to write loops
  • Those who think the upper bound is exclusive
  • Those who confuse the post-test loop

When to use?

Use a counted loop if the number of repetitions is known in advance, and a conditional loop if it is not.

If the body must run at least once, a post-test loop is appropriate; the scenario of getting valid input from the user is a typical example.

Common mistakes

Wrong

program Test;
var
  i: Integer;
begin
  for i := 1 to 5 do;
    writeln(i);
end.

The semicolon after do creates an empty body; the loop does nothing five times, and then the print runs once. The compiler does not give an error, and the output is completely different from expected. It is one of the most insidious bugs in Pascal.

Correct

program Test;
var
  i: Integer;
begin
  for i := 1 to 5 do
    writeln(i);
end.

Wrong

program Test;
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

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

Wrong

program Test;
var
  i: Integer;
begin
  i := 1;
  repeat
    writeln(i);
    i := i + 1;
  until i <= 5;
end.

The post-test loop stops when the condition is true; here, since the condition is already true on the first turn, the loop runs once and finishes. If the intention is to run it five times, you must write the reverse of the condition. Inverted logic is the most common mistake.

Correct

program Test;
var
  i: Integer;
begin
  i := 1;
  repeat
    writeln(i);
    i := i + 1;
  until i > 5;
end.

Frequently asked questions

How do I count backwards?

In a counted loop, you change the keyword that specifies direction; the start value is greater than the end, and the counter decreases by one. The step size cannot be changed, it is always one.

Why can't I modify the loop variable?

Because so that the compiler can calculate the loop count in advance and perform optimizations. Interfering with the counter inside the body would make the loop's behavior unpredictable. If you need to, you should use a conditional loop.

How do I exit a loop early?

Modern Pascal compilers have break and continue routines. Standard Pascal did not have these, and it was necessary to use a conditional loop with a flag variable; this pattern is frequently seen in legacy code.

Summary

Boundaries are inclusive in the counter-based loop.

a semicolon after do produces an empty body.

The loop variable cannot be modified within the body.

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

The post-test loop stops when the condition is true.

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