For…End Statement (FPScript)

09.03.2021

Repeats a series of statements several times until a loop counter reaches a certain value.

Syntax

For Counter = Start To End[StepIncrement] Do

   [Statements]

End

The syntax of the For...End statement consists of the following elements:

Element

Description

Counter

Variable that is used as a loop counter. You do not have to declare the loop counter with Dim.

Start

Starting value of Counter.

End

End value of Counter.

Increment

The optional increment for the counter. The default is 1.

Statements

One or more statements between For and End that are repeated until the Counter exceeds the end value.

Remarks

After all of the statements have been executed in the first loop cycle, the program increments the value of Counter by Increment. The statements in the loop are then either executed again (based on the same test that was carried out at the start of the first cycle), or the program ends the loop and continues the execution with the statement that follows the End statement.

You obtain a reverse loop when you use a negative increment and specify an end value that is smaller than the starting value.

You can change the value of Counter within a loop, but this makes comprehension and testing of the code more difficult.

For Start, End and Increment you can specify the quantities. Counter obtains the unit of Start. End and Increment are transformed to this unit before testing or addition as long as quantities are specified. Otherwise, these are treated as being present in the unit of Start.

Note:   If you use the loop counter to index values from data sets, you should specify Start, End and, if applicable, Increment as integers (append the suffix 'n').

You can nest For...End loops by positioning a For...End loop within another. Use a unique variable name as the counter for each loop. The following construction is correct:

 

For I = 1n To 10n Do

    For J = 1n To 10n Do

        For K = 1n To 10n Do

        . . .

        End

    End

End

Note:   You should avoid loops over individual values of a data set, if possible. FPScript makes it possible for you to calculate complete data sets in one statement. Loops can usually be replaced by functions for event isolation in conjunction with the Index operator. The For Each Value...End loop is the fastest FPScript loop.

Available in

FlexPro View, Basic, Professional, Developer Suite

Examples

The following example calculates the amplitudes for individual waves in a sinusoidal signal and returns these as a data series:

Arguments Data

Dim Idx, Result, n

Idx = LevelCrossings(Data, 0, 0, EVENT_INDEX + EVENT_POSITIVE)

n = NumberOfRows(Idx)

Result = 0. # 0    // Empty data series

For i = 1n To n - 1n Do

    Result := Maximum(Data[Idx, Idx])

End

Result

 

The following example shows a loop that uses units:

For s = 0 km To 100 km Step 250 m Do

    ...

End

 

The following example shows a reverse loop:

For i = 100n To 0n Step -1n Do

    ...

End

See Also

For Each Element...End Statement

For Each Value...End Statement

For Each Column...End Statement

For Each Row...End Statement

While...End Statement

Do...While Statement

Share article or send as email:

You might be interested in these articles