HexLibraryHexLibrary
</> HexCodeSign inSign up
PythonJavaScriptTypeScriptCC++JavaPHPGoRustRubyBashSQLLuaPerlCOBOLPascalDelphiFortranDartSwiftC#JuliaGroovyRElixirHaskellKotlinScalaClassic ASPClipperMS-DOS
LESSONS
Introduction to ClipperVariablesStringsConditions (IF / DO CASE)Loops (FOR / DO WHILE)DBF Database
REFERENCE
📖 Overview
🖥️ Output
📦 Variables
✍️ Text & Number
🔁 Condition & Loop
🗃️ Functions & Database
Clipper›Loops (FOR / DO WHILE)

Loops (FOR / DO WHILE)

Counting loops with FOR, conditional repetition with DO WHILE.

Simple enough for a 5-year-old to understand

FOR ... NEXT

Example code
1
LOCAL i
2
FOR i := 1 TO 5
From 1 to 5, including 5.
LoopFrom 1 to 5, including 5.
3
QOut(i)
Prints the number in each iteration.
Output / InputPrints the number in each iteration.
4
NEXT
End of the loop.
LoopEnd of the loop.
LoopOutput / Input
💡 Hint: You can use STEP to change the step size.

🎯 Exercise & Practice

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

1.Which keyword is used to close a FOR loop?

About this topic

There are two basic loops in Clipper: the counted loop and the conditional loop. The boundaries of the counted loop are inclusive and a step value can be provided; the conditional loop runs as long as the condition is true.

There are two statements to manage loop flow: one ends the loop completely, while the other skips that iteration and re-evaluates the condition. The name of the second statement can be misleading at first glance because it resembles a loop-starting keyword in other languages.

The most common use of loops in Clipper is to traverse a data file record by record. In this pattern, forgetting to advance the record pointer leads to an infinite loop, which is the most well-known bug in the language.

What you will learn

  • The counter-based loop
  • The conditional loop
  • Early exit and skipping iterations
  • Traversing an array
  • The record traversal pattern

Who is this for?

  • Developers traversing a data file
  • Those dealing with infinite loops
  • Array processors

When to use?

A counted loop is used when the number of repetitions is known; deriving the upper limit from the array's length when traversing an array prevents the code from breaking when the size changes.

When reading a data file, a conditional loop combined with an end-of-file indicator is the standard pattern, and advancing the pointer in each iteration is mandatory.

Common mistakes

Wrong

PROCEDURE Main()
   USE musteri
   DO WHILE !Eof()
      ? musteri->ad
   ENDDO
   RETURN

Because the record pointer is not advanced, the end-of-file indicator never becomes true; the same record is written infinitely. You need to move to the next record in each iteration. This is the most common infinite loop in the xBase family.

Correct

PROCEDURE Main()
   USE musteri
   DO WHILE !Eof()
      ? musteri->ad
      SKIP
   ENDDO
   RETURN

Wrong

PROCEDURE Main()
   LOCAL dizi := { "a", "b", "c" }
   LOCAL i
   FOR i := 1 TO 4
      ? dizi[i]
   NEXT
   RETURN

The array has three elements, but the loop goes up to four; accessing the fourth index produces an out-of-bounds error. Using the function that returns the array's length instead of hardcoding the upper limit ensures the code remains correct on its own when the array grows or shrinks.

Correct

PROCEDURE Main()
   LOCAL dizi := { "a", "b", "c" }
   LOCAL i
   FOR i := 1 TO Len(dizi)
      ? dizi[i]
   NEXT
   RETURN

Wrong

PROCEDURE Main()
   LOCAL i := 1
   DO WHILE i <= 5
      IF i == 3
         LOOP
      ENDIF
      ? i
      i++
   ENDDO
   RETURN

The skip iteration statement returns to the condition, but since the iteration is skipped without incrementing the counter, the condition remains the same forever and the program freezes. When using a skip, the progression must happen before the skip; the counted loop completely eliminates this risk.

Correct

PROCEDURE Main()
   LOCAL i
   FOR i := 1 TO 5
      IF i == 3
         LOOP
      ENDIF
      ? i
   NEXT
   RETURN

Frequently asked questions

How do you perform an early exit?

The exit statement immediately ends the loop and continues from the next line. In nested loops, it only exits the innermost loop; if you also need to exit the outer loop, you must use a flag variable.

How do I count backwards?

In a counted loop, the step value is given as negative and the start is greater than the end. If no step is specified, it increments by one. Traversing arrays from back to front prevents index shifting when deleting elements during traversal.

Can I traverse an array with a code block?

Yes, code blocks introduced with Clipper 5 can be used with library functions that apply an operation to each element of an array. This format is shorter than writing a loop and approaches functional programming patterns.

Summary

Boundaries are inclusive in the counter-based loop.

In a conditional loop, you are responsible for advancing the progression.

Failing to advance the record pointer produces an infinite loop.

The array boundary is written using the length function.

When skipping an iteration, progression must be done first.

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