You are here: Core Reference > Coding > Logic > FOR - TO - BY

FOR - TO - BY

You can use the FOR - TO - BY statement to create a loop—the program repeats a set of statements on each execution, until a specific value is reached.

When you use a FOR - TO - BY statement, you need to enter it in this format:

Where <Variable> is the name of the value that will be used to determine whether the program continues to execute the <Statements>. With FOR - TO - BY statements, you enter the variable in the FOR - TO - BY declaration and not in the VAR list. The variable has to be an integer.

The <Initial value> is the starting value of the integer <Variable>.

TO <End loop value> defines the value at which the program will stop executing the <Statements>. When the <Variable> reaches the <End loop value>, the program will no longer attempt to execute the <Statements>.

BY <Decrement value> defines the amount by which the <Variable> value will change upon each execution. The BY declaration is optional and is +1 by default.

DO <Statements> defines the statements that the program performs each time that the program executes and the <Variable> value is not the <End loop value>.

Example:

  • FOR InputA := 10 TO -10 BY -1 DO
    • Square[InputA] := InputA * InputA;
  • END_FOR;

In this example, the FOR - TO - BY statement instructs the program to decrease the InputA value by 1 on each execution of the program. It will decrease the InputA value by 1 until the InputA value is -10.

The Square[InputA] : InputA * InputA calculation is performed after each execution. This array is used to produce the square values of the array variables (which would be defined in the array above the FOR - TO - BY statement in the program).

As the array values are calculated based on the current value of InputA, it means that each time the program executes, it provides a new set of results for the array. This occurs due to the FOR - TO - BY statement decreasing the InputA value by 1 for each execution (until the InputA value is -10). As the InputA value is lower for each execution, the results for the array values are different for each execution (the program uses the InputA value to calculate the values for the array).


ClearSCADA 2015 R2