Skip to content

For

For

See Also: Boolean Expressions, Execution Control Commands, Loop, Repeat, Until, While, Break, Boolean

Purpose

Executes a block of code in an iteration.

Syntax

For
    {counter}
From
    {start-value}
To
    {stop-value}Loop

What It Does

When a For ... From ... To command line first executes, the value of {start-value} is moved to the variable {counter}. Then (and upon each re-execution of the block), the value of {counter} is compared with that of {stop-value}. If the current value of {counter} is less than or equal to that of {stop-value}, the block of statements executes. Each time the block executes to completion, the value of {counter} is incremented by the Loop, and there is a new range test on {counter}. Once the value of {counter} is greater than {stop-value}, the program continues execution on the line after Loop.

Example

This example displays 10 lines of text to the ShowLn console, using each of the loop constructs:

Integer i
For
    i
From
    1
To
    10
    Showln "For - Line: " i
Loop

Notes

  • {stop-value} is evaluated each time the loop is executed. If {stop-value} is a constant or a variable, the loop will execute faster than if it is a database field or expression.

  • Do not change the values of the loop range or the counter inside the For ... Loop block unless there is no other choice. DataFlex will allow you to change any of these variables, but this practice can lead to many subtle bugs. Changing the start value within a loop will never change the execution of the loop.

  • Use For ... Loop for loops where the counter is incremented by one. Use Repeat…Until and While…Loop for loops of greater complexity.

  • If {start-value} is greater than {stop-value} when the For command line is first executed, the block of statements will not execute.

  • You may place For ... Loop blocks inside other Begin ... End blocks up to a limit of 40. You may freely intermingle them with other block control commands (loops). However, you may not overlap them — you must end the innermost block first, then the next-innermost, and so on.