Please ensure Javascript is enabled for purposes of website accessibility
Powered by Zoomin Software. For more details please contactZoomin

Application Server

FOR … TO … STEP … NEXT loop

  • Last UpdatedJul 22, 2024
  • 2 minute read

FOR-NEXT performs a function (or set of functions) within a script several times during a single execution of a script. The general format of the FOR-NEXT loop is as follows:

FOR <analog_var> = <start_expression> TO <end_expression> [STEP <change_expression>];

[statements];

[EXIT FOR;];

[statements];

NEXT;

Where:

  • analog_var is a variable of type Integer, Float, Real, or Double.

  • start_expression is a valid expression to initialize analog_var to a value for execution of the loop.

  • end_expression is a valid expression. If analog_var is greater than end_expression, execution of the script jumps to the statement immediately following the NEXT statement.

    This holds true if loop is incrementing up, otherwise, if loop is decrementing, loop termination occurs if analog_var is less than end_expression.

  • change_expression is an expression that defines the increment or decrement value of analog_var after execution of the NEXT statement. The change_expression can be either positive or negative.

    • If change_expression is positive, start_expression has to be less than or equal to end_expression or the statements in the loop don't execute.

    • If change_expression is negative, start_expression has to be greater than or equal to end_expression for the body of the loop to be executed.

  • If STEP is not set, then change_expression defaults to 1 for increasing increments, and defaults to -1 for decreasing increments.

Exit the loop from within the body of the loop with the EXIT FOR statement.

The FOR loop is executed as follows:

  1. analog_var is set equal to start_expression.

  2. If change_expression is positive, the system tests to see if analog_var is greater than end_expression. If so, the loop exits. If change_expression is negative, the system tests to see if analog_var is less than end_expression. If so, program execution exits the loop.

  3. The statements in the body of the loop are executed. The loop can potentially be exited via the EXIT FOR statement.

  4. analog_var is incremented by 1,-1, or by change_expression if it is specified.

  5. Steps 2 through 4 are repeated.

FOR-NEXT loops can be nested. The number of levels of nesting possible depends on memory and resource availability.

TitleResults for “How to create a CRG?”Also Available in